我无法重命名上传的文件名

时间:2015-06-28 09:52:45

标签: php file-upload file-rename

<?php

ini_set('post_max_size', '40M');
ini_set('upload_max_filesize', '40M');

$filename = $_FILES['file']['name'];
$filesize = $_FILES['file']['size'];
$tmpname = $_FILES['file']['tmp_name'];
$filetype = $_FILES['file']['type'];
$folder = "uploads/";
$f_folder = $folder.$f_filename;
$date = date("d/m/Y H:i");
$f_filename = rename($filename,$filename.$date);


if (isset($_POST['do']) and $_POST['do'] == 'upload') {
    if (empty($filename)) {
        echo "choose file please";
    } else {
        move_uploaded_file($tmpname,$f_folder);
        echo "file is uploaded in ".$f_folder;
    }
};

echo "
<form action='upload.php' method='post' enctype='multipart/form-data' >

File Path : <input type='file' name='file' />
<br>
<input type='submit' name='do' value='upload' />

</form>
";

?>

任何var以f_开头这意味着它是最终结果 错误:

  

警告:重命名(aaaaa.PNG,aaaaa.PNG28 / 06/2015 12:45)   [function.rename]:在(php文件目录)中没有这样的文件或目录   13

     

警告:move_uploaded_file(uploads /)[function.move-uploaded-file]:   无法打开流:第20行(php文件目录)中的权限被拒绝

     

警告:move_uploaded_file()[function.move-uploaded-file]:无法执行   移动&#39; C:\ Windows \ TEMP \ php4A5B.tmp&#39;到&#39;上传/&#39;在(php文件目录)上   第20行文件在uploads /

上传

2 个答案:

答案 0 :(得分:0)

这是您正确的代码。您可以在移动时重命名文件。

<?php
ini_set('post_max_size', '40M');
ini_set('upload_max_filesize', '40M');

if (isset($_POST['do']) and $_POST['do'] == 'upload') {
  $filename = $_FILES['file']['name'];
  $filesize = $_FILES['file']['size'];
  $tmp_name = $_FILES['file']['tmp_name'];
  $file_type = $_FILES['file']['type'];
  $date = date("d-m-Y_H-i");
  $temp = explode(".", $filename);
  $new_filename = $temp[0] . '-' . $date . '.' . end($temp);
  $f_folder = "uploads" . DIRECTORY_SEPARATOR . $new_filename;

  if (empty($filename)) {
      echo "choose file please";
  } else {
      move_uploaded_file($tmp_name, $f_folder);
      echo "file is uploaded in " . $f_folder;
  }
};

 echo "
 <form method='post' enctype='multipart/form-data' >
  File Path : <input type='file' name='file' />
  <br>
  <input type='submit' name='do' value='upload' />
  </form>";
?>

答案 1 :(得分:0)

您的代码已损坏。在用户提交表单之前,您无法从$_FILES获取任何变量,$_POST["do"](提交)的值始终为&#34;上传&#34;。有些托管公司不允许您使用move_uploaded_file(),但不用担心,您可以使用rename()。首先移动文件,然后使用rename()对其进行重命名。并且你不应该只是将文件大小设置为变量而不用它做任何事情,你也应该检查它,以避免在不告诉用户任何事情的情况下拒绝过大的文件。

<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $filename = $_FILES['file']['name'];
    $filesize = $_FILES['file']['size'];
    if($filesize > 41943040){
        echo "File too large(max. 40MB)";
    } 
    elseif(empty($filename)){
        echo "choose file please";
    }
    else{
        $tmpname = $_FILES['file']['tmp_name'];
        $folder = "uploads/";
        $date = date("d-m-Y H:i");
        $f_filename = $filename. pathinfo($filename, PATHINFO_FILENAME) . $date . pathinfo($filename, PATHINFO_EXTENSION);
        $f_folder = $folder . rename($tmpname,$f_folder);
        echo "File was uploaded to " . $f_folder;
    }
}
?>
<form action='upload.php' method='post' enctype='multipart/form-data' >
File Path : <input type='file' name='file' />
<br>
<input type='submit' name='do' value='Upload' />
</form>