SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    belfast
    Posts
    386
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File Upload script timing out ...

    Hey guys,

    bit of a problem with my file upload script timing out and complainging about:
    Maximum execution time of 30 seconds exceeded in /home2/drapers/public_html/vehicleAdd on line 184

    this line is if($photos_uploaded['size'][$counter] > 0)

    Heres what I'm at ...

    1. get 3 images from a page,
    2. submit them back to that page - checking there are 3 photos submitted
    3. checking that they are valid images
    4. if valid and all present add them to the db

    My problem is that the page itself takes !!!ages!!! to load, maybe my script isnt optomised enough?? Any thoughts.

    The comparison then of checking if the size of the photos_uploaded is greater than zero only compounds the problems.

    Any ideas on how to improve the efficiency? Why would it take so long to validate the line if($photos_uploaded['size'][$counter] > 0)

    PHP Code:
    <?php
    //close if
    else
    {

    $userID $_POST['userID'];
    $vehicleName addslashes($_POST['vehicleName']);
    $vehicleDescription addslashes($_POST['vehicleDescription']);
    $specialOffer $_POST['specialOffer'];
    $type $_POST['type'];

    if (!
    $vehicleName)
    {
        
    error('Vehicle Name can not be blank.  Please Enter the vehicles name.');
    }

    if (!
    $vehicleDescription)
    {
        
    error('Vehcile Description cannot be blank.  Please Enter some information about the vehicle.');
    }

    if (
    $type == '--')
    {
        
    error('You must select the type of the Vehicle.  Please return and select the type of the vehicle from the drop down menu.');
    }

        
    // initialization
        
    $result_final "";
        
    $counter 0;
        
        
    // List of our known photo types
        
    $known_photo_types = array( 
                            
    'image/pjpeg' => 'jpg',
                            
    'image/jpeg' => 'jpg',
                            
    'image/gif' => 'gif',
                            
    'image/bmp' => 'bmp',
                            
    'image/x-png' => 'png'
                        
    );
        
        
    // GD Function List
        
    $gd_function_suffix = array( 
                            
    'image/pjpeg' => 'JPEG',
                            
    'image/jpeg' => 'JPEG',
                            
    'image/gif' => 'GIF',
                            
    'image/bmp' => 'WBMP',
                            
    'image/x-png' => 'PNG'
                        
    );

        
    // Fetch the photo array sent by upload
        
    $photos_uploaded $_FILES['photoFilename'];
        
    //testing the value of the array
            
    print_r($photos_uploaded);
        
        
    // Fetch the photo caption array
        
    $photo_caption $_POST['photoDescription'];
        
        while( 
    $counter <= count($photos_uploaded) )
            {
                
                
    //*** THIS BAD BOY IS THE TROUBLE LINE
    if($photos_uploaded['size'][$counter] > 0)
                {       
                            if(!
    array_key_exists($photos_uploaded['name'][$counter]))
                    {
                        
    error('You must select three photos to upload.  Please go back and select 3 photos.');
                    }
                    if(!
    array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
                    {
                        
    $result_final .= "File ".($counter+1)." is not a photo<br />";
                    }
                    else
                    {
                                            
                        
    $vehicleName $vehicleName;
                        
    $picture1URL 0;
                        
    $picture1Tag $photo_caption['0'];
                        
    $picture2URL 0;
                        
    $picture2Tag $photo_caption['1'];
                        
    $picture3URL 0;
                        
    $picture3Tag $photo_caption['2'];
                        
    $vehicleDescription $vehicleDescription;
                        
    $specialOffer $specialOffer;
                        
    $type $type;
                        
                        
    $addQuery "INSERT INTO vehicles SET vehicleName = '$vehicleName', picture1URL = '$picture1URL', picture1Tag = '$picture1Tag', picture2URL = '$picture2URL', picture2Tag = '$picture2Tag', picture3URL = '$picture3URL', picture3Tag = '$picture3Tag', vehicleDescription = '$vehicleDescription', specialOffer = '$specialOffer', dateCreated = now(), type = '$type'";
                        
                        
    $add mysql_query($addQuery);
                        
                        if (!
    $add)
                            {
                                
    error('Could not add your vehicle at this time.  Please try again later');
                            }
        
                                
    $new_id mysql_insert_id();
                                
    $filetype $photos_uploaded['type'][$counter];
                                
    $extention $known_photo_types[$filetype];
                                
    $filename $new_id."."."$counter".".".$extention;
                
                                
    $filename addslashes($filename);
                                
    $new_id addslashes($new_id);
                                                            
                                
    //$updateQuery1 = "UPDATE vehicles SET picture1URL='$filename' WHERE vehicleID='$new_id'";
                                //$updateQuery2 = "UPDATE vehicles SET picture3URL='$filename' WHERE vehicleID='$new_id'";
                                //$updateQuery3 = "UPDATE vehicles SET picture3URL='$filename' WHERE vehicleID='$new_id'";
                                
                                //$updateQuery = mysql_query($updateQuery);
                                                    
                                    //if (!$updateQuery1||!$updateQuery2||!$updateQuery3)
                                    //    {
                                    //        error('Could not update the file name during this transaction.  Please try again later');
                                    //    }
                                
                                // Store the orignal file
                                //copy($photos_uploaded['tmp_name'][$counter+1], $images_dir."/".$filename);
                
                    
    }
                }
            }
        }
        
    ?>
    Thanks in advance ...

    Ronan
    Last edited by ronanmagee; Feb 10, 2005 at 10:42.

  2. #2
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    problem is:
    PHP Code:
    while( $counter <= count($photos_uploaded) ) 
    You don't increase $counter in the while loop, so it's infinite. Even the fastest script can't do an infinite loop under 30 seconds

  3. #3
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    belfast
    Posts
    386
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Daimaju
    problem is:
    PHP Code:
    while( $counter <= count($photos_uploaded) ) 
    You don't increase $counter in the while loop, so it's infinite. Even the fastest script can't do an infinite loop under 30 seconds
    Cheers Daimaju,

    Once i went home last nite I fugured that out, and I have to optomise my SQL a bit better.

    Should have known that about the infinite loop, after all my DB was filling up to 57MB - gulp -

    cheers,

    Ronan

  4. #4
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    belfast
    Posts
    386
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And as if by magic there is another problem ... its to do with my while loop and the mysql command $new_id = mysql_insert_id()

    I have 3 images to upload, they all relate to one individual vehicle, there for no 2 vehicles will have the images.

    Currently the script works fine in one respect, it pulls all the data from the form and validates the images and updates the image names in the table.

    Now my problem is that the line $new_id = mysql_insert_id() generates a new ID every time it loops for the while - then it adds a new row to the table instead of updating the current row. I cant for the life of me get it to update the current row on all 3 passes, so as all the images and info for one vehicle are in the same row.

    PHP Code:
    // initialization
        
    $counter 0;
        
        
    // List of our known photo types
        
    $known_photo_types = array( 
                            
    'image/pjpeg' => 'jpg',
                            
    'image/jpeg' => 'jpg',
                            
    'image/gif' => 'gif',
                            
    'image/bmp' => 'bmp',
                            
    'image/x-png' => 'png'
                        
    );
        
        
    // GD Function List
        
    $gd_function_suffix = array( 
                            
    'image/pjpeg' => 'JPEG',
                            
    'image/jpeg' => 'JPEG',
                            
    'image/gif' => 'GIF',
                            
    'image/bmp' => 'WBMP',
                            
    'image/x-png' => 'PNG'
                        
    );

        
    // Fetch the photo array sent by upload
        
    $photos_uploaded $_FILES['photoFilename'];
        
        
    // Fetch the photo caption array
        
    $photo_caption $_POST['photoDescription'];
        
        while( 
    $counter <= count($photos_uploaded['name']) )
            {
                if(
    $photos_uploaded['size'][$counter] > 0)
                {
                    if(!
    array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
                    {
                        
    error('File '.($counter+1).' is not a valid image file.  Please go back and select a valid image file.');
                        exit();
                    }
                    else
                    {
                        
    $vehicleName $vehicleName;
                        
    $pictureURL "picture".($counter+1)."URL";
                        
    $defaultURLValue "0";
                        
    $pictureTagFieldName "picture". ($counter+1) ."Tag";
                        
    $pictureTag $photo_caption[$counter];
                        
    $vehicleDescription $vehicleDescription;
                        
    $specialOffer $specialOffer;
                        
    $type $type;
                        
                        
    $addQuery "INSERT INTO vehicles SET vehicleName = '$vehicleName', $pictureURL = '$defaultURLValue', $pictureTagFieldName = '$pictureTag', vehicleDescription = '$vehicleDescription', specialOffer = '$specialOffer', dateCreated = now(), type = '$type'";
                        
                        
    $add mysql_query($addQuery);
                        
                        if (!
    $add)
                            {
                                
    error('Could not add your vehicle at this time.  Please try again later');
                            }
                                
    //**THIS BAD BOY INSIDE THE WHILE LOOP IS CAUSING THE PROBLEMS, AND I CANT MOVE IT OUTSIDE THE WHILE LOOPS AS IT DOENST GENERATE THE LAST INSERT ID **//
                                                           
    $new_id mysql_insert_id();
                                echo 
    $new_id;
                                
    $filetype $photos_uploaded['type'][$counter];
                                
    $extention $known_photo_types[$filetype];
                                
    $filename $new_id.".Picture".($counter+1).".".$extention;
                
                                
    $filename addslashes($filename);
                                
    $new_id addslashes($new_id);
                                                            
                                
    $updateFieldName "picture". ($counter+1) ."URL";
                                
                                
    $updateQuery "UPDATE vehicles SET $updateFieldName='$filename' WHERE vehicleID='$new_id'";
                            
                                
    $updateQuery mysql_query($updateQuery);
                                                    
                                    if (!
    $updateQuery)
                                        {
                                            
    error('Could not update the file name during this transaction.  Please try again later');
                                        }
                                
                                
    // Store the orignal file
                                
    copy($photos_uploaded['tmp_name'][$counter], $photos_dir.$filename);
                
                    }
                }
            
    $counter++;
            }
        }
        
    ?> 

  5. #5
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why don't you just add the filename to your insert-query ? As you have all the information to create $updateFieldName and $filename before the insert-query?

  6. #6
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    belfast
    Posts
    386
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see where your coming from - but I cant actually do that. Once the files are uploaded i.e. just before the insert step they are assigned temp file names. I populate the DB with a temp file name of '0'. Once all the data is up there then I move the file from the tmp dir and rename it. Once this rename is done I then update the db.

    I think that my main problem is the use of the while loop. I might have to get rid of this and set the variables one by one instead of looping through.

    I'll let you know how I get on.


Bookmarks

Posting Permissions

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