1
votes

I'm trying to test a simple file upload script but it fails. Here is the html portion.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title="testing"></title>
</head>

<body>
    <form enctype="multipart/form-data" action="uploader.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    Choose a file to upload: <input type="file" name="uploaded"/>
    <input type="submit" value="Upload File"/>
    </form>
</body>

And below this is the php portion.

<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;

if ($uploaded_size > 350000)
{
    echo "Your file is too large.<br>";
    $ok=0;
}

if ($uploaded_type =="text/php")
{
     echo "No PHP files<br>";
     $ok=0;
}

if ($ok==0)
{
    Echo "Sorry your file was not uploaded";
}

else
{
    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
    {
            echo "The file ". basename( $_FILES['uploadedfile']['name']). " has   been             uploaded";
    }
    else
     {
            echo "Sorry, there was a problem uploading your file.";
     }
 }
 ?>

It fails at this line:

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))

And execute the else portion: "Sorry, there was a problem uploading your file." How can I get it to work without failing. Thank you for you help.

2
Do you have permission to write the file where you are trying to move itAnigel
IMHO the following SO question has a lot of useful information about PHP file uploads stackoverflow.com/questions/3501749/…Marcello Romani

2 Answers

0
votes

The most likely reason is lack of write access to the target directory.

Also, I'd check if $target resolves correctly, as there could be artifacts in the filename that seem to be passed directly to the script and are not filtered.

0
votes

You could test the permissions with is_writable( PATH_OF_FOLDER )

I also tend to test the existance of the file with is_file( PATH_OF_NEW_FILE )

before confirming the upload is successful!