Results 1 to 12 of 12

Thread: php - How to NOT Overwrite a File?

  1. #1

    php - How to NOT Overwrite a File?

    This is my current php script

    PHP Code:
    $fp fopen('image.jpeg''wb');
    fwrite($fp$GLOBALS['HTTP_RAW_POST_DATA']);
    fclose($fp); 
    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".

    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!

  2. #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);  
    ?>

  3. #3
    that 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?

  4. #4
    Quote Originally Posted by jebego View Post
    that 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?
    It 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

  5. #5
    Quote Originally Posted by IqAndreas View Post
    It 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.
    thanks! I'll research timestamping a bit!

  6. #6
    Anyway, 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.php
    Blog article of the month: Why My One Line 'if' Statements Are Unusual
    Twitter: @IQAndreas
    GitHub: IQAndreas

  7. #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:

    PHP Code:
    <?php
    $fp 
    fopen('image'.sha1(time()).'.jpeg''wb');
    fwrite($fp$GLOBALS['HTTP_RAW_POST_DATA']);
    fclose($fp);  
    ?>
    Then you're (practically) guaranteed unique names. Or you can do something like:

    PHP Code:
    <?php
    $fp 
    fopen('image'.substr(sha1(time()), 08).'.jpeg''wb');
    fwrite($fp$GLOBALS['HTTP_RAW_POST_DATA']);
    fclose($fp);  
    ?>

  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:

    PHP Code:
    <?php 
    $fp 
    fopen('image'.sha1(time().rand(0,50000)).'.jpeg''wb'); 
    fwrite($fp$GLOBALS['HTTP_RAW_POST_DATA']); 
    fclose($fp);   
    ?>
    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.
    "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

  9. #9
    icio's Avatar
    3,811
    posts
    looks better in lowercase
    Quote Originally Posted by blazes View Post
    PHP Code:
    <?php
    $fp 
    fopen('image'.sha1(time()).'.jpeg''wb');
    fwrite($fp$GLOBALS['HTTP_RAW_POST_DATA']);
    fclose($fp);  
    ?>
    Then you're (practically) guaranteed unique names. Or you can do something like:
    `sha1(time())` is going to be no more unique than `time()` itself.
    Last edited by icio; January 9th, 2010 at 01:24 PM. Reason: Left out the negative!
    "60% of the time it works... every time." -- Paul Rudd as Brian Fantana.

  10. #10
    time() 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.

  11. #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.

  12. #12
    jwilliam's Avatar
    476
    posts
    An intellectual carrot!
    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012