【发布时间】:2016-01-20 11:57:38
【问题描述】:
在项目文件夹中上传文件时遇到问题。我在下面解释我的代码。
$imageName=generateRandomNumber().'_'.$_FILES["uploadme"]["name"];
$target_dir = "upload1/";
$target_file = $target_dir . basename($imageName);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($_FILES["uploadme"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["uploadme"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["uploadme"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["uploadme"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
我在这里获取图像并添加一个随机数但是在上传图像时无法保存在upload1 文件夹中。这里我收到Sorry, there was an error uploading your file 消息。请帮我解决这个问题。
【问题讨论】:
-
检查文件夹的权限。
-
从脚本文件的位置确认你的文件夹路径是正确的
-
您使用的是linux操作系统吗?如果是这样,那就是文件夹权限问题。在终端中执行这个“sudo chmod 777 -R folder-path”命令
-
添加
var_dump(error_get_last())到else部分,看看错误并修复它 -
该文件夹的权限已经存在。
标签: javascript php file