I'm having a problem with getting PHP to upload a file. I've been wrestling with this for a few days. I'm sure the solution is simple but I'm a new coder.
I'm copying exercises from a book (PHP for the Web 4th edition). When I try to upload anything with this script, nothing happens. The page just refreshes. No errors are printed or anything.
I'm using WAMP on Windows 10. Here's the code. Does anything jump out to anyone?
<!DOCTYPE html>
<html>
<head>
<title>Upload A File</title>
</head>
<body>
<?php // Script 11.4 - upload_file.php
// address error reporting
error_reporting(E_ALL & ~E_NOTICE);
// Check if the form was submitted
if ($_SERVER['REQEST_METHOD'] == 'POST') {
// Move file to final destination
if (move_uploaded_file ($_FILES['the_file']['tmp_name'], "../uploads/{$_FILES['the_file']['name']}")) {
echo '<p>Your file has been uploaded.</p>';
} else { // Problem!
echo '<p style="color: red;"> Your file could not be uploaded because: ';
// Print an error message if file relocation didn't work
switch ($_FILES['the_file']['error']) {
case 1:
echo 'The file exceed the upload_max_filesize setting in php.ini';
break;
case 2:
echo 'The file exceed the MAX_FILE_SIZE setting in the HTML form';
break;
case 3:
echo 'The file was only partially uploaded';
break;
case 4:
echo 'No file was uploaded';
break;
case 6:
echo 'The temporary folder does not exist.';
break;
default:
echo 'Something unforseen happened.........';
break;
}
// Complete the error message and close both conditionals
echo '.</p>'; // Complete the end of paragraph
} // End of move_uploaded_file() IF
} // End of submission IF
?>
<form action="upload_file.php" enctype="multipart/form-data" method="post">
<p>Upload a file using this form:</p>
<input type="hidden" name="MAX_FILE_SIZE" value="300,000">
<p><input type="file" name="the_file"></p>
<p><input type="submit" name="submit" value="Upload This File"></p>
</form>
</body>
</html>
You've got a typo in this line:
if ($_SERVER['REQEST_METHOD'] == 'POST') {
It should be
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
Side note: don't suppress notices. You should always use error_reporting(E_ALL)
, or at very least, error_reporting(E_ALL & ~E_STRICT)
setting, it will help you learn good practices like checking if variables or array indexes are set. It'll require you to write some extra boilerplate code, but will save you a lot of pain later. In this case, you would find out right away about Undefined index "REQEST_METHOD" in line 7
.
What jumps out at me?
1) Well, Mr. Zasada is correct that $_SERVER['REQUEST_METHOD']
has a spelling error (credit where credit is due). Also, the opening <html>
tag was missing (although, that would not affect PHP).
2) In your final project, using the user agent supplied file name $_FILES['the_file']['name']}
is inadvisable without taking security precautions (filter/validate). Some would avoid using the user supplied file name altogether.
3) The absence of is_uploaded_file($_FILES['the_file']['tmp_name'])
in your file move statement. Some might say that move_uploaded_file()
is enough, but I say a quick double check cannot hurt. :-)
if(is_uploaded_file($_FILES['the_file']['tmp_name']) &&
move_uploaded_file ($_FILES['the_file']['tmp_name'], "../uploads/{$_FILES['the_file']['name']}")){}