SitePoint Sponsor |
|
User Tag List
Results 1 to 14 of 14
Thread: generate unique file name
-
Aug 23, 2001, 12:06 #1
- Join Date
- Jun 2001
- Location
- Thailand
- Posts
- 369
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
generate unique file name
Hi, Is any way to generate unique file name with PHP because my site allow member upload picture,filename in own folder
User may upload same filename so, I want PHP rename filename that unique like primarykey 1,2,3,4,5... (integer in mysql ) such as picture00001, picture00002, and so on..
please show code.
-
Aug 23, 2001, 12:45 #2
- Join Date
- Dec 2000
- Location
- Idaho, USA
- Posts
- 452
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You can try something like this perhaps:
PHP Code:$upload_dir = "/home/usr/www/pics";
$handle=opendir($upload_dir);
while ($file = readdir($handle)){
$files[count($files)] = $file;
}
closedir($handle);
rsort($files);
foreach($files AS $highest){
if(!$i){
$data = $highest;
$i++;
}
}
$data = eregi_replace("[\\.a-z]","", $data);
$data++;
$newfilename = "picture" . $data . ".gif";
Last edited by exbabylon; Aug 23, 2001 at 12:54.
Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.
Exbabylon- Professional Internet Services
-
Aug 23, 2001, 14:35 #3
- Join Date
- Jun 2001
- Location
- Thailand
- Posts
- 369
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks exbabylon but it don't work,
I use your code and change only $upload_dir and $data to my file name
First file that I upload it generate file 1 (no extension not 1.gif) and when I upload secode file or other file it save as 1 and replace with old file so although I upload many file it still file 1 and only lastest one file
Can you correct plase? and explain your code because it don't understand
-
Aug 23, 2001, 14:59 #4
- Join Date
- Apr 2001
- Location
- Des Moines, IA
- Posts
- 346
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Try basing your filename on time().
-
Aug 23, 2001, 15:06 #5
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You could do what abstraction said and it would work good, but then again I just spent 20 minutes making something that would work with the way you want it done.
PHP Code:<?
$allowed_types = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
$uploaddir = "C:/wwwroot/uploads/";
if($submit) {
//Grab all the files in your
//direcotry and put them in an array
$d = dir($uploaddir);
while($f = $d->read()) {
if(($f != '.') && ($f != '..')) {
$files[] = $f;
}
}
//Make sure there already some images in there
if(count($files)) {
//Reverse sort the files array since the highest number will be last.
rsort($files);
//Explode the highest one by a period.
$tmp = explode(".", $files[0]);
//Use regex to gett he numbers out of the filename and store in $args[1]
ereg("([0-9]+)", $tmp[0], $args);
//Cast the number retreived from regex to int
$num = (int)$args[1];
//Create a number with 5 decimal points using the next number up from the last file.
$num = sprintf("%05d", trim(++$num));
//Create the filename
$filename = "picture$num.gif";
}
//If there are no images make the filename 00001;
else {
$filename = "picture00001.gif";
}
//Make sure a file with something in it was uploaded.
if($userfile_size > 0) {
//make sure the file type is in the allowed type,
//yo ucan remove this if you want.
if(in_array($userfile_type, $allowed_types)) {
//Copy the file
if(copy($userfile, $uploaddir.$filename)) {
$error = "Your image was uploaded successfully<br>";
}
else {
$error = "Error uploading image<br>";
}
}
else {
$error = "Please upload only images<br>";
}
}
else {
$error = "Please choose a file to upload before clicking submit<br />";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?=$error?>
<form action="<?=$PHP_SELF?>" method="post" enctype="multipart/form-data">
File 1<input type="file" name="userfile"><br>
<input type="submit" name="submit" value="Upload File">
</form>
</body>
</html>Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Aug 23, 2001, 15:32 #6
- Join Date
- Dec 2000
- Location
- Idaho, USA
- Posts
- 452
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Sorry. I hadn't tested the code, merely trying to give you a starting point. But looks like freddy did an EXCELLENT job of fixing my flaws....
Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.
Exbabylon- Professional Internet Services
-
Aug 23, 2001, 15:48 #7
- Join Date
- Jun 2001
- Location
- Thailand
- Posts
- 369
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thank you very much freddydoesphp
your code work very well and best.
but I have little question plese?
1. In the step Grab all the files in direcotry and put them in an array and other check
PHP Code:if(count($files))
{
............
}
2. from 1. If I have 2000 picture Should I have to separate to 2 folder for quick access
Thanks againI live in Thailand. My English grammar not well.
-
Aug 23, 2001, 15:51 #8
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If you are talking about 2000+ files why don't you just use Abstraction's solution use time() as your file name.
You could also store a counter somewhere thats gets incremented everytime you upload a file. Like a file or a db.
My solution will work for small dirs, but with 2000+ items its gonna run a little slow most likely. And innefficient, do you absolutely need to have the file names in that format?Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Aug 23, 2001, 16:24 #9
- Join Date
- Jun 2001
- Location
- Thailand
- Posts
- 369
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
My site keep picture of article, news. I thinks picture increase everyday that member add news or articles that contain picture.
I don't understand concept to use Abstraction's solution use time() apply with my question? Can you explain me ?I live in Thailand. My English grammar not well.
-
Aug 23, 2001, 16:28 #10
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well time() would create a unique number that never will be the same. time() measures the number of seconds since the unix epoch Jan 1, 1970(i think). Anyways since its measured in seconds, the number changes every second. If you named your files like
PHP Code:$filename = time().".gif";
Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Aug 23, 2001, 16:42 #11
- Join Date
- Jun 2001
- Location
- Thailand
- Posts
- 369
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thank you very much again.
Thanks.
I live in Thailand. My English grammar not well.
-
Aug 23, 2001, 16:53 #12
- Join Date
- Dec 2000
- Location
- Idaho, USA
- Posts
- 452
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
OR
PHP Code:$filename = "/home/usr/www/filename_num.txt";
$fd = fopen($filename, "r");
$contents = fread($fd, filesize($filename));
fclose($fd);
$contents++;
$file_pointer = fopen($filename, "w");
fwrite($file_pointer, "$contents");
fclose($file_pointer);
$filename = $contents++;
Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.
Exbabylon- Professional Internet Services
-
Aug 23, 2001, 17:00 #13
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You don't need to open the file twice for this, sinmply open it for read and write then read it in place the file pointer at the start of the file and write an incremented number back to it.
PHP Code:<?
$fp = fopen("foo.txt", "r+");
$data = fgets($fp,128);
fseek($fp,0);
fputs($fp, ++$data);
fclose($fp);
?>Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Aug 23, 2001, 17:21 #14
- Join Date
- Dec 2000
- Location
- Idaho, USA
- Posts
- 452
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
my week isn't going so well... thanx freddy, maybe I'll just keep shut and let you answer...
Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.
Exbabylon- Professional Internet Services
Bookmarks