【发布时间】:2014-09-09 22:33:28
【问题描述】:
我使用以下代码创建带有输入文本名称的新文件夹并使用重命名来上传文件
$dir = $file_path .= $input .'/';
$file_path = "img/";
$input = $_POST['caption']; // this is the new folder you'll create
$file_path .= $input . '/';
if (!file_exists($file_path)) {
mkdir($file_path);
}
chmod($file_path, 0777);
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
echo "success";
} else{
echo "fail";
}
$ran = rand (1.1,1);
$ran2 = $ran.".";
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file1"]["name"]));
if ((($_FILES["file1"]["type"] == "image/gif")
|| ($_FILES["file1"]["type"] == "image/jpeg")
|| ($_FILES["file1"]["type"] == "image/jpg")
|| ($_FILES["file1"]["type"] == "image/pjpeg")
|| ($_FILES["file1"]["type"] == "image/x-png")
|| ($_FILES["file1"]["type"] == "image/png"))
&& ($_FILES["file1"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file1"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file1"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file1"]["name"] . "<br>";
echo "Type: " . $_FILES["file1"]["type"] . "<br>";
echo "Size: " . ($_FILES["file1"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file1"]["tmp_name"] . "<br>";
if (file_exists("$dir" . $_FILES["file1"]["name"]))
{
echo $_FILES["file1"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file1"]["tmp_name"], "$dir" . $ran2 . $extension);
echo "Stored in: " . "$dir" . $_FILES["file1"]["name"];
}
}
}
else
{
echo "Invalid file or service down";
echo "It must be an image to be uploaded.";
}
但是当我在托管中运行时,我收到以下错误,我从您自己的网站获得了上述代码,我将两个代码组合为单个代码文件,创建文件夹并重命名文件
Warning: move_uploaded_file(/1.png) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/a3629462/public_html/1.php on line 49
【问题讨论】:
-
我不知道解决方案,但我有一个建议。替换 (($_FILES["file1"]["type"] == "image/gif") || ($_FILES["file1"]["type"] == "image/jpeg") || ($ _FILES["file1"]["type"] == "image/jpg") || ($_FILES["file1"]["type"] == "image/pjpeg") || ($_FILES["file1 "]["type"] == "image/x-png") || ($_FILES["file1"]["type"] == "image/png")) with in_array($_FILES['file1' ]['type'], array("image/gif","image/jpeg","image/jpg","image/pjpeg","image/x-png","image/png")
-
$input为空白(可能为 NULL),您的脚本将文件发送到“root”,这是错误的,使用绝对路径,您要发送文件的文件夹是哪个?变量$input应该有什么值? -
运行时创建了一个新文件夹但文件没有移动到服务器
-
第 49 行是哪一行?回声的输出是什么?您是否尝试一次上传 2 个文件?
-
@user3853978 看我的回答
标签: php html forms file-upload rename