简体   繁体   English

PHP文件上传,重命名文件名

[英]PHP file upload, rename file name

Main goal is to upload files with a unique file_name every time. 主要目标是每次都上传具有唯一file_name文件。 This file will sit on our webserver and the file_path will be mapped to our database. 该文件将位于我们的Web服务器上,并且file_path将被映射到我们的数据库。 Then it will be displayed on a post from the file path stored in the database. 然后,它将显示在数据库中存储的文件路径中的帖子中。

I am using the code from here ( http://www.w3schools.com/php/php_file_upload.asp ) 我正在从这里使用代码( http://www.w3schools.com/php/php_file_upload.asp

I need to rename the files that are being uploaded to a auto incremental integer. 我需要将要上传的文件重命名为自动增量整数。 So the first file uploaded will be 1.file_extention , then the next will be 2.file_extention , and so... 因此,上传的第一个文件将是1.file_extention ,然后下一个将是2.file_extention ,依此2.file_extention ...

What is the best way to do this? 做这个的最好方式是什么?

If you are really set on using incremental numbers (probably not the best way) and you will be mapping these files in the db anyway, why not do the db insert first (I'm assuming your table has an auto-increment id field) and then use the id field from the row you inserted as the name of the file? 如果您真的设置增量数字(可能不是最好的方法),并且无论如何都将这些文件映射到数据库中,为什么不首先插入数据库(我假设您的表具有自动增量ID字段)然后使用您插入的行中的id字段作为文件名?

The caveat is that, if renaming the file using the id from the db fails for some reason, you'll need to remove that row but, any time you use such a mapping, you'll need to take that kind of care. 需要注意的是,如果由于某种原因使用数据库中的ID重命名文件失败,则需要删除该行,但是,每次使用这种映射时,都需要采取这种措施。

Hi you can prefix time stamp using below method 嗨,您可以使用以下方法为时间戳添加前缀

<?php
$target_dir = "uploads/";
$target_file = $target_dir . time().basename($_FILES["fileToUpload"]["name"]); // NOTICE THAT I CONCORDINATED TIME STAMP BEFORE THE FILE NAME
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

If you want to concordinate the primary key you have to insert the record first then to upload the file. 如果要协调主键,则必须先插入记录,然后再上传文件。 If file not uploaded then you have to throw a warning to user that the image not uploaded or have to delete the record. 如果文件未上传,则您必须向用户发出警告,该图像未上传或必须删除记录。

<?php
    require 'dbConnection.php';

    $stmt = "INSERT INTO TABLENAME (id, name, created) VALUES (NULL, '$name', NOW())";
    $qry = mysql_query($stmt);

    $lastInsertedId = mysql_insert_id();


    $target_dir = "uploads/";
    $target_file = $target_dir . $lastInsertedId.'_'.basename($_FILES["fileToUpload"]["name"]); // NOTICE THAT I CONCORDINATED TIME STAMP BEFORE THE FILE NAME
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
    }

?>

If you are using PDO connection then you have to modify slightly. 如果使用的是PDO连接,则必须稍作修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM