Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 12
  1. #1
    Utente bannato
    Registrato dal
    Dec 2010
    Messaggi
    297

    upload immagini in una cartella, con nome nel db

    Buongiorno a tutti

    vorrei far fare l'upload di immagini il quale:

    L'immagini viene salvata in una directory;
    nel db viene salvato il nome o il link dell'immagine;

    Io ho capito come fare per salvare tutta l'img nel db, ma a salvarla in una cartella e poi richiamarla dal nome del db non l'ho capito, qualcuno puo' aiutarmi?
    ciao a tutti

  2. #2
    Utente di HTML.it L'avatar di bstefano79
    Registrato dal
    Feb 2004
    Messaggi
    2,520
    fai l'upload in una cartella, salvi il nome e percorso in una tabella mysql che poi potrai recuperare per poter mettere nell'attributo src dell'immagine

  3. #3
    Utente bannato
    Registrato dal
    Dec 2010
    Messaggi
    297
    ad esempio ho trovato questa classe qui sul forum ma non ho capito come farla funzionare....


    come faccio a fare la chiamata alla classe Upload


    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






    Codice 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 ""$PostName =""$PostKey "")  
            {  
                
    $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;  
                 } 
            }  


  4. #4
    Utente di HTML.it L'avatar di bstefano79
    Registrato dal
    Feb 2004
    Messaggi
    2,520
    in upload.php farai

    Codice PHP:
    //recuperi i dati della form
    $uploadist=new Upload($path$nomedelfile /*etc etc*/);
    $uploadist->SaveUploadFile();
    //etc etc 

  5. #5
    Utente bannato
    Registrato dal
    Dec 2010
    Messaggi
    297
    ok mi dice file caricato correttamente

    pero' non vedo creata nessuna cartella sul serve, non capisco dove l'ha salvata

  6. #6
    Utente di HTML.it L'avatar di bstefano79
    Registrato dal
    Feb 2004
    Messaggi
    2,520
    non crea nessuna cartella quella classe, te lo salva in una cartella esistente che specifici come primo parametro del costruttore

  7. #7
    Utente bannato
    Registrato dal
    Dec 2010
    Messaggi
    297
    ho fatto una ricerca nel server non cè proprio quel file che ho caricato

    non me l'ha salvata da nessuna parte

  8. #8
    Utente bannato
    Registrato dal
    Dec 2010
    Messaggi
    297
    ho impostato il nome della cartella

    Codice PHP:
            public function __construct($path$name$overwrite TRUE$extensions "/upload/"$PostName ="control"$PostKey "upfile"

    ho creato la cartelal sul server ma niente non mi uppa niente

  9. #9
    Utente di HTML.it L'avatar di bstefano79
    Registrato dal
    Feb 2004
    Messaggi
    2,520
    posta il codice (quello dove usi la classe) che gli do un occhio

  10. #10
    Utente bannato
    Registrato dal
    Dec 2010
    Messaggi
    297
    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;  
                 } 
            }  
    }
    ?>

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.