图片上传不适用于php7
我使用nginx作为服务器和php7.我遵循了一些指示,并且一定做错了什么.
I use nginx as server and php7. I followed some instructions and must have made something wrong.
表格:
<div class="collapse" id="upload_avatar">
<div class="card card-body">
<form enctype="multipart/form-data" action="" method="post">
<p class="text-left">Upload Avatar:</p>
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<input name="image" type="file" /><br>
<button class="form-control mr-sm-2 btn btn-outline-success my-2 my-sm-0" type="submit" name="avatar_upload" aria-controls="collapse_upload_avatar">
Upload
</button>
</form>
</div>
</div>
php部分:
if(isset($_POST["avatar_upload"])){
$verifyimg = getimagesize($_FILES['image']['tmp_name']);
if($verifyimg['mime'] != 'image/png') {
echo "Only PNG images are allowed!";
exit;
}
$uploaddir = '/members/3/';
$uploadfile = $uploaddir . basename($_FILES['image']['name']);
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.<br>";
} else {
echo "Possible file upload attack!<br>";
}
echo '<pre>';
echo 'info:';
print_r($_FILES);
print "</pre>";
}
它打印出来:
Possible file upload attack!
info:Array
(
[image] => Array
(
[name] => Selection_001.png
[type] => image/png
[tmp_name] => /tmp/phpGpp3rB
[error] => 0
[size] => 299338
)
)
没有/tmp/php* file
/members/3/
目录中没有文件
/members
和/members/3
nginx/error.log显示: PHP消息:PHP警告:move_uploaded_file(/members/3/Selection_001.png):无法打开流:第197行上没有此类文件或目录... PHP消息:PHP警告:move_uploaded_file():无法将'/tmp/phpGpp3rB'移动到'/members/3/Selection_001.png'
nginx/error.log shows: PHP message: PHP Warning: move_uploaded_file(/members/3/Selection_001.png): failed to open stream: No such file or directory ... on line 197 PHP message: PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpGpp3rB' to '/members/3/Selection_001.png'
是否缺少nginx设置?
Is a nginx setting missing???
在您的成员目录中删除/.
Remove / in your members directory.
$ uploaddir ='members/3/';
$uploaddir = 'members/3/';