因此,我创建了一个小脚本,允许用户上传其汽车的多个图像。然后,我处理这些图像并将文件路径存储在数据库中,并在“vehicleImages”文件夹内创建一个文件夹来存储图像。不过,我一直遇到一个问题,所以正如你所看到的,我检查该文件夹是否存在,如果不存在,我就创建一个。然后图像得到处理,理论上应该存储在该文件夹内。
我遇到的问题是,它创建的文件夹就像它应该的那样,例如XX00VVV。但是,它不是将图像存储在“vehcileImages/XX00VVV”内,而是将图像存储在“vehicleImages”内,而不是在正确的文件夹内。我检查了服务器,文件夹“XX00VVV”确实被创建,但由于某种原因图像没有存储在里面。我认为这是我改变目标路径的地方,但我不明白为什么。有人可以给我一些关于我哪里出错的指示/提示吗?
$imagePath = "../../images/vehicleImages/".$vehicleReg;
$fileName = $vehicleReg;
if (!file_exists($imagePath)) {
mkdir($imagePath, 0777, true);
}
//Image Upload Section
$j = 0; //Variable for indexing uploaded image
$target_path = $imagePath; //Declaring Path for uploaded images
for ($i = 0; $i < count($_FILES['file']['name']); $i++) { //loop to get individual element from the array
$validextensions = array("jpeg", "jpg", "png"); //Extensions which are allowed
$ext = explode('.', basename($_FILES['file']['name'][$i])); //explode file name from dot(.)
$file_extension = end($ext); //store extensions in the variable
$target_path = $target_path.md5(uniqid()).
".".$ext[count($ext) - 1]; //set the target path with a new name of image
$j = $j + 1; //increment the number of uploaded images according to the files in array
if (($_FILES["file"]["size"][$i] < 100000000) //Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) { //if file moved to uploads folder
echo $j.
').<span id="noerror">Image Uploaded Successfully!.</span><br/><br/>';
} else { //if file was not moved.
echo $j.
').<span id="error">Please Try Again!.</span><br/><br/>';
}
} else { //if file size and file type was incorrect.
echo $j.
').<span id="error">***Invalid File Size or Type***</span><br/><br/>';
}
}
//End Image Upload Section
最佳答案
所以问题出在代码的第一行$imagePath
$imagePath = "../../images/vehicleImages/".$vehicleReg;
尝试将 ."/"
添加到行尾
像这样:
$imagePath = "../../images/vehicleImages/".$vehicleReg."/";
<小时/>
解决方案的解释是:
当你没有在路径末尾添加 /
时,你可以将它与 filename 合并,例如 XX00VVVfilenam.png
关于PHP图像处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55078151/