将图像上传到我的服务器

时间:2016-04-22 03:46:24

标签: php html image-uploading

所以我想允许用户上传图片并希望将该图像存储在我的服务器中。 (使用Ubuntu和Apache)所以服务器路径是(/ var / www / html /)

我的HTML代码是:

<!DOCTYPE html>
<html>
<body>
<form enctype="multipart/form-data" action="uploadimages.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form> 
</body>
</html>

和我的uploadimages.php文件代码为:

<?php
$uploaddir = '/var/www/html/images';
$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 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>

当我点击底部发送时,我收到以下消息:

File is valid, and was successfully uploaded. Here is some more debugging info:Array ( [userfile] => Array ( [name] => thisisthepictureIupload.png [type] => image/png [tmp_name] => /tmp/phpIUEUjE [error] => 0 [size] => 4596 ) )

但是当我转到文件夹时:/var/www/html/images图像不存在。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

我认为您可能错过了文件路径中的/

 $uploaddir = '/var/www/html/images';
 $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

应该是

$uploaddir = '/var/www/html/images';
$uploadfile = $uploaddir . '/' . basename($_FILES['userfile']['name']);