将图像上传到我的服务器


Uploading an image to my server

所以我想允许用户上传图像并希望将该图像存储在我的服务器中。(使用 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图像不存在。

任何帮助将不胜感激。

我认为您可能缺少文件路径中的/

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

应该是

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