【问题标题】:php random image file namephp 随机图片文件名
【发布时间】:2012-11-09 02:59:07
【问题描述】:

好的,我使用我在 google 上找到的 sn-p 来获取用户上传的图像并将其放在我的目录中的 Content 下

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

这里是我的代码,你大概可以理解我通过它的目的

<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);

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

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

$RandomAccountNumber = uniqid();
echo $RandomAccountNumber;

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

【问题讨论】:

  • 是不是有问题隐藏在某处?
  • 在将图像保存到没有重复 ID 的目录之前尝试定义图像?

标签: php


【解决方案1】:

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

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

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

【讨论】:

  • 哈哈谢谢你让我试着让它工作,如果我有任何问题,请给你留言
  • hmmm 我把它改成了这个 $RandomAccountNumber = uniqid();回声 $RandomAccountNumber; move_uploaded_file($ProfilePicName,"Content/".$RandomAccountNumber);但是现在文件没有进入目录文件??
  • @bushman,.$ProfilePicType 怎么了?你删除了它?还要确保您在该目录中具有写入权限
  • @bushman,我认为您需要将其保留为move_uploaded_file($ProfilePicTemp, "Content/".$RandomAccountNumber.$ProfilePicType);,因为您需要使用$ProfilePicTemp查找文件
  • hmm naw 我用了你所说的,我得到了这些错误警告:move_uploaded_file(Content/50ac1204a6921image/jpeg) [function.move-uploaded-file]:无法打开流:没有这样的文件或目录在 /home/content/34/9587634/html/MYDOMAIN.COM/Upload.php 的第 208 行警告:move_uploaded_file() [function.move-uploaded-file]: Unable to move '/home/content/34/9587634/ tmp/phpPp3mDX' 到第 208 行 /home/content/34/9587634/html/MYDOMAIN.Com/Upload.php 中的 'Content/50ac1204a6921image/jpeg'
【解决方案2】:

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

然后,你就有了一个数据库来分清哪个是哪个图像,并正确地显示给用户。

【讨论】:

  • 我认为这种方法会很糟糕。如果两个用户上传了同一个 LOL 猫,然后一个用户删除了它怎么办
  • @OmarJackman:当然,这完全取决于使用情况,您可以通过添加前缀或将它们分组到文件夹中来使文件号特定于用户。
  • @MadaraUchiha 你使用sha1 还是sha1_file?只是好奇
  • @mmmshuddup:我使用sha1_file()。许多人(出于某种奇怪的原因)喜欢使用sha1(file_get_contents())
  • @OmarJackman:唯一 ID 是随机的,这不是。当我有一个画廊项目来存储数十万张图像时,我使用了这个解决方案。您需要某种系统来实现它,否则您将永远无法保留该代码。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    random != unique

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

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

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

    【讨论】:

    【解决方案5】:

    这是我上传图片时使用的: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);
    }
    

    希望对你有所帮助。

    【讨论】:

    • 由于会话 id 可以包含逗号,使用这种方法在文件名中使用逗号会有什么问题吗?
    猜你喜欢
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多