PHP:用户将图像上传到站点时出错



我试图让用户上传一张图片,该图片将保留在一个名为"Images"的文件夹中。我不确定我是否正确格式化了它,因为我遵循的教程没有使用服务器。

以下是我目前收到的错误:

警告:move_uploaded_file(/student/globalit/2019/GamerMedia/pages/images/TrollFace.jpg(:无法打开流:第 13 行的/home/benrud/public_html/student/globalit/2019/GamerMedia/pages/account.php 中没有此类文件或目录

警告:move_uploaded_file((:无法将 '/tmp/phpVzhJCX' 移动到/home/benrud/public_html/student/globalit/2019/GamerMedia/pages/images/TrollFace.jpg' 在第 13 行 的/home/benrud//student/globalit/2019/GamerMedia/pages/account.php

这是文件夹的链接

下面是我的代码。任何帮助,不胜感激。谢谢。

<?php
if(isset($_POST['submit'])){
move_uploaded_file($_FILES['file']['tmp_name'],'/student/globalit/2019/GamerMedia/pages/images/'.$_FILES['file']['name']);
}
?>

您可以尝试以下操作:

//images may be many, just load into a named array 
$filesArray = $_FILES;
//first properly target your directory (which should have write permission)
$ximage = "ppsize_photo";   //actual name or id as in html 
$target_dir = $_SERVER['DOCUMENT_ROOT'] ."/student/globalit/2019/GamerMedia/pages/images/"; 
$target_file = $target_dir . basename($filesArray[$ximage]["name"]);
$ext1 = pathinfo($target_file, PATHINFO_EXTENSION);
$imageFileType = $ext1;     //you may use this later to reject some types 
//ensure that your file names are unique (use any means possible)
$unique_id = substr( base_convert( time(), 10, 36 ) . md5( microtime() ), 0, 16 );
$unique_id1 = $unique_id . "." . $ext1; 
$target_file = $target_dir . $unique_id1;   //complete file name; maybe here is where your error was 
//you may also validate the extensions; it may not be an image 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
exit();
}else {
//fine
}
//now move the image 
$uploadFlag = false;
if (move_uploaded_file($filesArray[$ximage]["tmp_name"], $target_file)) {
//it went fine 
$uploadFlag = true;             
} else {
//echo "Sorry, there was an error uploading your file.";
$uploadFlag = false;
}

请检查您的目标路径...我相信您的目标路径无效

最新更新