Results 1 to 12 of 12
-
January 7th, 2010, 07:59 PM #127▲▼▲
postsphp - How to NOT Overwrite a File?
This is my current php script
I'm using my flash file to send some data to my php file to create and save a .jpeg on my server under the name of "image.jpeg".PHP Code:$fp = fopen('image.jpeg', 'wb');
fwrite($fp, $GLOBALS['HTTP_RAW_POST_DATA']);
fclose($fp);
If I want to create more the one image, this script overwrites my current image.
How would I not overwrite it, but instead add a number to the end of the image's name, ex: image.jpeg, image-2.jpeg, image-3.jpeg, you get the point.
This is probobly pretty simple, I'm just somewhat of a noobie at php.
Thanks!
-
January 7th, 2010, 08:03 PM #2
The easiest method would to to just add a timestamp to the end.
PHP Code:<?php
$fp = fopen('image'.time().'.jpeg', 'wb');
fwrite($fp, $GLOBALS['HTTP_RAW_POST_DATA']);
fclose($fp);
?>
-
January 7th, 2010, 08:05 PM #327▲▼▲
poststhat idea occurred to me, but would it be possible for two people to create an image at the exact same time, therefore one overwriting the other? Or is this not possible?
-
January 7th, 2010, 08:10 PM #42,702Seņor Member
postsIt depends on how accurate the timestamp is. If it is down to the millisecond (which I am guessing it is, right?) Well, the chances of two people creating an image at the same millisecond are... ... not big. Also, the way PHP works, I could be wrong, but I'm guessing two exact same lines can't be run at the exact same time. Or does PHP use threading for each new page request?
But if you really want to check for this, you could check if the file already exists, and if it does, save the image one millisecond later instead.
Blog article of the month: Why My One Line 'if' Statements Are Unusual
Twitter: IQAndreas
GitHub: IQAndreas
-
January 7th, 2010, 08:11 PM #527▲▼▲
posts
-
January 7th, 2010, 09:05 PM #62,702Seņor Member
postsAnyway, here is the PHP documentation on time():
http://php.net/manual/en/function.time.php
Sadly, it returns seconds, but if there are a lot of users and it is likely images will be overridden, there is also a command named microtime() which is milliseconds:
http://www.php.net/manual/en/function.microtime.phpBlog article of the month: Why My One Line 'if' Statements Are Unusual
Twitter: IQAndreas
GitHub: IQAndreas
-
January 8th, 2010, 12:38 PM #7
If you're in a regular environment time() should be fine. If you want to be sure you can use microtime(). If you expect very heavy usage (or you just want to be as safe as possible), and you don't mind ugly names you can do something like:
Then you're (practically) guaranteed unique names. Or you can do something like:PHP Code:<?php
$fp = fopen('image'.sha1(time()).'.jpeg', 'wb');
fwrite($fp, $GLOBALS['HTTP_RAW_POST_DATA']);
fclose($fp);
?>
PHP Code:<?php
$fp = fopen('image'.substr(sha1(time()), 0, 8).'.jpeg', 'wb');
fwrite($fp, $GLOBALS['HTTP_RAW_POST_DATA']);
fclose($fp);
?>
-
January 8th, 2010, 01:04 PM #8
Blazes gave a great answer. Though, it seems you want an extra level of assurance (I can relate). Also not perfect or absolute, but a teensie bit less likely to cause conflict:
Though, for the record, in most cases you won't need to be so direct and even a simple time-stamp will do fine to keep you safely unique. Blazes first suggestion will work well enough 98% of the time.PHP Code:<?php
$fp = fopen('image'.sha1(time().rand(0,50000)).'.jpeg', 'wb');
fwrite($fp, $GLOBALS['HTTP_RAW_POST_DATA']);
fclose($fp);
?>"Give me the place to stand, and I shall move the earth" - Archimedes
"The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts." - Bertrand Russell
-
January 9th, 2010, 12:23 PM #9
-
January 10th, 2010, 01:09 PM #10107DEAD SHOT
poststime() is what i use and as like others i would sugest tht too. Or you could add time and then generate a random number and add them both to the filename.
-
January 10th, 2010, 05:24 PM #11
just do a hash on time and the users ip or something that's relatively unique
Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.
-
January 11th, 2010, 01:00 PM #12
It uses principles already mentioned in this thread, but I thought I'd point out this function:
http://us2.php.net/manual/en/function.uniqid.php

Reply With Quote


Bookmarks