使用唯一文件名通过 php 上传图像

2022-01-09 00:00:00 unique upload php cordova filenames

我目前正在借助 phonegap 编写移动应用程序.我希望这个应用程序具有的少数功能之一是能够捕获图像并将其上传到远程服务器......

I am currently in the process of writing a mobile app with the help of phonegap. One of the few features that I would like this app to have is the ability to capture an image and upload it to a remote server...

我目前的图像捕获和上传/发送电子邮件部分可以使用已编译的 apk 正常工作...但在我的 php 中,我目前将图像命名为图像 [插入从 10 到 20 的随机数]...问题这里是数字可以重复并且图像可以被覆盖......我已经阅读并考虑过只使用rand()并从0到getrandmax()中选择一个随机数,但我觉得我可能有同样的机会文件覆盖...我需要每次都以唯一的名称将图像上传到服务器,无论如何...所以 php 脚本将检查服务器已有的内容并写入/上传图像具有唯一的名称...

I currently have the image capturing and uploading/emailing portion working fine with a compiled apk... but in my php, I am currently naming the images "image[insert random number from 10 to 20]... The problem here is that the numbers can be repeated and the images can be overwritten... I have read and thought about just using rand() and selecting a random number from 0 to getrandmax(), but i feel that I might have the same chance of a file overwriting... I need the image to be uploaded to the server with a unique name every-time, no matter what... so the php script would check to see what the server already has and write/upload the image with a unique name...

除了rand()"还有什么想法吗?

any ideas other than "rand()"?

我也在考虑可能为每张图片命名...img + 日期 + 时间 + 随机 5 个字符,其中包括字母和数字...所以如果一张图片是在 3 月凌晨 4:37 使用应用程序拍摄的2013 年 2 月 20 日,当上传到服务器时,图像将被命名为img_03-20-13_4-37am_e4r29.jpg"......我认为这可能有效......(除非有更好的方法)但我相当新到 php 并且不明白如何写这样的东西......

I was also thinking about maybe naming each image... img + date + time + random 5 characters, which would include letters and numbers... so if an image were taken using the app at 4:37 am on March 20, 2013, the image would be named something like "img_03-20-13_4-37am_e4r29.jpg" when uploaded to the server... I think that might work... (unless theres a better way) but i am fairly new to php and wouldn't understand how to write something like that...

我的php如下...

print_r($_FILES);
$new_image_name = "image".rand(10, 20).".jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "/home/virtual/domain.com/public_html/upload/".$new_image_name);

感谢任何帮助...提前致谢!另外,如果有任何进一步的信息我可能会遗漏,请告诉我......

Any help is appreciated... Thanks in advance! Also, Please let me know if there is any further info I may be leaving out...

推荐答案

你可能要考虑 PHP 的 uniqid() 函数.这样,您建议的代码将如下所示:

You may want to consider the PHP's uniqid() function. This way the code you suggested would look like the following:

$new_image_name = 'image_' . date('Y-m-d-H-i-s') . '_' . uniqid() . '.jpg';
// do some checks to make sure the file you have is an image and if you can trust it
move_uploaded_file($_FILES["file"]["tmp_name"], "/home/virtual/domain.com/public_html/upload/".$new_image_name);

另外请记住,您的服务器的随机函数并不是真正随机的.如果您确实需要随机的东西,请尝试 random.org.随机随机随机.

Also keep in mind that your server's random functions are not really random. Try random.org if you need something indeed random. Random random random.

UPD:为了在您的代码中使用 random.org,您必须向他们的服务器发出一些 API 请求.相关文档可在此处获得:www.random.org/clients/http/.

UPD: In order to use random.org from within your code, you'll have to do some API requests to their servers. The documentation on that is available here: www.random.org/clients/http/.

调用示例为:random.org/integers/?num=1&min=1&max=1000000000&col=1&base=10&format=plain&rnd=new.请注意,您可以更改 minmax 和其他参数,如 文档.

The example of the call would be: random.org/integers/?num=1&min=1&max=1000000000&col=1&base=10&format=plain&rnd=new. Note that you can change the min, max and the other parameters, as described in the documentation.

在 PHP 中,您可以使用 GET 请求.php" rel="noreferrer">file_get_contents() 函数,cURL 库,甚至是套接字.如果您使用的是共享主机,您的帐户应该可以使用并启用传出连接.

In PHP you can do a GET request to a remote server using the file_get_contents() function, the cURL library, or even sockets. If you're using a shared hosting, the outgoing connections should be available and enabled for your account.

$random_int = file_get_contents('http://www.random.org/integers/?num=1&min=1&max=1000000000&col=1&base=10&format=plain&rnd=new');
var_dump($random_int);

相关文章