ti posto tutti i file
form.php
Codice PHP:
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="700000" />
<input type="file" name="upfile" value="" />
<input type="hidden" name="control" value="upload" />
<input type="submit" value="upload" />
</form>
upload.php
Codice PHP:
<?php
include 'upload.class.php';
$upload = new Upload("./uploads/", "upfile", TRUE, "jpg,gif,png", "control", "upload");
// Eventualmente
echo $upload->ErrorReport;
?>
upload.class.php
Codice PHP:
<?php
class Upload
{
public $SavePath;
public $ErrorReport;
protected $file = array();
protected $AllowedExtensions = array();
protected $SecurityPostName;
protected $SecurityPostKey;
protected $HiddenSecurity;
protected $OverWrite;
public function __construct($path, $name, $overwrite = TRUE, $extensions = "upload/", $PostName ="control", $PostKey = "upfile")
{
$this->SavePath = $path;
$this->file = $_FILES[$name];
$this->OverWrite = $overwrite;
if ($extensions)
{
$this->AllowedExtensions = $this->ParseExtensions($extensions);
}
else
{
$this->AllowedExtensions = FALSE;
}
if($PostName AND $PostKey)
{
$this->SecurityPostKey = $PostKey;
$this->SecurityPostName = $PostName;
$this->HiddenSecurity = TRUE;
}
else
{
$this->HiddenSecurity = FALSE;
}
$this->ErrorManagement();
}
private function ParseExtensions($string)
{
$ExtensionsArray = explode(",", $string);
return $ExtensionsArray;
}
private function FileExtention()
{
$exp = explode(".", $this->file['name']);
$exp = array_reverse($exp);
return $exp[0];
}
protected function ErrorManagement()
{
if ($this->HiddenSecurity)
{
if ($_POST[$this->SecurityPostName] != $this->SecurityPostKey)
{
$this->ErrorReport = "Tentativo non permesso di upload - possibile tentativo di forzatura";
return FALSE;
}
}
if ($this->AllowedExtensions)
{
if (!in_array($this->FileExtention(), $this->AllowedExtensions))
{
$this->ErrorReport = "Estensione file non accettata";
return FALSE;
}
}
if (!$this->OverWrite)
{
$files = scandir($this->SavePath);
if(in_array($this->file['name'], $files))
{
$this->ErrorReport = "Il nome del file caricato esiste già nella cartella di destinazione";
return FALSE;
}
}
switch($this->file['error'])
{
case UPLOAD_ERR_OK:
$this->ErrorReport = "File caricato correttamente";
$this->SaveUploadFile();
break;
case UPLOAD_ERR_INI_SIZE:
$this->ErrorReport = "Il file supera la dimensione massima impostata nel file php.ini (direttiva upload_max_filesize)";
break;
case UPLOAD_ERR_FORM_SIZE:
$this->ErrorReport = "Il file supera la dimensione massima impostata nel form";
break;
case UPLOAD_ERR_PARTIAL:
$this->ErrorReport = "Il file é stato caricato solo parzialmente";
break;
case UPLOAD_ERR_NO_FILE:
$this->ErrorReport = "Nessun file é stato caricato";
break;
case UPLOAD_ERR_NO_TMP_DIR:
$this->ErrorReport = "Nessuna cartella temporanea impostata";
break;
case UPLOAD_ERR_CANT_WRITE:
$this->ErrorReport = "Impossibile scrivere sul disco";
break;
}
}
protected function SaveUploadFile()
{
if(is_uploaded_file($this->file['tmp_name']))
{
move_uploaded_file($this->file['tmp_name'], $this->SavePath . $this->file['name']);
return TRUE;
}
else
{
$this->ErrorReport = "Tentativo non permesso di upload - possibile tentativo di forzatura";
return FALSE;
}
}
}
?>