Reply
Auto incrementing file name
Old 10-20-2009, 02:49 PM Auto incrementing file name
Experienced Talker

Posts: 39
Name: Craig
Trades: 0
Hello,

I have a script that uploads photos to a directory. I am trying to rename the file on the fly with an incrementing value, i.e. 0001.jpg, 0002.jpg. I can not figure out for the life of me how to do it. Usually I would use rand(), but I need the file name to increase incrementally, preferably with the date prefixed. The code I have now just outputs a file named "11.png" every time. Can anyone point out what the problem is? Maybe a solution, also... Thanks!

PHP Code:
$file_name=date('m-d-y');
$file_int=$file_name 1;
$image_name=$file_int.'.'.$extension;
$newname="images/".$image_name
werm82 is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 10-20-2009, 03:07 PM Re: Auto incrementing file name
Average Talker

Posts: 23
Location: Germany
Trades: 0
Basically, your code could look like this:
PHP Code:
$index=yourFunctionToGetTheCurrentValue();

$file_name=date('m-d-y');
$file_int=$file_name.'_'.$index;
$image_name=$file_int.'.'.$extension;
$newname="images/".$image_name;

yourFunctionToWriteDownTheNewValue($index+1); 
You could store the current value in an extern file for example. yourFunctionToGetTheCurrentValue() would be a function that opens this extern file and reads this value.
__________________
|MuscularBrain.com - Psychology, Philosophy and Self Development
|Criticizer.net - All About the Internet and Programming
Trey Walter is offline
Reply With Quote
View Public Profile Visit Trey Walter's homepage!
 
Old 10-20-2009, 03:16 PM Re: Auto incrementing file name
NullPointer's Avatar
Will Code for Food

Latest Blog Post:
Easy PHP Search with Opera
Posts: 1,069
Name: Matt
Location: Irvine, CA
Trades: 0
Date returns a string, not a number.
PHP Code:
$file_int=$file_name 1//invalid 
The best approach would probably be to store the current count somewhere and increment it every time you add an image:
PHP Code:
$count//the current image count
$filename 'images/' date('m-d-y') . $count '.' .$extension;
$count++;
//store the new value of count 
You'll have to figure out how to store $count.
__________________
Tinsology | How to Post Code | EverythingDev
NullPointer is online now
Reply With Quote
View Public Profile
 
Old 10-21-2009, 12:31 PM Re: Auto incrementing file name
mtishetsky's Avatar
King Spam Talker

Posts: 1,166
Name: Mike
Location: Mataro, Spain
Trades: 0
http://www.php.net/glob
__________________
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 10-21-2009, 06:43 PM Re: Auto incrementing file name
Junior Talker

Posts: 1
Trades: 0
Hello,
The solution that Trey Walter gived to you is a good one. The rest of the problem with the yourFunctionToGetTheCurrentValue() can be found here:
http://www.alfafiles.com/php/auto-in...thout-mysql/45

In order for this to work you will have either to put the date after the index number or to format the date in an alphabetical incrementing way ( y/m/d ).
Mipet is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Auto incrementing file name
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.11904 seconds with 13 queries