【问题标题】:Renaming a $_FILES (file) in PHP在 PHP 中重命名 $_FILES(文件)
【发布时间】:2015-12-12 08:35:28
【问题描述】:

我想知道为什么我的代码不能正确重命名我的文件。这就是我相信我的代码所做的。

  • 从 POST 中获取 $_FILES(文件)
  • 将其移至特定文件夹 (uploads/)
  • 将其重命名为日期格式(我想这样做以便没有文件具有相同的名称)


<?php

session_start();

$temps = date('Y-m-d H:i:s');

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {

    } else {
        $_SESSION['error'] = "This is not an image -.-";
        header('location:upload.php');
    }
}

if ($_FILES["fileToUpload"]["size"] > 2000000) {
    $_SESSION['error'] = "The uploaded image must be smaller than 2MB";
    header('location:upload.php');
}

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    $_SESSION['error'] = "Sorry, only JPG, PNG & GIF files are allowed";
    header('location:upload.php');
}

if ($uploadOk == 0) {
    $_SESSION['error'] = "There was an error uploading your file, please try again !";
    header('location:upload.php');
} 
else {
    $new = ($target_dir.$temps.'.'$imageFileType);
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        if (rename($target_file, $new) == true)
        {
            $_SESSION['error'] = "Your image has been uploadsdsaded with success ... Hurray !";
            header('location:upload.php');
        }
        else{
        $_SESSION['error'] = "There was an error uploading your file, please try again !";
        header('location:upload.php');
        }
    } else {
        $_SESSION['error'] = "There was an error uploading your file, please try again !";
        header('location:upload.php');
    }
}
?>

非常感谢。我是 PHP 新手,所以如果您有任何其他提示,请随时告诉他们:)

编辑:文件实际上是从临时目录移动到目标目录,只是不会被重命名。

【问题讨论】:

    标签: php file post


    【解决方案1】:

    你可以替换

    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    

    $temps = time();
    $target_file = $target_dir . $temps . '.' . $imageFileType;
    

    以及底部的 if/else 语句

    if ($uploadOk == 0) {
        $_SESSION['error'] = "There was an error uploading your file, please try again !";
        header('location:upload.php');
    } else {
        if (!move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            $_SESSION['error'] = "There was an error uploading your file, please try again !";
            header('location:upload.php');
        }
    }
    

    完整的文件。

    <?php
    session_start();
    
    $target_dir = "uploads/";
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    $temps = time();
    $target_file = $target_dir . $temps . '.' . $imageFileType;
    
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) {
    
        } else {
            $_SESSION['error'] = "This is not an image -.-";
            header('location:upload.php');
        }
    }
    
    if ($_FILES["fileToUpload"]["size"] > 2000000) {
        $_SESSION['error'] = "The uploaded image must be smaller than 2MB";
        header('location:upload.php');
    }
    
    $allowed_extensions = array("jpg", "png", "jpeg", "gif");
    
    if(!in_array($imageFileType, $allowed_extensions)) {
        $_SESSION['error'] = "Sorry, only JPG, PNG & GIF files are allowed";
        header('location:upload.php');
    }
    
    if ($uploadOk == 0) {
        $_SESSION['error'] = "There was an error uploading your file, please try again !";
        header('location:upload.php');
    } else {
        if (!move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            $_SESSION['error'] = "There was an error uploading your file, please try again !";
            header('location:upload.php');
        }
    }
    ?>
    

    【讨论】:

    • 非常感谢,我的 if(对于扩展)似乎不起作用,有什么原因吗?它假设只是 jpeg、png 和 gif,但他让它们全部进入。//没关系,现在我了解了标题的工作原理,它们只应用于页面末尾。谢谢
    • @GabrielRoy 添加了对答案所做更改的完整文件,您可以测试一下吗?如果文件过滤仍然不起作用,您可以在问题中添加var_dump($imageFileType)
    【解决方案2】:

    文件名不能有“:”字符。尝试使用 time() 之类的其他方法,它会返回当前时间戳,或者只是将时间中的“:”字符更改为“-”。

    【讨论】:

    • 仅适用于 Windows。 UNIX 系统对包含 : 的文件名没有问题。
    【解决方案3】:

    您可以使用以下内容:-

      $arrExtension = @explode('.', $_FILES["fileToUpload"]["name"]);
      $fileName = 'prefix-'. time() . '.' . $arrExtension[1]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 2014-01-20
      • 1970-01-01
      • 2012-03-06
      • 2019-03-23
      • 1970-01-01
      • 2015-03-19
      相关资源
      最近更新 更多