问题
I'm trying to upload file to server with php script. I'm Using MAMP
with PHP7.0.8
I have HTML form:
<form enctype="multipart/form-data" action="hi" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
Send File: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
And simple php:
<?php
$uploaddir = 'img/';
print_r($uploadfile = $uploaddir.basename($_FILES['userfile']['name']));
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'info:';
print_r($_FILES);
echo "<br />";
print_r($_FILES['userfile']['error']);
print "</pre>";
?>
My result is:
Possible file upload attack!
info:Array
(
[userfile] => Array
(
[name] => high-school-icon.png
[type] => image/png
[tmp_name] => /Applications/MAMP/tmp/php/phpoaPmYv
[error] => 0
[size] => 1807
)
)
回答1:
Form action documentation
Error:
action="hi"
Change it to: action="simple.php"
<form enctype="multipart/form-data" action="simple.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
Send File: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
回答2:
Sorry, it was stupid mistake. Wrong way for $uploaddir = 'img/';
来源:https://stackoverflow.com/questions/43098442/php-upload-file