Visualizzazione dei risultati da 1 a 8 su 8

Discussione: Upload immagine

  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    126

    Upload immagine

    Buondì gente.
    Sto realizzando il sito del team di football americano nel quale gioco.
    Come ogni "americanata" vorremmo realizzare le schede giocatore, con immagine, dati e magari una piccola gallery dedicata.

    Per la gestione dei dati personali attraverso form, nessun problema. Diverso invece per l'upload dell'immagine.
    In giro ho trovato questo script:
    Codice PHP:
    <?php
    //define a maxim size for the uploaded images in Kb
     
    define ("MAX_SIZE","100"); 

    //This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
     
    function getExtension($str) {
             
    $i strrpos($str,".");
             if (!
    $i) { return ""; }
             
    $l strlen($str) - $i;
             
    $ext substr($str,$i+1,$l);
             return 
    $ext;
     }

    //This variable is used as a flag. The value is initialized with 0 (meaning no error  found)  
    //and it will be changed to 1 if an errro occures.  
    //If the error occures the file will not be uploaded.
     
    $errors=0;
    //checks if the form has been submitted
     
    if(isset($_POST['Submit'])) 
     {
         
    //reads the name of the file the user submitted for uploading
         
    $image=$_FILES['image']['name'];
         
    //if it is not empty
         
    if ($image
         {
         
    //get the original name of the file from the clients machine
             
    $filename stripslashes($_FILES['image']['name']);
         
    //get the extension of the file in a lower case format
              
    $extension getExtension($filename);
             
    $extension strtolower($extension);
         
    //if it is not a known extension, we will suppose it is an error and will not  upload the file,  
        //otherwise we will do more tests
     
    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
             {
            
    //print error message
                 
    echo '<h1>Unknown extension!</h1>';
                 
    $errors=1;
             }
             else
             {
    //get the size of the image in bytes
     //$_FILES['image']['tmp_name'] is the temporary filename of the file
     //in which the uploaded file was stored on the server
     
    $size=filesize($_FILES['image']['tmp_name']);

    //compare the size with the maxim size we defined and print error if bigger
    if ($size MAX_SIZE*1024)
    {
        echo 
    '<h1>You have exceeded the size limit!</h1>';
        
    $errors=1;
    }

    //we will give an unique name, for example the time in unix time format
    $image_name=time().'.'.$extension;
    //the new name will be containing the full path where will be stored (images folder)
    $newname="images/".$image_name;
    //we verify if the image has been uploaded, and print error instead
    $copied copy($_FILES['image']['tmp_name'], $newname);
    if (!
    $copied
    {
        echo 
    '<h1>Copy unsuccessfull!</h1>';
        
    $errors=1;
    }}}}

    //If no errors registred, print the success message
     
    if(isset($_POST['Submit']) && !$errors
     {
         echo 
    "<h1>File Uploaded Successfully! Try again!</h1>";
     }

     
    ?>
    Usandolo carica correttamente l'immagine nella cartella, solo che vorrei rinominare l'immagine con l'ID del giocatore appena inserito.

    Qualche suggerimento?

    Grazie a tutti

  2. #2
    Utente di HTML.it L'avatar di las
    Registrato dal
    Apr 2002
    Messaggi
    1,221
    Codice PHP:
    //we will give an unique name, for example the time in unix time format
    $image_name=$idDelGiocatore.'TuoTestoCheVuoiAggiungere.'.$extension;
    //the new name will be containing the full path where will be stored (images folder)
    $newname="images/".$image_name;
    //we verify if the image has been uploaded, and print error instead 
    Il calcolatore è straordinariamente veloce, accurato e stupido.
    L'uomo è incredibilmente lento, impreciso e creativo.
    L'insieme dei due costituisce una forza incalcolabile.
    (Albert Einstein)

  3. #3
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    126
    Originariamente inviato da las
    Codice PHP:
    //we will give an unique name, for example the time in unix time format
    $image_name=$idDelGiocatore.'TuoTestoCheVuoiAggiungere.'.$extension
    Questo vale anche se non conosco l'ID che verrà assegnato al giocatore che sto inserendo?

  4. #4
    Prima inserisci il giocatore. Se l'inserimento va a buon fine, prendi l'id inserito e lo usi per nominare l'immagine... altrimenti salvi prima l'immagine dandogli un riferimento univoco (timestamp+underscore+un 5/6 numeri random basta e avanza) e quando salvi il giocatore ti salvi anche il nome che hai dato alla foto
    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

  5. #5
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    126
    Questo mi fa supporre che non è possibile fare tutto insieme...o sbaglio?

  6. #6
    Originariamente inviato da BullFrog
    Questo mi fa supporre che non è possibile fare tutto insieme...o sbaglio?
    se per "tutto insieme" intendi nella stessa pagina php, si...altrimenti non so cosa intendi
    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

  7. #7
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    126
    Si, intendo nella stessa pagina

  8. #8
    Originariamente inviato da BullFrog
    Si, intendo nella stessa pagina

    certo che si può fare nella stessa pagina...prima salvi l'immagine sul server e poi fai la query per l'insert del giocatore (o viceversa, è uguale)
    IP-PBX management: http://www.easypbx.it

    Old account: 2126 messages
    Oldest account: 3559 messages

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.