简体   繁体   English

PHP自定义文件名

[英]Php custom name to files

How can I make a custom name to the pictures they/I upload, preferably I want them to get numbers as names, and the next picture get the number of the last name +1.我如何为他们/我上传的图片自定义名称,最好我希望他们获得数字作为名称,而下一张图片获得姓氏+1的数字。

<?php
    $target_dir = "../image/DBFilmCover/";

    $target_file = $target_dir . basename($_FILES["fileToUpload"]["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;
        }
    } else {

        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            echo "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been      uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
?>

I appreciate all the help I can get, if I didn't give enough information, just ask我感谢我能得到的所有帮助,如果我没有提供足够的信息,请问

Yes you can.是的你可以。 Just change target_file according to your need:只需根据需要更改target_file

Eg:例如:

$target_file = $target_dir . "custom name";

For counting number of files in your directory simply do:要计算目录中的文件数,只需执行以下操作:

$files = scandir($target_dir);
$files_count = count($files)-2; // -2 for ignoring . and ..

Now filename would be:现在文件名将是:

$path = $_FILES['fileToUpload']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
$files_count++;
$filename = $files_count.".".$ext;
$target_file = $target_dir . $filename;

you can use from following algorithm您可以使用以下算法

  1. create txt file and save 0 on that创建txt文件并保存0
  2. in the uploading process get txt file content and +1 that在上传过程中获取txt文件内容和+1
  3. save file which new name保存文件的新名称
  4. and save new value(+1) to txt file for future.并将新值(+1)保存到 txt 文件以备将来使用。

Also you can save that on database您也可以将其保存在数据库中

Because each time the script is run it doesn't know anything about the last time it was run you would need to store the value / number assigned to the previous uploaded image somewhere.因为每次运行脚本时,它都不知道上次运行时的任何信息,所以您需要将分配给上一个上传图像的值/编号存储在某处。

You could store it in a text file or in a database and then increment it by one each time the script is run.您可以将其存储在文本文件或数据库中,然后每次运行脚本时将其加一。

An easier 'hack' might be to use a data/time stamp something like:更简单的“黑客”可能是使用数据/时间戳,例如:

$filename = 'image_' . date('Y-m-d_H-i-s');

For getting the next available number for the new image, you might use something as the following:要获取新图像的下一个可用编号,您可以使用以下内容:

$last_number = 1;
foreach (glob('../image/DBFilmCover/*.{png,jpg,gif}', GLOB_BRACE) as $name) {
    $number = intval(pathinfo($name, PATHINFO_FILENAME), 10);
    if ($number > $last_number) {
        $last_number = $number;
    }
}
$number++;

$ext = pathinfo($_FILES["fileToUpload"]["tmp_name"], PATHINFO_EXTENSION);
$target_file = "../image/DBFilmCover/$number.$ext";

And then proceed with your move_uploaded_file code.然后继续您的move_uploaded_file代码。

Be aware that the PATHINFO_FILENAME option was added in PHP 5.2.0.请注意PATHINFO_FILENAME选项是在 PHP 5.2.0 中添加的。

Edit: Also be aware that this method (and also methods such as remembering the last filename in a text file or database or using the time stamp as file name) is prone to concurrancy issues if more than one user might upload a file at the same time.编辑:另请注意,如果多个用户可能同时上传文件,则此方法(以及记住文本文件或数据库中的最后一个文件名或使用时间戳作为文件名的方法)容易出现并发问题时间。 To overcome this, you might want to add some random string to the file name as well.为了克服这个问题,您可能还想在文件名中添加一些随机字符串。 But this is not an issue if you are the single user for uploading.但是,如果您是上传的单个用户,这不是问题。

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

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