2

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

6
  • 1
    What do your permissions look like?
    – Nick
    Apr 17, 2011 at 12:58
  • the folder is on my local machine and there doesn't seem to be any special constraints on the folder.
    – Paul
    Apr 17, 2011 at 13:05
  • 1
    When an error occurs, you can use $_FILES['uploadedfile']['error'] and check what's going on exactly (my guess is that you're moving the file to nonexisting directory). Consult php.net on how to interpret error messages when dealing with file uploads. Apr 17, 2011 at 13:05
  • The error I keep running into is "Undefined index: uploadedfile" I thought the form added the file to this array for me... am i supposed to be adding it myself somewhere?
    – Paul
    Apr 17, 2011 at 13:08
  • Even better, always check whether or not $_FILES['uploadedfile']['error'] == UPLOAD_ERR_OK and act accordingly on error (see php.net/manual/en/features.file-upload.errors.php). Apr 17, 2011 at 13:09

3 Answers 3

1

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

2
  • Thank you very much this was indeed the problem! I just assumed it was the PHP since I was so new to it! One thing though... I've been testing this script now that it functions... and it still causes errors when I upload .mp3 files. I thought it was just a size problem, but even <1mb audio files can't be uploaded. is 'file' supposed to encompass audio? O.o
    – Paul
    Apr 17, 2011 at 16:19
  • @Paul: that shouldn't cause any problem; the only thing I see is that your pasted code contains a maximum file size rule <100,000 bytes (≠ 1 Mb). As said earlier, always check the status of the 'error' segment of the file array against the values on php.net/manual/en/features.file-upload.errors.php and you'll know if something (and what) went wrong (e.g., UPLOAD_ERR_FORM_SIZE). Apr 17, 2011 at 16:23
1

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.

2
  • 1
    I think it goes wrong in an earlier place, because of the error ‘Undefined index: uploadedfile’ in $_FILES. Apr 17, 2011 at 13:12
  • the erros I get are : Notice: Undefined index: uploadedfile in C:\Users\Paul\Desktop\XAMPP\htdocs\xampp\uploader.php on line 8 Notice: Undefined index: uploadedfile in C:\Users\Paul\Desktop\XAMPP\htdocs\xampp\uploader.php on line 10
    – Paul
    Apr 17, 2011 at 13:21
1

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

2
  • Thanks for your input, but this should be a comment. Even more, as you can see above, I already posted a comment to the question with this message (and yes, I know you can't post a comment on someone else's question if your rep is below 50). Apr 17, 2011 at 13:15
  • hmm, when I do this I get an extra error: Notice: Undefined variable: target_path in C:\Users\Paul\Desktop\XAMPP\htdocs\xampp\uploader.php on line 10
    – Paul
    Apr 17, 2011 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.