2

I've worked with a few scripts to begin uploading files on my development machine. Problem is, despite the expected ease of this operation, Apache seems to time-out whenever I try to upload an image. Uploading is set to On and the tmp directory is set in php.ini.

I tried uploading the main gif from Google, an 8.36KB image. It should be fine and well within the limits to PHPs uploading capabilities.

Here is a copy of the script. There should be an easy fix. As requested, I changed the tilde to an actual directory.

<?php 

if (!isset($_GET['upload'])) { ?>
  <form method="post" action="index.php?upload=true" enctype="multipart/form-data"> 
  <input type="file" name="file" class="form">
  <input name="submit" type="submit">
  </form>
<? } else  if (isset($_GET['upload']) && $_GET['upload'] == 'true') {
  $url = $_FILES['file']['name'];
    $move = move_uploaded_file($_FILES['file']['tmp_name'], "/Users/<username>/Sites/file.jpg");
    if ($move) {
        echo "Success!";
    } else { 
        echo "Err..."
    } 
} ?>

Thanks, Dan

EDIT:

I fixed it, with help from a few of the answers, to one of which I will mark.

A few things here were causing this behavior.

  1. Permissions on the images directory were not set to allow the _www user to access it. A chmod -R 777 images seemed to fix it, as well as a sudo chown _www images.

  2. The form output may have been corrupting the PHP script itself. As suggested, an ECHO <<< ...END helped, I think.

2
  • Try testing it with a small image so we can nail down whether it's due to file size or a problem with the code and/or configuration. Post your results. Commented May 30, 2009 at 4:56
  • I tested with the Google logo. Still a no-go. Commented May 30, 2009 at 14:45

4 Answers 4

3

What is it that leads you to believe that Apache is timing out rather than, say, outright failing in some way? Because what leaps out at me is that you're trying to move the file to ~/file.jpg, which I'm nearly certain will not work; ~ is a construct that only normally has meaning inside shells, unless one of PHP's freakish obscure features is processing it in contexts like this. Anyway, try putting the actual directory.

Sign up to request clarification or add additional context in comments.

1 Comment

Don't know, other than the request takes FOREVER to complete. In fact, it doesn't complete. It's been a few minutes the last time I tried and stopped it. No logs in Apache regarding this action are recorded, either.
2

This is more than likely an issue with the size of the file and/or a permission issue between the Apache user and the directory specified. For instance make sure the Apache instance is not running under user (nobody).

Comment to chaos: He is right the tilde (~) can cause issues, but would probably not cause a timeout; it would display a warning. Even if it does work on your system it would probably deposit the file into an unexpected directory or run into some issues if the Apache user (ie www) does not have a valid home directory set.

1 Comment

You were right, the wrong permissions were set on the images directory. Thanks!
2

If the issue is filesize, add the following lines to your php.ini file and it should work:

upload_max_filesize = 500M ;
post_max_size = 500M ;

1 Comment

It's not, I tried uploading a very small (8.36KB) file above, and after 40 minutes, still nothing.
2

PHP by default has a 30 second timeout on the page. So if your upload takes longer than 30 seconds it will fail. Set the timeout either in your php.ini or put the following code at the top of the file.

ini_set(max_execution_time, 90);

The second argument represents the time in seconds before the page will timeout. Set it to whatever time you feel is appropriate. Also, chaos is correct in that '~' is a construct that commonly has meaning only inside shells.

Re: https://www.php.net/manual/en/ini.list.php

EDIT: The problem is that you reopened the tag in the middle of a conditional. Trying your code I get a syntax error. It's strange that you were able to see any web form. This is the fixed code (that works for me).

<?php 
if (!isset($_GET['upload'])) { 
ECHO <<<END
  <form method="post" action="index.php?upload=true" enctype="multipart/form-data"> 
  <input type="file" name="file" class="form">
  <input name="submit" type="submit">
  </form>
END;
 } else  if (isset($_GET['upload']) && $_GET['upload'] == 'true') {
    $url = $_FILES['file']['name'];
    $move = move_uploaded_file($_FILES['file']['tmp_name'], "/Users/<username>/Sites/file.jpg");
    if ($move) {
        echo "Success!";
    } else { 
        echo "Err...";
    } 
} ?>

1 Comment

Even though the timeout is set, Safari still says the page is loading. This is with your suggested line at the top of my script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.