Page 1 of 1

The sequence number for $filename

Posted: Wed Oct 16, 2013 11:17 pm
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 !!  :)

Re: The sequence number for $filename

Posted: Thu Oct 17, 2013 2:46 am
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

Re: The sequence number for $filename

Posted: Thu Oct 17, 2013 3:59 am
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?

Re: The sequence number for $filename

Posted: Thu Oct 17, 2013 4:16 am
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

Re: The sequence number for $filename

Posted: Thu Oct 17, 2013 5:54 am
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.

Re: The sequence number for $filename

Posted: Thu Oct 17, 2013 10:47 am
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.

Re: The sequence number for $filename

Posted: Thu Oct 17, 2013 10:50 am
by Celauran
Race conditions could still cause file name collisions but, that aside, OP specifically stated he wants incrementing numbers.

Re: The sequence number for $filename

Posted: Thu Oct 17, 2013 3:10 pm
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.

Re: The sequence number for $filename

Posted: Thu Oct 17, 2013 4:19 pm
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;