php php随机图片文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13483439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:33:11  来源:igfitidea点击:

php random image file name

php

提问by bush man

Okay im using a snippet I found on google to take a users uploaded image and put it in my directory under Content

好的,我使用我在谷歌上找到的片段来获取用户上传的图片并将其放在我的目录下的目录中

But Im worried about duplicates so I was going have it upload the image as a Random number

但我担心重复,所以我打算将图像作为随机数上传

well here is my code you can probably understand what im going for through it anyways

好吧,这是我的代码,无论如何你都可以理解我的目的

<label for="file">Profile Pic:</label> <input type="file" name="ProfilePic" id="ProfilePic" /><br />

<input type="submit" name="submit" value="Submit" />

$ProfilePicName = $_FILES["ProfilePic"]["name"];
$ProfilePicType = $_FILES["ProfilePic"]["type"];
$ProfilePicSize = $_FILES["ProfilePic"]["size"];
$ProfilePicTemp = $_FILES["ProfilePic"]["tmp_name"];
$ProfilePicError = $_FILES["ProfilePic"]["error"];

$RandomAccountNumber = mt_rand(1, 99999);  
echo $RandomAccountNumber; 
move_uploaded_file($ProfilePicTemp, "Content/".$RandomAccountNumber.$ProfilePicType);

And then basicly after all this Im going try to get it to put that random number in my database

然后基本上在这一切之后我会尝试让它把那个随机数放在我的数据库中

Someone gave me a new snippet that looks like it will do what I want but now the file isnt making it all the way to my directory

有人给了我一个新的片段,看起来它会做我想要的,但现在文件并没有一直到我的目录

$RandomAccountNumber = uniqid();
echo $RandomAccountNumber;

move_uploaded_file($ProfilePicName,"Content/".$RandomAccountNumber);

回答by DiverseAndRemote.com

try using the php uniqidmethod to generate the unique id you need

尝试使用 phpuniqid方法生成您需要的唯一 id

http://php.net/manual/en/function.uniqid.php

http://php.net/manual/en/function.uniqid.php

$RandomAccountNumber = uniqid();
move_uploaded_file($ProfilePicTemp, "Content/" . $RandomAccountNumber);

回答by Madara's Ghost

When I upload images, I usually save it as the sha1()of the image's contents (sha1_file()). That way, you get two birds with one stone: You'll never (if you do, go fill out the closest lottery) get duplicate file names, AND, you'll prevent duplicate images (because duplicate images would have the same checksum).

当我上传图片时,我通常将其保存为sha1()图片内容 ( sha1_file())。那样的话,你一石二鸟:你永远不会(如果你这样做,去填写最接近的彩票)得到重复的文件名,并且,你将防止重复的图像(因为重复的图像将具有相同的校验和) .

Then, you have a database to sort out which image is which, and correctly display them to the user.

然后,您有一个数据库来整理哪个图像是哪个,并将它们正确地显示给用户。

回答by Frank

This is what I use when uploading pictures: a combination of session_id(), time()and a random string:

这是我上传图片时使用的:session_id()time()和随机字符串的组合:

$rand = genRandomString();
$final_filename = $rand."_".session_id()."_".time();

function genRandomString() 
{
    $length = 5;
    $characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWZYZ";

    $real_string_length = strlen($characters) ;     
    $string="id";

    for ($p = 0; $p < $length; $p++) 
    {
        $string .= $characters[mt_rand(0, $real_string_length-1)];
    }

    return strtolower($string);
}

I hope this help.

我希望这会有所帮助。

回答by Sammitch

random != unique

random != unique

No matter what method you use to generate a 'random' file name you're probably going to want to do this to avoid collisions.

无论您使用什么方法生成“随机”文件名,您都可能希望这样做以避免冲突。

$path = '/path/to/directory/';
do {
    $filename = some_function();
} while( file_exists($path.$filename) );

It's not super-necessary, but if you're just looking for peace of mind in case of a one-in-a-million filename collision then those couple extra lines will do the trick.

这不是必需的,但如果您只是想在百万分之一的文件名冲突的情况下安心,那么这几行额外的行就可以解决问题。

回答by Dean Rather

One of my favorite Coding Horror articlesaddresses why this approach is dumber than it looks, and you should use something like uniqidinstead of mt_rand(1, 99999);...

我最喜欢的Coding Horror 文章之一阐述了为什么这种方法比看起来更笨,你应该使用类似的东西uniqid而不是mt_rand(1, 99999);......