Hye guys, I have created jquery ajax file uploader, but I have one problem with it which is the server side, it will over-ride any file with the same name And Im going to generate a random number, which should be different to another one but so the code for my uploader is
<?php
$target_path = "/var/www/vhosts/grubber.co.nz/httpdocs/developer/_social_development/uploads/blog/";
$target_path = $target_path . basename( $_FILES['img']['name']);
if (file_exists($target_path)) {
echo "The file $filename exists";
$target_path = "/var/www/vhosts/grubber.co.nz/httpdocs/developer/_social_development/uploads/blog/" . basename( $_FILES['img']['name']);
} else {
echo "The file $filename does not exist";
}
if(move_uploaded_file($_FILES['img']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['img']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
Question, I want it to check if a file exsits and if it does generate a random number and add it to the name? if not just continues and writes the file.
One solution could be to prepend filename with some unique number.
You can use uniqid() function. http://php.net/manual/en/function.uniqid.php