How to create unique id for uploaded file using this code.
Code uploads the image, but when you upload it using iPhone all images are come as image.jpg. So I need a function to rename existing file or just to create unique id for each.
<form enctype="multipart/form-data" action="" method="POST"> Please choose a file: <input name="uploaded" type="file" /><br /> <input type="submit" value="Upload" /> </form>
<?php
function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; }
$ext = findexts ($_FILES['uploaded']['name']) ;
$ran = rand () ;
$ran2 = $ran.".";
$target = "uploads/";
if(file_exists("uploads/$filename")) unlink("uploads/$filename");move_uploaded_file($target, "uploads/$filename");
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file has been uploaded as ".$ran2.$ext; } else { echo "Sorry, there was a problem uploading your file."; }
$target = $target . $ran2.$ext;
You can use
uniqid()
function for unique image names. It gets two parameters. First: prefix, Second: more entropy.See details here: http://php.net/manual/en/function.uniqid.php