用PHP重命名$ _FILES(文件)

问题描述 投票:0回答:3

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

  • 从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
3个回答
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');
    }
}
?>

0
投票

你可以用下面的东西: -

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

-1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.