www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Upload Problem

Hybrid View

  1. #1
    Join Date
    May 2009
    Location
    Hampshire, UK
    Posts
    277

    resolved [RESOLVED] Upload Problem

    Hi,

    I seem to be having a problem with my upload script. The script is meant to loop through the code 8 times, each time it checks whether there is an image to be uploaded, if there is then it determines what to call it and moves it to a directory.

    This a snippet of the html code it loops through:
    HTML Code:
    <td>Gallery Image Source 1:</td>
    <td><input name="image1" onchange="document.getElementById('ignoreGallery').checked=false;" type="file" />
    <input type="hidden" name="deleteImage1" id="deleteImage1" />
    <input type="hidden" name="oldImage1" value="" /></td>
    Obviously each different snippet of html has different identities such as the next file input would be called "image2"

    The first bit determines whether there is an old image that is going to be replaced

    Then it tests that form file field if there is an image ready to be uploaded

    The next bit loops through the directory where the images are and sees if the oldImage exists and if so deletes it

    The the final bit of code does the upload with a certain filename.

    Here is my php code:
    PHP Code:
    for($i=0;$i<8;$i++){
                    
                    
    $oldImage $_POST['oldImage'.$i];
                    if(empty(
    $oldImage)){$oldImage=$i;}
                                    
                    if((!empty(
    $_FILES['image'.$i]['tmp_name']) && preg_match('/(?:jpe?g|gif|png)$/i'$_FILES['image'.$i]['name'])) || $_POST['deleteImage'.$i]==1){
                                            
                        if (
    $handle opendir($uploadsDirectory)) {
                            while (
    false !== ($file readdir($handle))) {
                                if (
    $file === $oldImage) {
                                    
    $photoExists $file;
                                }
                            }
                            
    closedir($handle);
                        }
                        
                        if(
    $photoExists){
                            
    unlink($uploadsDirectory.$photoExists);
                        } 
                    
                        
    $fieldname 'image'.$i;
                        if(
    is_uploaded_file($_FILES[$fieldname]['tmp_name'])) {
                            
    $oldImageName explode('.'$oldImage);
                            
    $extension end(explode("."$_FILES[$fieldname]['name']));                        
                            while(
    file_exists($uploadFilename $uploadsDirectory.$oldImageName[0].'.'.$extension)){ 
                                
    $oldImageName[0]++;         
                            }                        
                            
    move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename);
                            
    $sessionVars['gallery'][$i]=$oldImageName[0].'.'.$extension;
                        }        
                                            
                    }
                    
                } 
    The main problem is that the script sometimes works and sometimes doesn't depending on whether there has already been an uploaded image. The weird thing is that I do not get any errors. Simply $_FILES['image'.$i]['tmp_name'] does not find any values, apparently there is no image to upload although I know there is.

    Any help much appreciated,

    Thanks in Advance,

    Ben

  2. #2
    Join Date
    Nov 2008
    Posts
    2,477
    Check $_FILES['image'.$i]['error'] to see if there was an error.

    You might also want to consider using arrays (images[]) rather than inputs names like image1, image2 etc since you can then use foreach and handle a variable amount of uploads rather than doing 8 no matter what.

  3. #3
    Join Date
    May 2009
    Location
    Hampshire, UK
    Posts
    277
    I solved it by fiddling with it for a bit. Turned out it was just a small problem with an array.

    Just one last quick question though, if I want to consider using arrays rather than input names like image1 and image2, how would I go about it?

  4. #4
    Join Date
    Nov 2008
    Posts
    2,477
    Name your form elements accordingly:

    HTML Code:
    <input type="file" name="images[]" />
    <input type="file" name="images[]" />
    <input type="file" name="images[]" />
    PHP Code:
    foreach ($_FILES['images'] as $key => $image) {
        if (
    $image['error'] == UPLOAD_ERR_OK) {
            echo 
    "File $key uploaded successfully";
        }


  5. #5
    Join Date
    May 2009
    Location
    Hampshire, UK
    Posts
    277
    Brilliant, I will give that a try. Thanks for your time

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles