Sera a tutti,
ho alcuni problemi con un form che si occupa di eseguire l'upload di uno o più file nel mio db. Il problema è che sembra che le variabili passate dai campi hidden non vengano ricevute dalla pagina upload.php, cosa posso fare?
caricafile.php:
Codice PHP:
<?php
session_start();
require_once('mysqlconn.php');
mysql_select_db($database, $utenti);
$id = (int)$_SESSION['id'];
if ($_SESSION['stat'] != true)
header('Location: login.php');
$sql = mysql_query("SELECT * FROM utenti WHERE id = '".$id."' ") or die (mysql_error());
$info = mysql_fetch_array($sql);
$ed = (int)$_POST['ed'];
$doc = (int)$_POST['doc'];
include('funzioni.php');
$abbonamento = ver_abbonamento();
if($_GET['tipo'] != 'contratto' && $abbonamento == false){
header('Location: edificio.php?ed='.$ed.'');
}else{
?>
<html>
<head>
<title>Caricamento file</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="https://bootswatch.com/cosmo/bootstrap.min.css"><!-- Latest compiled and minified CSS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><!-- jQuery library -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script><!-- Latest compiled JavaScript -->
<!--AGGIUNTi -->
<link rel="stylesheet" href="css/global.css">
<script src="js/uploads.js" type="text/javascript"></script>
<meta charset="utf-8" />
<script type="text/javascript">
var inputs = document.getElementById("inputs");
function addInput(event){
var input = document.createElement("input");
input.type = "file";
input.name = "file[]";
input.onchange = addInput;
inputs.appendChild(input);
event.target.onchange = undefined;
}
inputs.getElementsByTagName("input")[0].onchange = addInput;
</script>
</head>
<body onLoad="addInput()">
<br>
<?php include('navbar.php'); ?>
<div id="main" class="container">
<div class="row">
<div class="col">
<?php if((int)$_GET['add'] == 1){
echo "INSERISCI SUBITO IL CONTRATTO DI QUESTO EDIFICIO<br><br>";}?>
<form action="upload.php?ed=<?php echo $ed;?>&doc=<?php echo $doc; ?>&tipo=<?php echo $tipo; ?>" method="post" enctype="multipart/form-data" id="upload" class="upload">
<fieldset>
<legend>Upload files</legend>
<input type="hidden" name="edificio" id="edificio" value="<?php echo $ed; ?>">
<input type="hidden" name="doc" id="doc" value="<?php echo $doc; ?>">
<input type="hidden" name="tipo" id="tipo" value="<?php echo $_GET['tipo']; ?>">
<input type="file" id="file" name="file[]" multiple>
<input type="submit" name="submit" id="submit" value="upload" >
</fieldset>
<div class="bar">
<span id="pb" class="bar-fill"><span id="pt" class="bar-fill-text">0%</span></span>
</div>
<!-- decommentalo se vuoi che ti mostri sotto la progress bar i file uploadati una volta finito (in tal caso decommenta anche la parte commentata nel upload.js nella cartella js <div id="uploads" class="uploads"> Upload file links will apper her. -->
</div>
<script type="text/javascript">
document.getElementById('submit').addEventListener('click', function(e){
e.preventDefault();
var f = document.getElementById('file'), pb = document.getElementById('pb');
pt = document.getElementById('pt');
app.uploader({
files:f,
progressBar: pb,
progressText: pt,
processor: 'upload.php' //commenta o decommenta o rimuovi in base alle tue esigenze
/*finished :
function(data){
var uploads = document.getElementById('uploads'),
succeeded = document.createElement('div'),
failed = document.createElement('div'),
anchor,
span,
x;
if (data.failed.length) {
failed.innerHTML = '<p>Unfortunately, the following.</p>';
};
uploads.innerText = '';
for (x =0; x < data.succeeded.length ; x++) {
anchor = document.createElement('a');
anchor.href = 'upload/' + data.succeeded[x].file;
anchor.innerText = data.succeeded[x].name;
anchor.target = '_blank';
succeeded.appendChild(anchor);
};
for (x =0; x < data.failed.length ; x++) {
span = document.createElement('span');
span.innerText = data.failed[x].name; failed.appendChild(span);
};
uploads.appendChild(succeeded);
uploads.appendChild(failed);
},
error: function(){
console.log('not working');
} */
});
});
</script>
</form>
</div>
</div>
</div>
</body>
</html>
<?php } ?>
upload.php:
Codice PHP:
<?php
header('Content-Type: application/json');
$succeeded = array();
$failed =array();
$uploaded = array();
$allowed = array('mp3', 'jpg');
session_start();
require_once('mysqlconn.php');
mysql_select_db($database, $utenti);
$id = (int)$_SESSION['id'];
if($_SESSION['stat'] != true){
header('Location: login.php');
}
$sql = mysql_query("SELECT * FROM utenti WHERE id = '".$id."' ") or die (mysql_error());
$info = mysql_fetch_array($sql);
$ed = (int)$_GET['edificio'];
$doc = (int)$_GET['doc'];
$tipo = $_GET['tipo'];
include('funzioni.php');
$error = false;
$abbonamento = ver_abbonamento();
if($_POST['tipo'] != 'contratto' && $abbonamento == false){
header('Location: edificio.php?ed='.$ed.'');
}else{
file_put_contents('log.txt', "Abbonamento valido");
file_put_contents('log.txt', "tipo:".$tipo." \n"."file: \n ".$_FILES["file"]."\ned:".$ed);
if(!empty($_FILES["file"]) && !empty($tipo)) {
file_put_contents('log.txt', "File non vuoto");
$controllo = mysql_query("SELECT * FROM edifici WHERE id = '".$ed."' ") or die (mysql_error());
if(mysql_num_rows($controllo) == 0){
$error = true;} //EDIFICIO INESISTENTE
$verifica = mysql_fetch_array($controllo);
if($verifica['utente'] != $id){
$error = true;} //EDIFICIO DI UN ALTRO UTENTE
if($error != true && $tipo != 'identità'){
file_put_contents('log.txt', "Errore nel possesso");
foreach ($_FILES['file']['name'] as $key => $name) {
if ($_FILES['file']['error'][$key] === 0) {
$temp = $_FILES['file']['tmp_name'][$key];
$ext = explode('.', $name);
$ext = strtolower(end($ext));
$file = md5_file($temp) . time() . '.' . $ext;
if (in_array($ext, $allowed) === true /*&& move_uploaded_file($temp, "upload/{$file}") === true*/) {
file_put_contents('log.txt', "File valido");
$file1 = pathinfo($_FILES['file']['name'][$key]);
$data = addslashes(fread(fopen($_FILES['file']['tmp_name'][$key], "rb"),$_FILES['file']['size'][$key]));
$risultato = mysql_query("INSERT INTO file (utente, edificio, nome, tipo, nomefile, file, size, type, estensione, documento) VALUES ('".$id."', '".$ed."', '".mysql_real_escape_string($_POST['nome'])."', '".mysql_real_escape_string(htmlspecialchars($_POST['tipo']))."', '".mysql_real_escape_string(htmlspecialchars($_FILES['file']['name'][$key]))."', '".$data."', '".$_FILES['file']['size'][$key]."', '".mysql_real_escape_string(htmlspecialchars($_FILES['file']['type'][$key]))."', '".$file1['extension']."', '".$doc."') ") or die (mysql_error());
$succeeded[] = array(
'name' => $name,
'file' => $file
);
file_put_contents('log.txt', "Successo");
}else{
$failed[] = array(
'name' => $name);
}
}
}
}
}
if (!empty($_POST['ajax'])) {
echo json_encode(array(
'succeeded' => $succeeded,
'failed' => $failed ));
}
}
?>