<?php
# http://localhost/test_site/php/test/_FileUploader.php
if (!empty($_POST['Submitted']))
{
// estrae e stampa variabili e valori da $_POST
extract($_POST, EXTR_OVERWRITE);
print '<table width="800" border="0" cellspacing="5" cellpadding="5">';
while(list($chiave, $valore)=each($_POST)){
print "<tr><td>".$chiave." : </td><td>".${$chiave}."</td></tr>";
}
if (!GestisciFileToUpload()) exit;
print "</table>";
print "<br /><br /><a href='".$_SERVER['PHP_SELF']."'>TRY AGAIN</a>";
}
else
{
?>
<!DOCTYPE html>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
<table width="500" border="0" cellspacing="5" cellpadding="5">
<caption> </caption>
<tr>
<td colspan="2"><b>FILE UPLOADER</b></td>
</tr>
<tr>
<td><label for="FileToUpload">FileToUpload :</label></td>
<td><input type="file" name="FileToUpload" id="FileToUpload" /></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><input name="submit" type="submit" id="submit" value="Invia"></td>
</tr>
</table>
<input type="hidden" name="Submitted" value="1" />
</form>
</html>
<?PHP
}
function GestisciFileToUpload() {
global $FileToUpload, $errormsg;
$FileToUpload = "";
if ($_FILES["FileToUpload"]["error"] == 4)
return true;
else {
if ($_FILES["FileToUpload"]["error"] > 0) {
print "<font color=red>Upload Return Code: ".$_FILES["FileToUpload"]["error"]."</font><br />";
return false;
} }
$FileToUpload_name = $_FILES["FileToUpload"]["name"];
$FileToUpload_type = $_FILES["FileToUpload"]["type"];
$FileToUpload_size = $_FILES["FileToUpload"]["size"] / 1024;
$FileToUpload_temp = $_FILES["FileToUpload"]["tmp_name"];
$FileToUpload_path = dirname($_FILES["FileToUpload"]["tmp_name"]);
$FileToUpload = $FileToUpload_path."/".$FileToUpload_name;
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["FileToUpload"]["name"]);
$extension = strtolower(end($temp));
if (!in_array($extension, $allowedExts)) {
print "<font color=red>file extension not allowed</font><br />";
// return false;
}
$allowedTypes = array("image/gif", "image/jpeg", "image/jpg", "image/pjpeg", "image/x-png", "image/png");
if (!in_array(strtolower($_FILES["FileToUpload"]["type"]), $allowedTypes)) {
print "<font color=red>file type not allowed</font><br />";
// return false;
}
$maxallowedSize = 2000000;
if ($_FILES["FileToUpload"]["size"] > $maxallowedSize) {
print "<font color=red>file size exceeded</font><br />";
// return false;
}
print "<tr><td>Upload : </td><td>".$FileToUpload_name."</td></tr>";
print "<tr><td>Type : </td><td>".$FileToUpload_type."</td></tr>";
print "<tr><td>Size (kB) : </td><td>".$FileToUpload_size."</td></tr>";
print "<tr><td>Stored in : </td><td>".$FileToUpload_temp."</td></tr>";
print "<tr><td>folder : </td><td>".$FileToUpload_path."</td></tr>";
print "<tr><td>new file : </td><td>".$FileToUpload."</td></tr>";
if (file_exists($FileToUpload)) {
print "<font color=red>".$FileToUpload." a previous copy exists on the server<br />";
print "it will be replaced by the newone</font><br />";
unlink($FileToUpload);
}
// move_uploaded_file($FileToUpload_temp, $UploadPath.$FileToUpload_name); <<-- non gestito
rename ($FileToUpload_temp, $FileToUpload);
if (file_exists($FileToUpload)) {
print "<font color=green>".$FileToUpload." file successfully uploaded</font><br />";
}
return true;
}
?>