用PHP重命名$ _FILES(文件)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用PHP重命名$ _FILES(文件)相关的知识,希望对你有一定的参考价值。

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

  • 从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的新手,所以如果您有任何其他提示,请随时告诉他们:)

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

答案

你可以替换

$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');
    }
}
?>
另一答案

你可以用下面的东西: -

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

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

以上是关于用PHP重命名$ _FILES(文件)的主要内容,如果未能解决你的问题,请参考以下文章

C#实现文件批量重命名源码下载

重命名功能不适用于 php 中的文件夹

用php重命名文件夹中的重复文件

php检查文件名是不是存在,重命名文件

xml用php解析是重命名php文件吗

在php中重命名文件夹中所有图像的最佳方法是啥?