Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    problema con upload immagini

    ciao,

    ho questo script che gestisce l'inserimento di prodotti nel db mysql.
    Riceve quindi i dati dal form, inserisce i dati nel db, prende l'immagine, fa un resize per l'anteprima e la copia nella cartella specificata insieme a quella originale.

    Adesso voglio utilizzare html5 che mi permette di selezionare più immagini contemporaneamente :
    codice:
    <input name="fleImage[]" type="file" id="fleImage" class="box" multiple/>
    Quindi ho modificato lo script in questo modo:
    Codice PHP:
    function addProduct() { 
    $catId $_POST['cboCategory']; 
    $name $_POST['txtName']; 
    $description $_POST['mtxDescription']; 
    $description nl2br(htmlspecialchars($descriptionENT_QUOTES));

    foreach(
    $_FILES['fleImage']['tmp_name'] as $key => $tmp_name ){ 
    $file_name $key.$_FILES['fleImage']['name'][$key]; 
     
    $images uploadProductImage('fleImage'SRV_ROOT 'images/product/'); 

    $mainImage $images['image']; 
    $thumbnail $images['thumbnail']; 

    $query="INSERT into tbl_images (cat_id, file_name, pd_image, pd_thumbnail) VALUES('$catId','$file_name','$mainImage','$thumbnail') "
    $result dbQuery($query); 



    $sql "INSERT INTO tbl_product (cat_id, pd_name, pd_description, pd_date) VALUES ('$catId', '$name', '$description', NOW())"
    $result dbQuery($sql); 
    header("Location: index.php?catId=$catId"); } 
    mentre la funzione che esegue il resize e l'upload delle immagini è questo:

    Codice PHP:
    function uploadProductImage($inputName$uploadDir) { 

    $image $_FILES[$inputName]; 
    $imagePath ''$thumbnailPath ''// if a file is given 

    if (trim($image['tmp_name']) != '') { 
    $ext substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']]; // generate a random new file name to avoid name conflict 

    $imagePath md5(rand() * time()) . ".$ext";
     
    list(
    $width$height$type$attr) = getimagesize($image['tmp_name']); // make sure the image width does not exceed the // maximum allowed width 

    if (LIMIT_PRODUCT_WIDTH && $width MAX_PRODUCT_IMAGE_WIDTH) { 
    $result createThumbnail($image['tmp_name'], $uploadDir $imagePathMAX_PRODUCT_IMAGE_WIDTH); 

    $imagePath $result

    } else { 
    $result move_uploaded_file($image['tmp_name'], $uploadDir $imagePath); 

    if (
    $result) { // create thumbnail 

    $thumbnailPath md5(rand() * time()) . ".$ext"

    $result createThumbnail($uploadDir $imagePath$uploadDir $thumbnailPathTHUMBNAIL_WIDTH); // create thumbnail failed, delete the image 

    if (!$result) { 
    unlink($uploadDir $imagePath); 
    $imagePath $thumbnailPath ''

    } else {
     
    $thumbnailPath $result

    } else { 
    // the product cannot be upload / resized 
    $imagePath $thumbnailPath ''


    } return array(
    'image' => $imagePath'thumbnail' => $thumbnailPath); } 
    Rispetto allo script originale funzionante, ho aggiunto solo il foreach nella funzione addProduct() e una query, mentre la seconda funzione uploadProductImage non l'ho toccata.
    Il problema: mi carica i dati nel DB ma non fa ne il resize ne l'upload delle immagini.

    C'è qualcosa che mi sfugge??
    Grazie!

  2. #2

  3. #3
    Utente di HTML.it L'avatar di Ranma2
    Registrato dal
    Mar 2003
    Messaggi
    2,648
    Non ho letto tutto, ma vedendo che hai posto nel input fleImage[] devi fare il foreach di solo $_FILES['fleImage']

  4. #4
    ciao,

    si quello lo avevo fatto


    codice:
    foreach($_FILES['fleImage'] as $image )
    ma non ho capito come collegare il foreach con resto delle funzioni. Cioè come collegare la funzione addProduct() con la funzione uploadProductImage($inputName, $uploadDir) che è quella che esegue il resize + upload delle immagini

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.