PHP:用户将图像上载到站点时出错
我正在尝试让用户上传一个图像,该图像将保留在名为“图像”的文件夹中。我不确定是否正确设置了格式,因为我下面的教程没有使用服务器 以下是我当前收到的错误: 警告:移动上传的文件(/student/globalit/2019/GamerMedia/pages/images/TrollFace.jpg):无法打开流:第13行的/home/benrud/public\html/student/globalit/2019/GamerMedia/pages/account.php中没有此类文件或目录 警告:move_upload_file():无法将“/tmp/phpVzhJCX”移动到第13行/home/benrud/public_html/student/globalit/2019/GamerMedia/pages/images/TrollFace.jpg”中的“/student/globalit/2019/GamerMedia/pages/account.php” 下面是我的代码。任何帮助都将不胜感激。谢谢PHP:用户将图像上载到站点时出错,php,forms,image,file-upload,directory,Php,Forms,Image,File Upload,Directory,我正在尝试让用户上传一个图像,该图像将保留在名为“图像”的文件夹中。我不确定是否正确设置了格式,因为我下面的教程没有使用服务器 以下是我当前收到的错误: 警告:移动上传的文件(/student/globalit/2019/GamerMedia/pages/images/TrollFace.jpg):无法打开流:第13行的/home/benrud/public\html/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;
}
您可以尝试以下操作:
//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;
}
请检查您的目的地路径。。。我相信您的目的地路径无效请检查您的目的地路径。。。我相信您的目标路径无效请将您看到的错误发布到您的问题中。它比屏幕截图的链接更有用。谢谢服务器上是否存在目标文件夹?运行Web服务器的用户有权
写入此文件夹?请将您看到的错误发布到您的问题中。它比屏幕截图的链接更有用。谢谢服务器上是否存在目标文件夹?运行Web服务器的用户是否有权写入此文件夹?