-2

Possible Duplicate:
Unique key generation

How to automaticly make a new name of file when you upload it to server?

I upload a picture file with < input type="file" / >. 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?

Community
  • 1
  • 1
Green
  • 28,742
  • 61
  • 158
  • 247
  • Your question is very unspecific. It would be helpful, to provide additional information and rephrase your question, like: "I want to do XYZ and keep track of the uploaded files. But i don't know how , because if i upload a file with a given name, the names get changed when stored on the server. I'm using XYZ as CMS/WebFramework and i provided a short log/transcribed below: ..." – Don Question Dec 24 '11 at 22:57

5 Answers5

1

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

$file = 'somefile.jpg';
$new_name = uniqid();
$new_name .= '.'.pathinfo($file,PATHINFO_EXTENSION);
Alex Coplan
  • 13,211
  • 19
  • 77
  • 138
1

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 from basename($_FILES['userfile']['name'])

Visit here for more information : http://www.php.net/manual/en/features.file-upload.post-method.php

Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
Remo Harsono
  • 469
  • 9
  • 15
0

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)

powerbuoy
  • 12,460
  • 7
  • 48
  • 78
  • 1
    In some situations this would be a good approach; however, if two images had the same local filename (but uploaded by different users, for example) this would be problematic. I think the OP is looking for a function to create unique filenames on the server, regardless of their original, local filename. – Logan Serman Dec 24 '11 at 22:58
  • What will happen if an user uploads a pic named me.jpg, and another one uploads another picture with the same name? Of course md5 can be used but not like that. – Bakudan Dec 24 '11 at 22:59
  • I was unsure whether he wanted a method of creating "safe" filenames or a way to generate unique ones, hence my edited reply. If you _did_ use `md5()` you would obviously need to make sure they're not already in use. – powerbuoy Dec 24 '11 at 23:02
0

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.

move_uploaded_file($_FILES['userfile']['tmp_name'], $filePN)

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.

Kodlee Yin
  • 1,089
  • 5
  • 10
-1

Looks like a GUID.

If you are an ASP.NET programmer, use something like this:

string filename = new Guid().ToString() + ".jpg";
savraska
  • 1
  • 4