Home » Posts filed under How to make a php image uploader script | Image Upload Script

How to make a php image uploader script | Image Upload Script

Problem:
image upload script that change image name
Hello all.

I've got this upload script, working well.

PHP Syntax
  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?
[Re]
Solve:
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.
php Syntax 
  1. $unique = uniqid(mt_rand());
  2. $filename = "user_imgs/{$unique}_{$_FILES['file']['name']}";
  3. $filename1 = "user_imgs/thumbs/{$unique}_small{$_FILES['file']['name']}";

[Re]
Ah ! thank you very much.