Visualizzazione dei risultati da 1 a 3 su 3

Discussione: upload immagine

  1. #1

    upload immagine

    non riesco proprio a fare l' upload di un' immagine.
    mi da questo errore:
    failed to open stream: No such file or directory in .....

    uso questo script ......

    la prima condizione non si verifica.

    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    echo ("immagine uploaded");

    }
    $tmp_name = $_FILES["userfile"]["tmp_name"][$key];
    $name = $_FILES["userfile"]["name"][$key];
    $size = $_FILES["userfile"]["size"][$key];

    move_uploaded_file($tmp_name, $currentDir.$uploadDir."/img/$name");


    boh, è la prima volta che spatacco con l' upload e sinceramente non so dove cercare la soluzione.....

  2. #2
    Utente di HTML.it L'avatar di polinet
    Registrato dal
    Nov 2000
    Messaggi
    993
    se salvi il percorso della immagine nel DB potresti fare cosi:
    <form action="metti_la_foto.php" method="post" enctype="multipart/form-data" name="form1">
    Foto n° 1
    <input name="foto" type="file" id="foto">
    etc.....
    </form>
    metti_la_foto.php
    Codice PHP:
    <?php
    include('img.class.php');
    if (
    $_FILE['foto'] == "")echo "non hai inserito la foto";
    if(
    $_FILES['foto']){
        
    $immagine=$_FILES['foto']['tmp_name'];
        
    $I=new IMG($immagine);
        
    $type=$I->getType();
        if(
    $type!="jpeg"&&$type!="png") echo "Il formato della foto non è corretto.
    "
    ;
        else {
    $type=($type=="jpeg")?"jpg":$type;
            
    $nome=$_FILES['foto']['name'];
            if(
    file_exists(str_replace(".","_p","img/".$nome))) echo "Attenzione un'immagine con questo nome esiste già!!! Rinomina l'immagine.
    "
    ;
            else { 
                
    // questa è la grande
                
    $I=new IMG($immagine);
                
    $I->resize_max(420,600);
                
    //riduco l'immagine fino a quando sta in un retangolo con dimensioni 600,600 (quadrato in questo caso)
                
    $nomebis_1=str_replace(".","_g.",$nome);
                
    $I->save("img/$nomebis_1");
                
                
    // questa è la media
                
    $I=new IMG($immagine);
                
    $I->centro_proporzionato(200,150); 
                
    //creo un'immagine 100x100 recuperando il quadrato massimo proporzionato all'interno della immagine originale
                
    $nomebis_2=str_replace(".","_m.",$nome);
                
    $I->save("img/$nomebis_2");
                echo 
    "Immagine 1 creata.
    "
    ;

                
    // questa è la piccola
                
    $I=new IMG($immagine);
                
    $I->centro_proporzionato_2(120,90); 
                
    //creo un'immagine 100x100 recuperando il quadrato massimo proporzionato all'interno della immagine originale
                
    $nomebis_p=str_replace(".","_p.",$nome);
                
    $I->save("img/$nomebis_p"); // qui da definire la variabile
                //echo "Immagine 1 creata.
    ";
            }
        }
        
    }
    Ora hai bisogno della classe:
    Te la passo tutta....
    Codice PHP:
    <?php
    class IMG{
        
    //dichiaro le VARIABILI GLOBALI
        
    var $_img//variabile globale dell'immmagine
        
    var $_x//variabile globale dimensione width img
        
    var $_y//variabile globale dimensione height img
        
    var $_type//variabile globale tipo img
        
    var $_width;  //dimensione w immagine finale
        
    var $_height;  //dimensione h immagine finale
        
    var $_versione// versione dell'immagine orizzontale o verticale
        
        //dichiaro il COTRUTTORE    
        
    function IMG($file=""){
            if(
    $file!=""){
                
    $this->_x=0;
                
    $this->_y=0;
                
    $this->info($file);
                
    $this->load($file);
            }
        }

        
    // definisco la funzione "load"
        
    function load($file){
            if(
    $file=="") return FALSE;
            if(
    $this->_type=="") return FALSE;
            else {
                switch(
    $this->_type){
                    case 
    "gif":
                        return 
    FALSE//questo perché le img GID con la GD2 non funzionano
                    
    break;

                    case 
    "jpeg":
                        
    $this->_img=imagecreatefromjpeg($file);
                    break;

                    case 
    "png":
                        
    $this->_img=imagecreatefrompng($file);
                    break;

                    case 
    "wbmp":
                        
    $this->_img=imagecreatefromwbmp($file);
                    break;

                    default:
                        return 
    FALSE;
                    break;
                }
                return 
    TRUE;
            }
        }
        
        
    //ritorno valore width
        
    function getWidth(){
            return 
    $this->_x;
        }
        
        
    //ritorno valore height
        
    function getHeight(){
            return 
    $this->_y;
        }
        
        
    //ritorno valore type
        
    function getType(){
            return 
    $this->_type;
        }

        
    //ritorno versione type
        
    function getVersione(){
            if(
    $this->_x $this->_y$this->_versione="o"//orizzontale
            
    else $this->_versione "v"//verticale
            
    return $this->_versione;
        }
            
        
    //dichiarazione delle caratteristiche dell'img
        
    function info($file){
            if(
    $file=="") return FALSE;
            
    $info=getimagesize($file);
            if(
    $info==FALSE) return FALSE;
            else {
                switch(
    $info[2]){
                    case 
    1:
                        
    $this->_type="gif";
                    break;

                    case 
    2:
                        
    $this->_type="jpeg";
                    break;

                    case 
    3:
                        
    $this->_type="png";
                    break;

                    case 
    6:
                        
    $this->_type="wbmp";
                    break;

                    default:
                        return 
    FALSE;
                    break;
                }
                
    $this->_x=$info[0]; //assegno il valore a x e y
                
    $this->_y=$info[1];
                return 
    TRUE;
            }
        }
        
        
    /*----->  dichiarazione funzione "RESIZE" 
            ridimensiona l'immagine dentro una quadro con dimensioni 
            passate e inserisce il colore passato nello spazio rimanente
        */
        
    function resize($width="",$height="",$r=255,$g=255,$b=255){
            if(
    $this->_x==0||$this->_y==0) return FALSE;
            else {
                if(
    $this->_x>$width||$this->_y>$height){
                    
    $scale min($width/$this->_x$height/$this->_y);
                    
    $img_width = (int)($this->_x*$scale);
                    
    $img_height = (int)($this->_y*$scale);
                    
    $deltaw = (int)(($width $img_width)/2);
                    
    $deltah = (int)(($height $img_height)/2);
                    
    $newimg=imagecreatetruecolor($width,$height); //imagecreatetruecolor GD2
                    
    $color imagecolorallocate ($this->_img,$r,$g,$b);
                    
    imagefill($newimg,0,0,$color);
                    
    $ok=imagecopyresampled($newimg,$this->_img,$deltaw,$deltah,0,0,$img_width,$img_height,$this->_x,$this->_y); //imagecopyresampled GD2
                    
    $this->_x=$width;
                    
    $this->_y=$height;
                    if(
    $ok==FALSE) return FALSE;
                    else {
                        
    $this->_img=$newimg;
                        return 
    TRUE;
                    }
                } else {
                    
    $deltaw = (int)(($width $this->_x)/2);
                    
    $deltah = (int)(($height $this->_y)/2);
                    
    $newimg=imagecreatetruecolor($width,$height); //imagecreatetruecolor GD2
                    
    $color imagecolorallocate ($this->_img,$r,$g,$b);
                    
    imagefill($newimg,0,0,$color);
                    
    $ok=imagecopyresampled($newimg,$this->_img,$deltaw,$deltah,0,0,$this->_x,$this->_y,$this->_x,$this->_y); //imagecopyresampled GD2
                    
    $this->_x=$width;
                    
    $this->_y=$height;
                    if(
    $ok==FALSE) return FALSE;
                    else {
                        
    $this->_img=$newimg;
                        return 
    TRUE;
                    }
                }
            }
        }
        
        
    /*------>dichiarazione funzione "RESIZE MAX"
            ridimensiona le immagini in modo che stiano 
            all'interno di un'area definita
        */
        
    function resize_max($max_w="",$max_h=""){
            if(
    $this->_x==0||$this->_y==0) return FALSE;
            else {
                if(
    $this->_x>$max_w||$this->_y>$max_h){
                    
    $scale min($max_w/$this->_x$max_h/$this->_y);
                    
    $img_width = (int)($this->_x*$scale);
                    
    $img_height = (int)($this->_y*$scale);
                    
    $newimg=imagecreatetruecolor($img_width,$img_height); //imagecreatetruecolor GD2
                    
    $ok=imagecopyresampled($newimg,$this->_img,0,0,0,0,$img_width,$img_height,$this->_x,$this->_y); //imagecopyresampled GD2
                    
    $this->_x=$img_width;
                    
    $this->_y=$img_height;
                    if(
    $ok==FALSE) return FALSE;
                    else {
                        
    $this->_img=$newimg;
                        return 
    TRUE;
                    }
                } else {
                    
    $newimg=imagecreatetruecolor($this->_x,$this->_y); //imagecreatetruecolor GD2
                    
    $ok=imagecopyresampled($newimg,$this->_img,0,0,0,0,$this->_x,$this->_y,$this->_x,$this->_y); //imagecopyresampled GD2
                    
    if($ok==FALSE) return FALSE;
                    else {
                        
    $this->_img=$newimg;
                        return 
    TRUE;
                    }
                }
            }
        }
        
        
    /*-----> dichiarazione funzione "IMG CENTRALE"
            prende l'area centrale dell'immagine di dimensioni definite 
            e la ridimensiona alle nuove dimensioni che vengono passate 
            GD2 nuove .... :o)
        */
        
    function centro($width="",$height="",$por_w="",$por_h=""){
            if(
    $this->_x==0||$this->_y==0) return FALSE;
            else {
                
    $x=($this->_x-$por_w)/2;
                
    $y=($this->_y-$por_h)/2;
                
    $newimg=imagecreatetruecolor($width,$height);
                
    $ok=imagecopyresampled($newimg,$this->_img,0,0,$x,$y,$width,$height,$por_w,$por_h); //imagecopyresampled GD2
                
    $this->_x=$width;
                
    $this->_y=$height;
                if(
    $ok==FALSE) return FALSE;
                else {
                    
    $this->_img=$newimg;
                    return 
    TRUE;
                }
            }
        }
        
        
    /*-----> dichiarazione funzione "IMG CENTRALE"
            prende l'area centrale dell'immagine di dimensioni definite 
            e la ridimensiona alle nuove dimensioni che vengono passate 
            GD2 nuove .... :o)
        */
        
    function centro_proporzionato($width="",$height=""){
            if(
    $this->_x==0||$this->_y==0) return FALSE;
            else {
                
    $scale=min($this->_x/$width,$this->_y/$height);
                
    $por_w=$width*$scale;
                
    $por_h=$height*$scale;
                
    $x=($this->_x-$por_w)/2;
                
    $y=($this->_y-$por_h)/2;
                
    $newimg=imagecreatetruecolor($width,$height);
                
    $ok=imagecopyresampled($newimg,$this->_img,0,0,$x,$y,$width,$height,$por_w,$por_h); //imagecopyresampled GD2
                
    $this->_x=$width;
                
    $this->_y=$height;
                if(
    $ok==FALSE) return FALSE;
                else {
                    
    $this->_img=$newimg;
                    return 
    TRUE;
                }
            }
        }

        
    /*-----> dichiarazione funzione "IMG CENTRALE 2"
            prende l'area centrale dell'immagine di dimensioni definite 
            e la ridimensiona alle nuove dimensioni che vengono passate 
            GD2 nuove .... :o)
        */
        
    function centro_proporzionato_2($width="",$height=""){
            if(
    $this->_x==0||$this->_y==0) return FALSE;
            else {
                
    $scale=min($this->_x/$width,$this->_y/$height);
                
    $por_w=$width*$scale;
                
    $por_h=$height*$scale;
                
    $x=($this->_x-$por_w)/2;
                
    $y=($this->_y-$por_h)/2;
                
    $newimg=imagecreatetruecolor($width,$height);
                
    $ok=imagecopyresampled($newimg,$this->_img,0,0,$x,$y,$width,$height,$por_w,$por_h); //imagecopyresampled GD2
                
    $this->_x=$width;
                
    $this->_y=$height;
                if(
    $ok==FALSE) return FALSE;
                else {
                    
    $this->_img=$newimg;
                    return 
    TRUE;
                }
            }
        }
        
        
        
    /*-----> dichiarazione funzione "IMG CENTRALE"
            prende l'area centrale dell'immagine di dimensioni definite 
            e la ridimensiona alle nuove dimensioni che vengono passate 
            GD2 vecchia .... :o(
        */
        
    function centro2($width="",$height="",$por_w="",$por_h=""){
            if(
    $this->_x==0||$this->_y==0) return FALSE;
            else {
                
    $x=($this->_x-$por_w)/2;
                
    $y=($this->_y-$por_h)/2;
                
    $newimg=imagecreatetruecolor($width,$height);
                
    $tmp_img=imagecreatetruecolor($por_w,$por_h);
                
                
    imagecopy ($tmp_img$this->_img,0,0,$x,$y,$por_w,$por_h);
                
    $ok=imagecopyresampled ($newimg,$tmp_img,0,0,0,0,$width,$height,$por_w,$por_h);
                
                
    $this->_x=$width;
                
    $this->_y=$height;
                if(
    $ok==FALSE) return FALSE;
                else {
                    
    $this->_img=$newimg;
                    return 
    TRUE;
                }
            }
        }

        
        
    /*-----> dichiarazione funzione "RESIZE STRECH"
            ridimensiona alle dimensioni stabilite strecciando l'immagine (senza mantenere il rapporto originale)
        */
        
    function resize_strech($width="",$height=""){
            if(
    $this->_x==0||$this->_y==0) return FALSE;
            else {
                
    $this->_width=$width;
                
    $this->_height=$height;
                
    $newimg=imagecreatetruecolor($this->_width,$this->_height);
                
    $ok=imagecopyresampled($newimg,$this->_img,0,0,0,0,$this->_width,$this->_height,$this->_x,$this->_y);
                
    $this->_x=$this->_width;
                
    $this->_y=$this->_height;
                if(
    $ok==FALSE) return FALSE;
                else {
                    
    $this->_img=$newimg;
                    return 
    TRUE;
                }
            }
        }
        
        
    /* -----> dichiarazione funzione "ADD_IMG"
            aggiunge una immagine sopra l'originale
        */
        
    function add_img($x,$y,$d,$img,$width,$height){
            if(
    $d=="sopra"){
                
    $pawn  imagecreatefrompng ($img);
                
    $ok=imagecopy ($this->_img$pawn$x$y00$width$height);
            } else if(
    $d=="sotto"){
                
    $pawn  imagecreatefrompng ($img);
                
    $ok=imagecopy ($pawn$this->_img$x$y00$width$height);
                
    $this->_img=$pawn;
            }
            
    //$this->_x=($this->_x>$width)?$this->_x:$width;
            //$this->_y=($this->_y>$height)?$this->_y:$height;
            
    if($ok==FALSE) return FALSE;
            else {
                return 
    TRUE;
            }
        }
        
        
    /* -----> dichiarazione funzione "SAVE"
            salva l'immagine con il nome passato
        */
        
    function save($nome,$qualita="60"){
            if(
    $this->_x==0||$this->_y==0) return FALSE;
            else {
                
    $func="image".$this->_type;
                
    $ok=call_user_func($func,$this->_img,$nome);
                if(
    $ok!=FALSE) return FALSE;
                else {
                    return 
    TRUE;
                }
            }
        }

        
            
        
    /*---> dichiarazione funzione "DISPLAY"
            stampa l'immagine sullo schermo
        */
        
    function display($qualita="60"){
            if(
    $this->_x==0||$this->_y==0) return FALSE;
            else {
                
    header("Content-Type: image/".$this->_type.""); 
                
    $func="image".$this->_type;
                
    $ok=call_user_func($func,$this->_img,"",$qualita);
                if(
    $ok==FALSE) return FALSE;
                else {
                    return 
    TRUE;
                }
            }
        }
    }
    ?>
    Dopo di che salvi il tutto nel DB.
    facci sapere!!!!

    Edit: i nomi delle variabili sono: $nome_bis etc.......
    Edit: i file vanno nella cartella img offcourse CHMOD777
    ¿Hasta la pasta?

  3. #3
    ciao,
    grazie tante del tuo codice. Però una cosa devo capire, l' errore che mi da a cosa è dovuto ?

    failed to open stream: No such file or directory in .....


    a me sembra anche che non mi faccia l'upload sul server......

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 © 2025 vBulletin Solutions, Inc. All rights reserved.