. File has name picture_1.jpg. But I'd like to store it in filesystem with name like this ec0b4c5173809b6aa534631f3207.jpg? How such new names are created? Is some special script/generator used to make such names?
For example picture in FB: http:// a3.sphotos.ak.fbcdn.net/hphotos-ak-snc6/190074_10150167009837952_8062627951_8311631_4439729_n.jpg.
The name of it is 190074_10150167009837952_8062627951_8311631_4439729_n.jpg
. But original name was different for sure. So I'd like to change the name of uploaded file the same way. How is it possible?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
在 PHP 中,我使用 uniqid() 来命名我从上传中存储的文件
如果您想要一个额外长的或看似更随机的 ID,您可以 sha1() 或 <一个href="http://php.net/manual/en/function.md5.php" rel="nofollow">md5() 用于创建哈希的 uniqid。
您当然也可以使用这两种方法来创建文件名的哈希值。
例如,以下代码可用于为文件生成新名称
In PHP I use uniqid() for naming files that I store from uploads
If you want an extra long or seemingly more random id, you can sha1() or md5() the uniqid to create a hash.
You could of course also use those two methods to create a hash of the filename.
For example, the following code can be used to generate a new name for a file
您没有提到您正在使用什么编程语言。
在 PHP 中,可以在数组
$_FILES
中找到该神秘名称和原始文件名。假设您的表单元素名称是 userfile,您可以从
basename($_FILES['userfile']['tmp_name'])
获取该神秘名称,并从basename($_FILES) 获取原始名称['userfile']['name'])
请访问此处了解更多信息:
http://www.php.net/manual/en /features.file-upload.post-method.php
You didn't mention what programming language you are using.
in PHP, that cryptic name and original filename can be found on array
$_FILES
.let's assume your form's element name is userfile, you can get that cryptic name from
basename($_FILES['userfile']['tmp_name'])
and the original name frombasename($_FILES['userfile']['name'])
Visit here for more information :
http://www.php.net/manual/en/features.file-upload.post-method.php
您可以 MD5 名称来创建一个不带空格或点的字符串:
md5($filename)
(http://php.net/md5) 尽管我不确定这就是您所要求的?编辑:您还可以使用
uniqid()
(http://php.net/manual/en/function.uniqid.php)You could MD5 the name to create a string without spaces or dots:
md5($filename)
(http://php.net/md5) although I'm not sure that was what you were asking?Edit: you could also use
uniqid()
(http://php.net/manual/en/function.uniqid.php)您只需在将文件从服务器上的临时文件移动到您想要移动到的位置时重写文件名即可。
其中 &filePN 是要将文件移动到的位置的路径和名称。
然而,您所讨论的特殊脚本可以是多种内容,从输入名称的 MD5 哈希值到防止覆盖的递增数字。
You just rewrite the name of the file upon moving it from the temporary files on the server, to the location you want to move it to.
Where &filePN is the path and name of where you want to move the file to.
the special script that you're talking about, however, can be a multitude of things from an MD5 hash of the input name, to an incremented number to prevent overwrites.
看起来像 GUID。
如果您是 ASP.NET 程序员,请使用如下内容:
Looks like a GUID.
If you are an ASP.NET programmer, use something like this: