Php upload script suddenly stops working

I made a very simple php upload script a year or two ago. Everything always worked fine until last week, when file upload stopped working.

I haven’t changed anything. It just doesn’t work now. When I try the upload, it seems like the file is uploading, as it takes a while for bigger files, then I even get a success message that everything is OK, like I used to when the upload worked. However, the file is just not there after the upload.

I tried everything, and the weird part is that after I reboot my server, the file upload works for 2-3 files, and then it doesn’t work again… Maybe there is not enough RAM? Could that be the problem?

Here’s my script:

  1. The form:
<form method="POST" enctype="multipart/form-data" name="upload_form" action="upload.php">
<p><input type="file" name="url" size="40"></p>
<p><input type="submit" value="Upload" name="action"></p>
</form>
  1. The upload
<?
//print_r($_POST);

if($_POST["action"] == "Upload")
{
unset($filename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['url']))
$error["url"] = "An image was not found.";


$filename = basename($_FILES['url']['name']);
//echo $filename;

if(empty($filename))
$error["filename"] = "The name of the image was not found.";

if(empty($error))
{
$newfile = "../uploads/" . $filename;
echo "The file <b>$filename</b> was successfully uploaded";
$result = @move_uploaded_file($_FILES['url']['tmp_name'], $newfile);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}

}

?>

What errors if any are you getting?

 <?
//print_r($_POST);

if($_POST["action"] == "Upload")
{
unset($filename);

if(!isset($_FILES['url']))
$error["url"] = "An image was not found.";


$filename = basename($_FILES['url']['name']);
//echo $filename;

if(empty($filename))
$error["filename"] = "The name of the image was not found.";

if(empty($error))
{
$newfile = "../uploads/" . $filename;
echo "The file <b>$filename</b> was successfully uploaded";
$result = @move_uploaded_file($_FILES['url']['tmp_name'], $newfile);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
var_dump($_FILES['userfile']['error']); // <--- display error number 
}
?>

Add the line marked, that will display the contents of the $_FILES[‘userfile’][‘error’] which will be a number. That number is a kinda “status code” which indicates whether the upload of the file to wherever it is going was successful.

These two lines are not needed

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

as the use of was depreciated as of php version 4.1

Thanks for your help. I added the error code


var_dump($_FILES['userfile']['error']); // <--- display error number 
}

and the error I get is NULL

What does it mean?

There could be some possible reasons for this. If you are running this script i a server and the server might have been changed configurations or upgraded the PHP. So just try the following code once:


if($_POST["action"] == "Upload"){
    if(is_uploaded_file($_FILES['url']['tmp_name'])){
        $filename = basename($_FILES['url']['name']);
        $newfile = "../uploads/" . $filename;
        if(move_uploaded_file($_FILES['url']['tmp_name'], $newfile)){
            echo "The file <b>$filename</b> was successfully uploaded";
        }
        else{
            $error["result"] = "There was an error moving the uploaded file.";
        }
    }
    else{
        $error["url"] = "An image was not found.";
    }
}

What errors is php giving when file uploading stops working? Uncomment the print_r() at the start, what is displayed by it?

Hi, I uncommented the print_r() and I get this after upload finishes

Array ( [action] => upload ) The file flower-thumb.gif was successfully uploadedNULL

So it seems as if the file was uploaded, but it’s just not there.

@rajug, thanks, I tried it, but also does not work