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

时间:2019-10-08 03:20:15

标签: php forms image file-upload directory

我正在尝试让用户上传图像,该图像将保留在名为“图像”的文件夹中。我不确定我是否正确设置了格式,因为我正在遵循的教程未使用服务器。

这是我当前遇到的错误:

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

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

Here is the link to the folder

下面是我的代码。感谢您的帮助。谢谢。

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

2 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

//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;
}

答案 1 :(得分:0)

请检查您的目标路径...我认为您的目标路径无效