Php – Why is PHP’s move_uploaded_file() method returning false in the script

fileformshtmlphpupload

I'm building a page that allows files to be uploaded to the server, and sorted. I know this code, even if it was working, isn't fit to be live, it was just a first draft to get the mechanics working since I'm quite new to server-side scripting.

Anyway, here's the upload-form page:

<html>
<body>
<?php include("header.php"); ?>
<center>
<h1>Please enter the details of the file you would like to upload:</h1><br/> 


    <table> 
    <tr>
    <td align='center'> 
    <form action='uploader.php' method='POST' enctype='multipart/form-data'><b>Name:</b> </td> 
    <td align='left'><input type='text' name='name' maxlength='255' />
    </td>
    </tr> 

    <tr>
    <td align='center'><b>Description of file uploaded: </b>
    </td> 
    <td align='left'><textarea name='description' rows='5' cols='20'></textarea>
    </td>
    </tr> 

    <tr>
    <td align='center'><b>Category: </b>
    </td> 

    <td align='left'> 
    <select name='category'> 
    <option value='1'>Agriculture</option>
    <option value='2'>Sexual Health</option>
    <option value='3'>Arithmetic</option>
    <option value='4'>Technology</option>
    <option value='5'>Reading/Literature</option>
    <option value='6'>Health/Medicine</option>
    </td>
    </tr>

    <tr>
    <td align ='center' >
    <b>File: </b>
    </td>
    <td align='left'>
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    <Choose a file to upload: <input name="uploadedfile" type="file" /><br />
    </td>
    </tr>

    <tr height='20px'></tr> 

    <tr align ='left'>
    <td> </td>
    <td>
    <input type='Submit' value='Upload File' /> 
    </td>
    </tr>
    </table> 

</center>

<?php include("footer.php"); ?>
</body>
</html>

and here's the action page uploader.php:

<?php

$target_path = "./audio/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file( $_FILES['uploadedfile']['tmp_name'], $target_path) )  {
    echo "The file " .  basename( $_FILES['uploadedfile']['name']) . " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

?>

Whenever I try to test this, on my localhost ( using XAMPP ) It echos "There was an error uploading the file, please try again!"

And every few times it also says: "
Notice: Undefined index: uploadedfile in C:\Users\Paul\Desktop\XAMPP\htdocs\xampp\uploader.php on line 5 "

But I can't see what's wrong with that argument? isn't uploadedfile defined as the temporary name for the file in my form?

Can you tell me what I've done wrong here?

Thanks a lot for

Best Solution

The problem is: your HTML is invalid, causing the form element being closed prematurely.

There's no </form> end tag, so it's inserted at the next appropriate point, i.e.

<td align='center'>
  <form action='uploader.php' method='POST' enctype='multipart/form-data'>
    <b>Name:</b>
  </form> <!-- <- here -->
</td> 

Doing so, the file input element with name uploadedfile is outside of the form and never gets send to uploader.php. To correct this issue, move the start tag outside of your table, add the end tag near the end of body and remove the < before Choose a file ….

There are a couple of other validating errors, but I'll leave it to you to repair those. Next time, please serve your HTML to W3C's validator (you can do this automatically using Chris Pederick's Web Developer toolbar) and/or look at Firebug's/Chrome's web inspector and look at the form element (and what it contains).

Related Question