简体   繁体   English

Joomla 2.5:GD库-如何命名文件以避免冲突

[英]Joomla 2.5: GD Library - how to name the file to avoid conflict

I'm writing a component for Joomla 2.5 that generates a graph using the PHP GD Library depending on the users input. 我正在为Joomla 2.5编写一个组件,该组件使用PHP GD库根据用户输入生成图形。

What name should I give the image file to avoid conflict when multiple users are using the component. 我应该给图像文件起什么名字,以避免多个用户使用该组件时发生冲突。

you can generate random name with help of eg date and php hash function: http://www.php.net/manual/en/function.hash.php 您可以借助日期和PHP哈希函数生成随机名称: http : //www.php.net/manual/zh/function.hash.php

So the name will be unique. 因此名称将是唯一的。

Or, depends on used feature in your component, you can somehow add the user name to the image name. 或者,取决于组件中使用的功能,您可以以某种方式将用户名添加到映像名称。 It depends of the function of the component and on my factors (if there will be only one image per user, or per some other feature, etc.) 它取决于组件的功能和我的因素(如果每个用户或每个其他功能等只有一张图像,等等)

Jan 一月

In my opinion you have two way. 我认为您有两种方法。 The first is save the image with prefix like this 首先是使用这样的前缀保存图像

scotttiger-img12321323.jpg

where scotttiger is the username and 12321323 is the user input. 其中scotttiger是用户名, 12321323是用户输入。 Another way is to use different folder for every user 另一种方法是为每个用户使用不同的文件夹

scotttiger/1231321.jpg

But if You don't have the user logged in and doesn't know the username, do a simple check with isfile ( http://it1.php.net/is_file ) function and set incremental name. 但是,如果您没有用户登录并且不知道用户名,请使用isfile( http://it1.php.net/is_file )函数进行简单检查并设置增量名称。 eg: 例如:

$wrote = false;
$prog = 0;
$filename="img-{$user_input}-{$prog}.jpg";
while(isfile($filename)) {
   $prog++;
   $filename="img-{$user_input}-{$prog}.jpg";
}
write_file($filename);

So you have the first time img-input-1.jpg the second once img-input-2.jpg and so on. 因此,您第一次使用img-input-1.jpg,第二次使用img-input-2.jpg,依此类推。 With this solution You doesn't have duplicates or conflict. 使用此解决方案,您不会出现重复或冲突。

You can improve performance with this solution: 您可以使用以下解决方案提高性能:

$wrote = false;
$prog = md5(time());
$filename="img-{$user_input}-{$prog}.jpg";
while(isfile($filename)) {
   $prog = md5(time());
   $filename="img-{$user_input}-{$prog}.jpg";
}
write_file($filename);

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

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