Jump to content

"Invalid upload file" appears and then disappears


Chrisj

Recommended Posts

I've added this code (below) to a file called uploader.php (attached).

 

When I simply open the corresponding html page, I see "Invalid upload file" in the top left corner of the page. When I remove this code from uploader.php the "Invalid upload file" disappears. Any ideas on how to resolve this will be appreciated.

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = strtolower( end($temp) ); // in case user named file ".JPG" etc.!!!

if (  $_FILES["file"]["size"] < 200000 
      && in_array($extension, $allowedExts) )
{
    if ($_FILES["file"]["error"] > 0) 
    {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    } else {
        $length = 20;
        $randomString = substr(str_shuffle(md5(time())),0,$length);
        $newfilename = $randomString . "." . $extension;
        move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename);
      
       $file_location = '<a href="http://www.-----.com/upload/' . $newfilename . '">' . $newfilename . '</a>';
    }
} else {
  echo "Invalid upload file";
}
$description = $description . " \n " . $newfilename;
Link to comment
Share on other sites

Do you have php error checking turned on so you might see any possible errors in this code?  I'm guessing that (obviously) your if statement is failing so that means either you are specifying the upload file field name incorrectly or the size is > 200000.  Try echoing out those values before you execute the test to confirm what you think is there.

Link to comment
Share on other sites

the code you have posted is for processing a form submission. you are seeing the invalid .... message because you are unconditionally running the code when the page is requested and none of the values being tested exist, resulting in a false value for the conditional test that causes the invalid ... message to be output.

 

any form processing code needs to test if the expected form has been submitted before attempting to use any of the form data.

 

you are also seeing the error message at the top-left of the page because you are not outputting it through the template system the script you are trying to use is using to produce the html pages. you cannot simply copy/paste together code without taking into account the methods being used by the code you are trying to modify.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.