| | |
Php upload script with namechange
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 13
Reputation:
Solved Threads: 1
Hello all.
I've got this upload script, working just fine.
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.
I've got this upload script, working just fine.
PHP Syntax (Toggle Plain Text)
<?php error_reporting(0); $change=""; $abc=""; define ("MAX_SIZE","400"); function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; if($_SERVER["REQUEST_METHOD"] == "POST") { $image =$_FILES["file"]["name"]; $uploadedfile = $_FILES['file']['tmp_name']; if ($image) { $filename = stripslashes($_FILES['file']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { $change='<div class="msgdiv">Ukendt fil format.<br /><br /></div> '; $errors=1; } else { $size=filesize($_FILES['file']['tmp_name']); if ($size > MAX_SIZE*1024) { $change='<div class="msgdiv">Du har overskredet fil størrelsen!<br /><br /></div> '; $errors=1; } if($extension=="jpg" || $extension=="jpeg" ) { $uploadedfile = $_FILES['file']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); } else if($extension=="png") { $uploadedfile = $_FILES['file']['tmp_name']; $src = imagecreatefrompng($uploadedfile); } else { $src = imagecreatefromgif($uploadedfile); } echo $scr; list($width,$height)=getimagesize($uploadedfile); $newwidth=400; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); $newwidth1=193; $newheight1=143; $tmp1=imagecreatetruecolor($newwidth1,$newheight1); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height); $filename = "user_imgs/". $_FILES['file']['name']; $filename1 = "user_imgs/thumbs/small". $_FILES['file']['name']; imagejpeg($tmp,$filename,100); imagejpeg($tmp1,$filename1,100); imagedestroy($src); imagedestroy($tmp); imagedestroy($tmp1); }} } //If no errors registred, print the success message if(isset($_POST['Submit']) && !$errors) { // mysql_query("update {$prefix}users set img='$big',img_small='$small' where user_id='$user'"); $change=' <div class="msgdiv">Billedet blev uploadet succesfuldt!<br /><br /></div>'; } ?>
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.
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.
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 (Toggle Plain Text)
$unique = uniqid(mt_rand()); $filename = "user_imgs/{$unique}_{$_FILES['file']['name']}"; $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!
And use [code] tags!
![]() |
Other Threads in the PHP Forum
- Previous Thread: Warning message? & NEXT page of ´results' not showing any results?
- Next Thread: error on wampserver2.0i
Views: 636 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for php, script, upload
access action ajax archive array arrays asp automatically beginner body c# cakephp class cms code cookies curl data database date directory display downloader dropdownlist duplicates email execution file files flash folder form forms function functions html iframe image include innerjoins insert integration java javascript jquery js lamp limit link links linux listview login mail malfunction menu methods multiple mysql navigation news oop open paypal pdf php post problem query recursiveloop replace rss script search searchmonkey security session simple sms soap spam sql static support! survey table test textbox tutorial upload validation vbulletin video virus web webdesign xml ydn youtube zend






