The sequence number for $filename

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MaxSoon
Forum Newbie
Posts: 2
Joined: Wed Oct 16, 2013 11:05 pm

The sequence number for $filename

Post by MaxSoon »

Hi everyone, in the last few days, I tried to used Flash and PHP to make a program that can take photos with webcam and upload the photos into server folder.

Inside my php code "$filename = "images1/visualstudio". mktime(). ".jpg";" is the reference I found online, I understand the "mktime()" was generating the combination of date and time,but what I really need is when the photos that upload to folder will be name with sequence number like: "visualstudio01.jpg","visualstudio02.jpg","visualstudio03.jpg"...

Because this is the first time I deal with PHP, from my logic, the code should be "$filename = "images/visualstudio".SEQUENCE NUMBER CODE.".jpg";", but the problem is I don't know how the "SEQUENCE NUMBER CODE" structure should look like, can anyone give me some suggestion or some references?


And here is my php code:
---------------------------------------------------------------------------------------

Code: Select all

[/b]
---------------------------------------------------------------------------------------
<?php

//Now get the encoded image form flash through HTTP_RAW_POST_DATA
if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
	$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
	$img = $_GET["img"];
	
	//image Directory
	$filename = "images1/visualstudio". mktime(). ".jpg";
	file_put_contents($filename, $jpg);
} else{
	//show error if image is not recived 
	echo "Encoded JPEG information not received.";
}
?>
---------------------------------------------------------------------------------------
[b][/PHP CODE][/b]
---------------------------------------------------------------------------------------

Thanks for your time !!  :)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: The sequence number for $filename

Post by social_experiment »

will the sequence values always be numeric?

you would have to find the file that was modified last so you can generate the next number within a sequence. perhaps use filemtime() to determine the last file modified and then move onto using a regular expression to retrieve the numeric value from the filename. Once you have that you can modify it accordingly and rename your new file.

hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
MaxSoon
Forum Newbie
Posts: 2
Joined: Wed Oct 16, 2013 11:05 pm

Re: The sequence number for $filename

Post by MaxSoon »

According your suggestion, I had tried to refined my code into this:


---------------------------------------------------------------------------------------
[/PHP CODE]
---------------------------------------------------------------------------------------


<?php

//Now get the encoded image form flash through HTTP_RAW_POST_DATA
if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
$img = $_GET["img"];

//image Directory
$filename = "images1/visualstudio". filemtime(). ".jpg";
file_put_contents($filename, $jpg);
} else{
//show error if image is not recived
echo "Encoded JPEG information not received.";
}
?>


---------------------------------------------------------------------------------------
[/PHP CODE]
---------------------------------------------------------------------------------------

But the files that uploaded into the folder only showed "visualstudio.jpg" and keep overwrite the previous one. How can i prevent the uploaded files from overwriting the previous one?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: The sequence number for $filename

Post by social_experiment »

Code: Select all

<?php
$filename = "images1/visualstudio". filemtime(). ".jpg";
?>
^ this is actually incorrect usage for the function; it checks the modification date of the filename in question, using it there won't work.

Something easier you could try is to loop through the directory and count the number of files, create the next value in the sequence and then change the filename
readdir()

:idea: the PHP Code button will wrap your code in php tags, it helps with reading the code :)

Edit
Leaving for work soon but i'll look at this problem a bit later ITO actual code
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: The sequence number for $filename

Post by Celauran »

Naming files sequentially based on count is going to cause headaches; what if you have files 1 - 7 then delete file2? You'll count 6 files, name the next one file7 and have a collision. What about using glob() to get an array of files in the directory, determine the largest file name based on that array, and increment from there? Alternately, you could simply persist the counter somewhere.
tazz24mania
Forum Newbie
Posts: 10
Joined: Tue Oct 15, 2013 2:53 pm
Location: Newcastle Upon Tyne

Re: The sequence number for $filename

Post by tazz24mania »

rather than looking through the directory and counting files etc.... would it not be easier to use the time() function in place of filetime(). That way its guarenteed top have a unique number in that place.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: The sequence number for $filename

Post by Celauran »

Race conditions could still cause file name collisions but, that aside, OP specifically stated he wants incrementing numbers.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: The sequence number for $filename

Post by Christopher »

I would recommend using a database to generate the sequence numbers. Some databases have sequences. You can emulate them in ones that don't. You could also use a file, but a database would be better.
(#10850)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: The sequence number for $filename

Post by social_experiment »

Celauran wrote:Naming files sequentially based on count is going to cause headaches;
good point, i didn't take that into account initially;
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply