Results 1 to 13 of 13

Thread: PHP and file uploads timing out too soon

  1. #1
    Senior Member
    Join Date
    Sep 2004
    Posts
    371
    Thanks
    44
    Thanked
    10 times in 9 posts

    PHP and file uploads timing out too soon

    I have a university project that involves making a database driven website using PHP and MySQL. My group has decided to make a website that allows users to upload image files. I have created an HTML form that allows the user to locate the file on their hdd. When they submit the file the page postbacks and allows the user to enter some more details about the file such as a description.

    Everything works perfectly accept for one thing. The page seems to timeout after about 12-15seconds on files ~300KB or bigger. I have searched the net for hours trying to figure out why this is happening. To try and solve it I have done various things such as:
    - used the PHP function set_time_limit(0);
    - used another PHP function to change PHPs settings from within the PHP file (forgotten what it is)
    - uploaded a php.ini file to the same dir as the PHP files with settings for: max_execution_time, post_max_size, memory_limit, max_input_time, upload_max_filesize. My host (QiQ) says this should work but it has made no difference.
    - got rid of the postback and called another page
    - removed the check is_uploaded_file()
    - changed the file extension to php5

    I have also checked the relevant settings for PHP and they are all default, meaning it should at least wait 30seconds and handle much bigger files than 300KB. In case it helps the server API is CGI. I also have checked the MIME type for the file when it times out and it says application/octet-stream. I contacted my host and they don't seem to think their is a problem their side. I'm really at a loss.

    If anyone has any ideas on what the problem might be I would love to hear them.

  2. #2
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    Thanks
    1
    Thanked
    0 times in 0 posts
    create a page with just one line of code in it
    Code:
    <? phpinfo(); ?>
    Upload that and run it and see what the settings are for post_max_size, file_uploads, upload_tmp_dir and upload_max_filesize. Check that they match the values you put into your php.ini file

    You also have to remember that the file will go into the temp dir first before you can then move it to the destination you want. I'm assuming you haven't got that far yet though since it's timing out halfway through. You will also have to stop and restart the web server to get it to pick the updated php.ini settings (if memory serves)

  3. #3
    Senior Member
    Join Date
    Sep 2004
    Posts
    371
    Thanks
    44
    Thanked
    10 times in 9 posts
    I've used phpinfo() to find out the values already and they were all default (or at least I think they were from what I've read). I'll post them anyway just incase:
    max_execution_time = 30
    max_input_time = 60
    memory_limit = 8M
    post_max_size = 8M
    upload_max_filesize = 2M

    I've just thought, when you do phpinfo() there is a local value and master value. If I upload my own php.ini in to the same directory as the php pages should the local value change? Although, as I said before, it should at least time out after 30seconds which it doesn't so I'm sure that's not the main problem.

    I'm not sure if I can stop and restart the webserver. I'm assuming I've got shared hosting, although I could be wrong. I'll look in to it though.

    Also, any images under 300k upload fine. I check the uploaded file is the one the user tried to upload through the form, check the file type to see if it's an image, if it is I move it to the users folder. I then let them enter any extra details such has image name and description. Then when they submit that an entry is entered in the database and the image is then renamed with the image ID. I then use image magick to downsize the image and watermark it.

    I have tested it on loads of images under 300KB and it all works fine. No problems what so ever. It's just images over 300KB that are an issue.

    EDIT: It seems the PHP settings are being changed when I upload my own php.ini without having to restart the server. It still times out after about 10seconds.
    Last edited by McClane; 28-11-2006 at 10:58 AM.

  4. #4
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    Thanks
    1
    Thanked
    0 times in 0 posts
    Seems silly but does your tmp folder have limited free space ?

  5. #5
    Senior Member
    Join Date
    Sep 2004
    Posts
    371
    Thanks
    44
    Thanked
    10 times in 9 posts
    I'm at uni now but I'll see if I can change the temp directory to my webspace when I get home to see if that solves it.

  6. #6
    Senior Member
    Join Date
    Sep 2004
    Posts
    371
    Thanks
    44
    Thanked
    10 times in 9 posts
    I tried changing the temp dir and that didn't make any difference I really don't understand. I bet it's something really silly.

  7. #7
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    Thanks
    1
    Thanked
    0 times in 0 posts
    can you post your code ?

  8. #8
    Senior Member
    Join Date
    Sep 2004
    Posts
    371
    Thanks
    44
    Thanked
    10 times in 9 posts
    PHP Code:
    <html>
        <head>    
        
        </head>
        <body>    
            <?php
                
    // Check if page called itself or not
                
    if ($_SERVER['REQUEST_METHOD'] == "POST") {
                    
    // Will be a session var
                    
    $currentUser "Nickycolgan";
                    
                    
    // upload directory
                    
    $filedir "./users/".$currentUser."/images/";
                    
                    
    // temp file location
                    
    $tempfile $_FILES['imagefile']['tmp_name'];

                    
    // Check the file is the one the user submitted
                    
    if (is_uploaded_file($tempfile)) {

                        
    // Store the image details in easy to remember variables
                        
    $filename $_FILES['imagefile']['name'];
                        
    $filetype image_type_to_mime_type(exif_imagetype($tempfile));
                        
    $imagesize number_format($_FILES['imagefile']['size']/(1024*1024), 2"."""); 
                        
    $imageattr getimagesize($tempfile);
                        
    $imagewidth $imageattr[0];
                        
    $imageheight $imageattr[1];                    
                        
                        
    // Check the file type is one that the website supports ie image files
                        
    if ($filetype  == "image/gif" || $filetype  == "image/jpeg" || 
                            
    $filetype  == "image/bmp" || $filetype  == "image/tiff" || $filetype  == "image/png") {
                            
                            
    // Move the file to the users directory
                            
    if(move_uploaded_file ($tempfile$filedir.$filename)) {
                                
    // Print our some information about the file that was uploaded
                                
    echo "File uploaded successfully!<p />";                            
                                echo 
    "File Name: ".$filename."<br />";
                                echo 
    "File Type: ".$filetype."<br />";
                                echo 
    "File Size: ".$imagesize."<br />";
                                echo 
    "Image Width: ".$imagewidth."<br />";
                                echo 
    "Image Height: ".$imageheight."<br />";                            
                                
                                
    // Get the user to enter any additional details about the image
                                
    ?>                            
                                <form action="uploaded.php" method="POST">
                                Image name: <input type="text" name="imagename" maxlength="40" /><br />
                                Description: <input type="text" name="imagedescription" /><br />
                                Price: £<input type="text" name="imagepricepounds" maxlength="4" />.<input type="text" name="imagepricepence" maxlength="2" /><br /><br />
                                <input type="hidden" name="filedir" value="<? echo $filedir ?>" />
                                <input type="hidden" name="filename" value="<? echo $filename ?>" />
                                <input type="hidden" name="filetype" value="<? echo $filetype ?>" />
                                <input type="hidden" name="imagesize" value="<? echo $imagesize ?>" />
                                <input type="hidden" name="imagewidth" value="<? echo $imagewidth ?>" />
                                <input type="hidden" name="imageheight" value="<? echo $imageheight ?>" />
                                <input type="submit" value="Add Image" />
                                </form>                                
                                <?php
                                
                            
    } else {
                                
    // The file could not be moved
                                
    echo "There was an error uploading the file. Please try again later.<br />";
                            }
                        } else {
                            
    // The file type didn't meet any of the types listed above
                            
    echo "The file you uploaded was not a image file that we support.<br />";
                        }                  
                    } else {
                        switch(
    $_FILES['tempfile']['error']){
                            case 
    0:
                                echo 
    "The file your are trying to upload may not have been the same file that was submitted. Please try again later.";
                                break;
                            case 
    1:
                            case 
    2:
                                echo 
    "The file you are trying to upload is too big.";
                                break;
                            case 
    3
                                echo 
    "The file you are trying upload was only partially uploaded. Please try again later.";
                                break;
                            case 
    4:
                                echo 
    "You must select an image to be uploaded.";
                                break;
                            default:
                                echo 
    "There was a problem with the file you tried to upload. Please try again later.";
                                break;                   
                       }
                    }            
                } else {    
                    
    // The page did not call itself so display a form to upload an image            
                    
    ?>            
                    <form enctype="multipart/form-data" action="<?$_SERVER['PHP_SELF'];?>" method="POST">
                    <input type="hidden" name="MAX_FILE_SIZE" value="300000" />
                    Upload Image: <input type="file" name="imagefile" />
                    <input type="submit" value="Upload Image" />
                    </form>    
                
                    <?php
                
    }
            
    ?>    
            
        </body>
    </html>

  9. #9
    Senior Member
    Join Date
    Sep 2004
    Posts
    371
    Thanks
    44
    Thanked
    10 times in 9 posts
    Solved the problem with the help of one of my course tutors. It was as simple as adding in an extra 0 in:
    <input type="hidden" name="MAX_FILE_SIZE" value="300000" />

    doh!

  10. #10
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line
    Thanks for posting this, i've been having no end of trouble with my file upload script - if you don't mind, i might just adapt yours a bit

  11. #11
    Senior Member
    Join Date
    Sep 2004
    Posts
    371
    Thanks
    44
    Thanked
    10 times in 9 posts
    Go for it

  12. #12
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line
    What did you have your as your folder's chmod?

  13. #13
    Senior Member
    Join Date
    Sep 2004
    Posts
    371
    Thanks
    44
    Thanked
    10 times in 9 posts
    755

    Also, thanks Iain for all the help you gave!

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. PHP file writing help needed
    By EvilWeevil in forum Software
    Replies: 5
    Last Post: 04-06-2006, 02:58 PM
  2. Replies: 0
    Last Post: 10-02-2006, 11:28 PM
  3. php - opening file to html form?
    By Ramedge in forum Software
    Replies: 11
    Last Post: 10-10-2005, 10:24 AM
  4. Replies: 13
    Last Post: 30-07-2005, 06:15 PM
  5. Writing to a file. (PHP)
    By Nasimov in forum Software
    Replies: 3
    Last Post: 04-05-2004, 08:20 PM

Posting Permissions

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