Paul
Paul

Reputation: 3582

Why is PHP's move_uploaded_file() method returning false in my script?

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

Upvotes: 2

Views: 8662

Answers (3)

Marcel Korpel
Marcel Korpel

Reputation: 21763

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).

Upvotes: 1

sunil
sunil

Reputation: 187

if ($_FILES['uploadedfile']['error'] != 0) will give you the possible reasons for an error. Look at http://www.php.net/manual/en/features.file-upload.errors.php for the error codes

Upvotes: 1

Nick
Nick

Reputation: 6965

You appear to be calling the function correctly and using it in the correct context.

As far as the documentation is concerned there are two instances in which false can be returned from the move_uploaded_file function:

If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.

We can assume this isn't the case because we're using the tmp_name element from our file in the $_FILES array.

If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.

So it must be something to do with this catch all. I'd suggest turning display_errors on so we can see the warning that's generated.

Upvotes: 1

Related Questions