在php中按数字重命名图像

时间:2013-03-09 06:12:44

标签: php

我正在尝试创建一个将头像上传到我的网站的选项。我想要实现的是:

first avatar : 1.jpg
second avatar : 2.jpg
third avatar : 3.png 
and so on.. 

如何在php中创建上传计数器?我目前的代码是:

if(!empty($_FILES['cover']['tmp_name']))
{
    $uploadfolder =  "avatar/"; 
    $file1 = rands().'.'.end(explode('.',$_FILES['cover']['name'])); 
    $cover = $uploadfolder.$file1;
    move_uploaded_file($_FILES['cover']['tmp_name'],$cover);
}
else 
{
    $cover = ''
}

函数rands()没有做任何事情,所以请用它来演示如何实现我的目标。

5 个答案:

答案 0 :(得分:1)

如果您将用户存储在数据库中并且存在整数用户ID,则最好使用此用户ID进行文件命名,而不是使用单独的递增计数器。

此外,您可以查看现有文件以查找最大现有数字,如下所示:

function getNextFileName ()
{
    $a = 0;
    $b = 2147483647;

    while ($a < $b)
    {
        $c = floor (($a + $b) / 2);
        if (file_exists ("$c.jpg")) $a = $c + 1;
        else $b = $c;
    }

    return "$a.jpg";
}

function saveAvatar ($avatar)
{
    for ($i = 0; $i < 16; $i++)
    {
        $name = getNextFileName ();
        $fd = fopen ($name, 'x');
        if ($fd !== FALSE)
        {
            fwrite ($fd, $avatar);
            fclose ($fd);
            return $name;
        }
    }
    return FALSE;
}

for ($i = 0; $i < 20; $i++)
    saveAvatar ("BlahBlahBlah$i");

答案 1 :(得分:1)

您可以尝试使用随机数生成问题:

  $prefix = substr(str_shuffle("0123456789"), 0,3);
  $file1 = $prefix.'.'.end(explode('.',$_FILES['cover']['name']));  

上面的$前缀将是:任意随机3位

希望会有所帮助!

答案 2 :(得分:1)

    /*
     * currentDir - path - eg. c:/xampp/htdocs/movies/uploads (no end slash)
     * $dir - points current working directory.
     * $filename - name of the file.
 */

public static function getFileName($dir, $filename) {

    $filePath = $dir . "/uploads/" . $filename;
    $fileInfo = pathinfo($filePath);
    $i = 1;
    while(file_exists($filePath)) {
        $filePath = $dir . "/uploads/" . $fileInfo['filename'] . "_" . $i . "." . $fileInfo['extension'];
        $i++;

    }
    return $filePath;
}

move_uploaded_file($_FILES['cover']['tmp_name'],$filePath);

如果您的uploads文件夹中存在相同的文件名。它会自动生成

avatar_1.jpg, avatar_2.jpg, avatar_3.jpg,ans等......

答案 3 :(得分:0)

创建一个XML文件并将计数存储在那里

<? xml version="1.0" ?>
<MyRootNode>
  <count>123</count>
</MyRootNode>

更新

在您的问题中添加了更多代码后添加了更新。

Function Rands(FileExtension as string) as long
  '1 open xml file 
  '2 read counter in
  '3 increment counter
  '4 save value to back xml file
  '5 return incremented counter with the file extension passed attached on the end
  'This is in case a BMP GIF or PNG has been and not JPG
  ' SAMPLE filename 123.GIF
End Function

答案 4 :(得分:0)

如果(这是一个很大的IF)你的服务器支持文件锁定,你可以合理地确保你有一个唯一的递增ID:

function get_avatar_id()
{
    $lockfile = fopen("avatar_id_lock_file","a");
    if(flock($lockfile, LOCK_EX)) // Get an exclusive lock to avoid race conditions
    {
        $avatar_id = intval(file_get_contents("avatar_id"); // Assumes you made it and put a number in it
        $avatar_id++;
        file_put_contents("avatar_id", $avatar_id);
        flock($lockfile, LOCK_UN);
        fclose($lockfile);
        return $avatar_id;
    }
    else
    {
        //What do you want to do if you can't lock the file?
    }
}
相关问题