Come posso fare in modo che in un form l'upload sia facoltativo?
Io ho un form i cui c'è un campo "immagine" nel quale si seleziona un'immagine jpg dal proprio harddisk, però non deve essere obbligatorio!
Ho usato queste due classi:
Codice PHP:
class Image{
var $src_filename;
var $src_witdh;
var $src_height;
var $src_type;
var $src_attr;
var $src_image;
function Image($filename){
$this->src_filename = $filename;
$this->GetImageInfo();
}
function GetImageInfo(){
list($this->src_width,$this->src_height, $this->src_type, $this->src_attr) = getimagesize($this->src_filename);
}
function CreateSourceImage(){
switch($this->src_type){
case 1:
$this->src_image =imagecreatefromgif($this->src_filename);
break;
case 2:
$this->src_image =imagecreatefromjpeg($this->src_filename);
break;
case 3:
$this->src_image =imagecreatefrompng($this->src_filename);
break;
default: return false;
}
return true;
}
function SaveProportionateImage($filename, $quality, $width){
if ($width < $this->src_width){
$dest_width = $width;
$ratio = $this->src_width / $dest_width;
}
else {
$ratio=1;
$dest_width = $this->src_width;
}
$dest_image = imagecreatetruecolor($dest_width, $this->src_height / $ratio);
imagecopyresampled($dest_image, $this->src_image, 0, 0, 0, 0,
$this->src_width / $ratio,
$this->src_height / $ratio,
$this->src_width,
$this->src_height);
imagejpeg($dest_image, $filename.'.jpg', $quality);
imagedestroy($dest_image);
}
function SaveProportionateImageAll($filename, $quality, $width){
if ($this->src_width > $this->src_height && $width < $this->src_width){
$dest_width = $width;
$ratio = $this->src_width / $dest_width;
}
else if($width < $this->src_height){
$dest_width = $width;
$ratio = $this->src_height / $dest_width;
}
else{
$ratio=1;
$dest_width = $this->src_width;
}
$dest_image = imagecreatetruecolor($this->src_width / $ratio, $this->src_height / $ratio);
imagecopyresampled($dest_image, $this->src_image, 0, 0, 0, 0,
$this->src_width / $ratio,
$this->src_height / $ratio,
$this->src_width,
$this->src_height);
imagejpeg($dest_image, $filename.'.jpg', $quality);
imagedestroy($dest_image);
}
function SaveProportionateImageAllConcerti($filename, $quality, $width){
if ($this->src_width > $this->src_height && $width < $this->src_width){
$dest_width = $width;
$ratio = $this->src_width / $dest_width;
}
else if($width < $this->src_height){
$dest_width = $width;
$ratio = $this->src_height / $dest_width;
}
else{
$ratio=1;
$dest_width = $this->src_width;
}
$dest_image = imagecreatetruecolor($this->src_width / $ratio, $this->src_height / $ratio);
imagecopyresampled($dest_image, $this->src_image, 0, 0, 0, 0,
$this->src_width / $ratio,
$this->src_height / $ratio,
$this->src_width,
$this->src_height);
imagejpeg($dest_image, $filename, $quality);
imagedestroy($dest_image);
}
function Free(){
imagedestroy($this->src_image);
}
}
Codice PHP:
class FileUpload{
var $up_dir; //la directory temporanea in cui verrà uploadata l'img
var $filename; //il nome del file
var $new_filename; //il nuovo nome del file se vogliamo rinominarlo
function FileUpload($up_dir){
$this->up_dir = $up_dir;
}
function RenameFile($new_filename){
$this->new_filename = $new_filename;
}
function Upload($files){
if(!file_exists($this->up_dir))
die('
ERRORE: La directory non esiste! Contatta il [email="webmaster@novarawakeup.it"]webmaster[/email] e digli cosa ti è successo!
[url="javascript:history.go(-1)"]<-Torna indietro[/url]');
$this->filename = ($this->new_filename) ? $this->new_filename :$files['name'];
if(trim($files["name"]) == "")
die("
ERRORE: Non hai indicato il file da uploadare!
<a href=\"javascript:history.go(-1)\"><-Torna indietro</a>");
if(is_uploaded_file($files["tmp_name"])){
move_uploaded_file($files["tmp_name"],$this->up_dir."/".$this->filename)
or die("
ERRORE: Impossibile spostare il file; controlla l'esistenza o i permessi della directory! Contatta il <a href=\"mailto:webmaster@novarawakeup.it\">webmaster</a> e digli cosa ti è successo!
<a href=\"javascript:history.go(-1)\"><-Torna indietro</a>");
}else
die ("
ATTENZIONE: Problemi nell'upload del file ".$files["name"].". Contatta il <a href=\"mailto:webmaster@novarawakeup.it\">webmaster</a> e digli cosa ti è successo!
<a href=\"javascript:history.go(-1)\"><-Torna indietro</a>");
}
function DeleteFile(){
unlink($this->up_dir . '/' . $this->filename);
}
}
Nela classe file upload c'è questo controllo sul fatto di aver selezionato un file:
if(trim($files["name"]) == "")
die("
ERRORE: Non hai indicato il file da uploadare!
<a href=\"javascript:history.go(-1)\"><-Torna indietro</a>");
se lo tolgo mi da comunque un errore nell'upload!
Come modifico i controlli in modo da rendere l'upload facoltativo?
Grazie
Ste