【问题标题】:php file renaming and folder renamingphp文件重命名和文件夹重命名
【发布时间】: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


【解决方案1】:

很难说你在做什么。例如行:

$dir = $file_path .= $input .'/';
$file_path = "img/";
$input = $_POST['caption']; // this is the new folder you'll create
$file_path .= $input . '/';

似乎不太明显。您将$dir 设置为$file_path 并附加了一些输入还是只是一个错误?

另一件事:

if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
    echo "success";
} else{
    echo "fail";
}

所以在这些行之后很难说发生了什么,因为你只有$file_path 没有任何文件名。使用move_uploaded_file时我总是宁愿使用文件名

另一件事 - 您似乎尝试上传 2 个文件。为什么只检查第二个文件而不检查第一个文件?或者这可能是错误的,您尝试只上传一个文件?在那种情况下,你的代码可能一切都错了。

另一个错误是:

rand (1.1,1);

对于rand(),您应该只传递整数而不是浮点数,据我所知,这个将始终返回1

$dir 目录也很可能不存在,因为您从未尝试在代码中创建一个。事实上,$dir 似乎是 / 是空字符串,所以你尝试在 / (根服务器目录)中创建文件当然是不允许的,因为你应该在你的用户/域目录中创建它。

我建议你重新编写整个代码。有很多可能的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 2014-01-20
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 2016-04-15
    相关资源
    最近更新 更多