pictureupload / uploadname needs to differ everytime

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
Patricio88
Forum Newbie
Posts: 6
Joined: Thu Jun 12, 2008 8:41 am

pictureupload / uploadname needs to differ everytime

Post by Patricio88 »

Hi guys,
I have a code which uploads pictures to a directory on my Server. Here is the code:

Code: Select all

 
 <?php
   
      // Is it a jpg picture?
   
      if ((($_FILES['meinbild']['type'] == 'image/pjpeg') || ($_FILES['meinbild']['type'] == 'image/jpeg')) && ($_FILES['meinbild']['error'] == 0) && ($_FILES['meinbild']['tmp_name'] != none) && ($_FILES['meinbild']['name']) && ($_FILES['meinbild']['size'] > 0)) {
   
      $filename_old = $_FILES['meinbild']['name'];
      $filename_new = 'new_picture.jpg';
  
      $savepath = "images/$ID_user/";
   
      $size = getimagesize($_FILES['meinbild']['tmp_name']);
   
      $width_old = $size[0];
      $height_old = $size[1];
  
      // new format
  
      if ($width_old > $height_old) {
  
      $width_new = '200';
  
      $height_new = intval($height_old * $width_new / $width_old);
  
      } else {
  
      $height_new = '200';
  
      $width_new = intval($width_old * $height_new / $height_old);
  
      }
   
      // processing
  
      $picture_old = imagecreatefromjpeg($_FILES['meinbild']['tmp_name']);
  
      $picture_new = imagecreatetruecolor($width_new, $height_new);
  
      imagecopyresampled($picture_new, $picture_old, 0, 0, 0, 0, $width_new, $height_new, $width_old, $height_old);
   
      // save
  
      $create = imagejpeg($picture_new, $savepath.$filename_new);
       $create_old = imagejpeg($picture_old, $savepath.$filename_old);
  
      imagedestroy($picture_new);
     imagedestroy($picture_old);
  
       
     if ($create && $create_old) {
  
      echo '<p>picture uploaded view: <a href="'.$savepath.$filename_new.'">here</a> .</p>'."\n";
  
      }
  
      } else {
  
      echo '<p>not successfull</p>'."\n";
  
      }
  
      ?>
  
$filename_new = 'new_picture.jpg';

The part in red needs to be changed. As it is now, a picture that would be uploaded would get that name. However, if I upload another picture it would have the same name and hence be overwritten. So, how can the picture be uploaded so it has a different name everytime.

I thought about a text field so that the user can input a filename and if it is already taken that he had to chose a different name. But I really can't do that and it would take you guys to long either.

So, I thought about a loop. What about if the pictures are named 1.jpg, 2.jpg etc... But I don't know how o do that either because you'd have to check if there is already one picture of that name on the server.

Please help me. Thanks a lot in advance.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: pictureupload / uploadname needs to differ everytime

Post by andym01480 »

How about a timestamp? - the chances of 2 people uploading an image at the very same second are pretty slim - unless you are developing facebook, myspace...

Code: Select all

$filename_new = 'new_picture'.mktime().'.jpg';
or if you are likely to have two people at the same time

Code: Select all

$filename_new = 'new_picture'.mktime().rand().'.jpg';
which adds a random element to the time stamp!
Patricio88
Forum Newbie
Posts: 6
Joined: Thu Jun 12, 2008 8:41 am

Re: pictureupload / uploadname needs to differ everytime

Post by Patricio88 »

I am actually trying to code kind of a Facebook site, so it is very likely that many people will upload at the same time.

So should I use your second example or are there any better ways? I am just asking because I am not really a genius in PHP.
Thank you soo much!
agentcris
Forum Newbie
Posts: 12
Joined: Thu Jun 12, 2008 8:27 am

Re: pictureupload / uploadname needs to differ everytime

Post by agentcris »

andym01480 wrote: or if you are likely to have two people at the same time

Code: Select all

$filename_new = 'new_picture'.mktime().rand().'.jpg';
which adds a random element to the time stamp!
As andym01480 quoted, it is one of the best ways to do it.

Another way of doing it,

Code: Select all

$filename_new = 'new_picture'.mktime().mt_rand().'.jpg';
According to the PHP manual, mt_rand() is 4 times faster than rand() and in my personal experience, I have always found mt_rand() to produce more random numbers than rand(). There have been many benchmark tests that have been done to compare the speeds of these two functions and most of them came up with the conclusion that rand() is just slightly faster that mt_rand(). But nevertheless, you could use mt_rand() as mentioned above.
Patricio88
Forum Newbie
Posts: 6
Joined: Thu Jun 12, 2008 8:41 am

Re: pictureupload / uploadname needs to differ everytime

Post by Patricio88 »

Thank you very much. You two guys helped me out a lot!
Post Reply