#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Issue with multiple upload images


    Hello,

    I have a issue with multiple images upload. Basically, what I want to do is edit a working script that I have and and change it in order to run multiple upload.

    From the form I get the array containing the image data
    Code:
    <input name="fleImage[]" type="file" id="fleImage" class="box" multiple="multiple">
    Below I have 2 functions that process the whole.
    The first function: addProduct (). Note that I added the foreach that was not there before, but that does not work

    PHP Code:
    foreach($_FILES['fleImage']['tmp_name'] as $images ){ // <-- added

     
    $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); 

    header("Location: index.php?catId=$catId"); 

    the function uploadProductImage () that processes the image by a resizing and moving with the original one, in a default folder. I did not touch this function.
    PHP Code:
    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); } 
    What I can not understand is how to run the foreach to take advantage of the second function. Inserting data into the database works but not uploading images.

    Thanks for helping
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    please be a bit more specific when describing errors. "Doesn't work" doesn't mean anything. We could have guessed that there's something wrong, because otherwise you probably wouldn't be here. What we need to know is the exact problem. Do you get a blank page? An error message? Unexpected data? Did your CPU melt while you ran the script?

    Originally Posted by Aganju
    the function uploadProductImage () that processes the image by a resizing and moving with the original one, in a default folder. I did not touch this function.
    And that's the problem. If you look at the function, it's obviously made for a single file, not an array of files. You need to change that.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    please be a bit more specific when describing errors. "Doesn't work" doesn't mean anything.
    It is true you are right.
    When I run the script I have no errors. Simply return to index.php without uploading images
    And that's the problem. If you look at the function, it's obviously made for a single file, not an array of files. You need to change that.
    but it is called within the first function and inside the foreach ...
    PHP Code:
    foreach($_FILES['fleImage']['tmp_name'] as $images ){
     
    $images uploadProductImage('fleImage'SRV_ROOT 'images/product/'); 
    can not work?
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by Aganju
    but it is called within the first function and inside the foreach ...
    Yes, but you never pass the loop variable to the function. You just call the same function with the same arguments multiple times.

    And like I said: Look at the function. In the first few lines, you have
    PHP Code:
    trim($image['tmp_name']) 
    This obviously won't work if $image['tmp_name'] is an array rather than a single string.

    So there really is no way to avoid changing the function. For example, you could replace the "foreach" loop with a "for" loop and pass each index to the function. Then you can select the right entry for "tmp_name", "name" etc.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    Thank you for the explanation Jacques1, but I'm not an expert in php.
    I'm trying to learn and I can only do simple things. I can not understand how the script works. I mean, I know what I want to do but do not know how to get it.
    Could you help me? thanks

    Comments on this post

    • ptr2void disagrees : Translation: I copied and pasted and have no clue. How about learning PHP?

IMN logo majestic logo threadwatch logo seochat tools logo