Visualizzazione dei risultati da 1 a 6 su 6

Discussione: upload "facoltativo"

  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2002
    Messaggi
    298

    upload "facoltativo"

    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_image0000,
                
    $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_image0000,
                
    $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_image0000,
                
    $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

  2. #2
    prima di richiamare la classe che esegue l'upload, controlla se l'utente abbia realmente selezionato un file da uploadare. dovrebbe bastare un controllo come questo:

    Codice PHP:
    if (!empty($_FILES['userfile']['name'])) {
          
    // richiamo della classe di upload

    dove "userfile" è il nome del campo <input type="file"> del tuo form

  3. #3
    oppure fai il controllo direttamente nella classe upload ed esci nel caso $_FILES sia vuoto
    PHP geek
    web: HostManiak - Andyydev

  4. #4
    Utente di HTML.it
    Registrato dal
    Nov 2002
    Messaggi
    298
    umm..ma faccio già un controllo al momento della chiamata della calsse del tipo:

    if($_FILES['file']!="")

    ho provato anche if($_FILES['file']!=NULL) e if(!empty($_FILES['file']))

  5. #5
    la variabile $_FILES['file'] è un array, quindi i controlli che fai non sono corretti. fai il controllo sulla variabile $_FILES['file']['name'] che è una stringa contenente il nome del file che stai uploadando

  6. #6
    Utente di HTML.it
    Registrato dal
    Nov 2002
    Messaggi
    298
    yay!


    vero!funziona! Grazie mille!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.