Php upload script with namechange

Thread Solved

Join Date: Jun 2009
Posts: 13
Reputation: brianzet is an unknown quantity at this point 
Solved Threads: 1
brianzet brianzet is offline Offline
Newbie Poster

Php upload script with namechange

 
0
  #1
Oct 30th, 2009
Hello all.

I've got this upload script, working just fine.

  1. <?php
  2. error_reporting(0);
  3.  
  4. $change="";
  5. $abc="";
  6.  
  7.  
  8. define ("MAX_SIZE","400");
  9. function getExtension($str) {
  10. $i = strrpos($str,".");
  11. if (!$i) { return ""; }
  12. $l = strlen($str) - $i;
  13. $ext = substr($str,$i+1,$l);
  14. return $ext;
  15. }
  16.  
  17. $errors=0;
  18.  
  19. if($_SERVER["REQUEST_METHOD"] == "POST")
  20. {
  21. $image =$_FILES["file"]["name"];
  22. $uploadedfile = $_FILES['file']['tmp_name'];
  23.  
  24.  
  25. if ($image)
  26. {
  27.  
  28. $filename = stripslashes($_FILES['file']['name']);
  29.  
  30. $extension = getExtension($filename);
  31. $extension = strtolower($extension);
  32.  
  33.  
  34. if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
  35. {
  36.  
  37. $change='<div class="msgdiv">Ukendt fil format.<br /><br /></div> ';
  38. $errors=1;
  39. }
  40. else
  41. {
  42.  
  43. $size=filesize($_FILES['file']['tmp_name']);
  44.  
  45.  
  46. if ($size > MAX_SIZE*1024)
  47. {
  48. $change='<div class="msgdiv">Du har overskredet fil st&oslash;rrelsen!<br /><br /></div> ';
  49. $errors=1;
  50. }
  51.  
  52.  
  53. if($extension=="jpg" || $extension=="jpeg" )
  54. {
  55. $uploadedfile = $_FILES['file']['tmp_name'];
  56. $src = imagecreatefromjpeg($uploadedfile);
  57.  
  58. }
  59. else if($extension=="png")
  60. {
  61. $uploadedfile = $_FILES['file']['tmp_name'];
  62. $src = imagecreatefrompng($uploadedfile);
  63.  
  64. }
  65. else
  66. {
  67. $src = imagecreatefromgif($uploadedfile);
  68. }
  69.  
  70. echo $scr;
  71.  
  72. list($width,$height)=getimagesize($uploadedfile);
  73.  
  74.  
  75. $newwidth=400;
  76. $newheight=($height/$width)*$newwidth;
  77. $tmp=imagecreatetruecolor($newwidth,$newheight);
  78.  
  79.  
  80. $newwidth1=193;
  81. $newheight1=143;
  82. $tmp1=imagecreatetruecolor($newwidth1,$newheight1);
  83.  
  84. imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
  85.  
  86. imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
  87.  
  88.  
  89. $filename = "user_imgs/". $_FILES['file']['name'];
  90.  
  91. $filename1 = "user_imgs/thumbs/small". $_FILES['file']['name'];
  92.  
  93.  
  94.  
  95. imagejpeg($tmp,$filename,100);
  96.  
  97. imagejpeg($tmp1,$filename1,100);
  98.  
  99. imagedestroy($src);
  100. imagedestroy($tmp);
  101. imagedestroy($tmp1);
  102. }}
  103.  
  104. }
  105.  
  106. //If no errors registred, print the success message
  107. if(isset($_POST['Submit']) && !$errors)
  108. {
  109.  
  110. // mysql_query("update {$prefix}users set img='$big',img_small='$small' where user_id='$user'");
  111. $change=' <div class="msgdiv">Billedet blev uploadet succesfuldt!<br /><br /></div>';
  112. }
  113.  
  114. ?>

Problem is, when I upload 2 different images, but with the same file name, one of the images overwrites the other, I dont want that.

Can anyone help me how to generate a random name for each picture that is getting upload, so that no images contains the same file name?

Thanks in advance.
Brian.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 455
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
0
  #2
Oct 31st, 2009
Hey.

You can use the uniqid and mt_rand functions to generate a unique string of numbers and letters that you can add to the image name.
  1. $unique = uniqid(mt_rand());
  2. $filename = "user_imgs/{$unique}_{$_FILES['file']['name']}";
  3. $filename1 = "user_imgs/thumbs/{$unique}_small{$_FILES['file']['name']}";
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Reply

Tags
php, script, upload

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 638 | Replies: 1
Thread Tools Search this Thread



Tag cloud for php, script, upload
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC