Providing uploaded images with a unique name for mysqli

I want to allow users to upload images without any problems that may be caused by multiple users uploading images that potentially have the same image name. I'm at a dead end on how to accomplish this, and I don't know where to start ..

Here is my code:

if(isset($_POST['submitimage'])){ move_uploaded_file($_FILES['file']['tmp_name'],"pictures/".$_FILES['file']['name']); $con = mysqli_connect("localhost","root","","database"); $q = mysqli_query($con,"UPDATE users SET image = '".$_FILES['file']['name']."' WHERE user_id = '".$_SESSION['user']."'"); header("Location: index.php"); } ?> 

Any help would be awesome. Thank!

+2
php file-rename
May 29 '16 at 22:35
source share
3 answers

My solution is to generate a random line for each downloaded file, i.e.:

 <?php if(!empty($_POST['submitimage'])){ //get file extension. $ext = pathinfo($_FILES['file']['name'])['extension']; //generate the new random string for filename and append extension. $nFn = generateRandomString().".$ext"; move_uploaded_file($_FILES['file']['tmp_name'],"pictures/".$nFn); $con = mysqli_connect("localhost","root","","database"); $q = mysqli_query($con,"UPDATE users SET image = '{$nFn}' WHERE user_id = '{$_SESSION['user']}'"); header("Location: index.php"); } function generateRandomString($length = 10) { return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, $length); } ?> 
+2
May 29 '16 at 10:50
source share

PHP has a built-in function to create unique files on your server. This function is called tempnam () . If you carefully read the comments on this website, there is a small chance that you will get unwanted behavior from this function if many processes call it at the same time. Thus, the modification of this function will be as follows:

 <?php function tempnam_sfx($path, $suffix){ do { $file = $path."/".mt_rand().$suffix; $fp = @fopen($file, 'x'); } while(!$fp); fclose($fp); return $file; } ?> 

Since the file remains open during its creation, it cannot be accessed by another process, and therefore it is impossible to create 2 files with the same name, simply because several visitors to your site accidentally uploaded images at exactly the same moment. Therefore, to implement this in your own code:

 <?php function tempnam_sfx($path, $suffix){ do { $file = $path."/".mt_rand().$suffix; $fp = @fopen($file, 'x'); } while(!$fp); fclose($fp); return $file; } $uploaddir = 'pictures'; // Upload directory $file = $_FILES['file']['name']; // Original file $ext = pathinfo($path, PATHINFO_EXTENSION); // Get file extension $uploadfile = tempnam_sfx($uploaddir, $ext); move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile); $con = mysqli_connect("localhost","root","","database"); $q = mysqli_query($con,"UPDATE users SET image = '".basename($uploadfile)."' WHERE user_id = '{$_SESSION['user']}'"); header("Location: index.php"); ?> 
+1
May 29 '16 at 23:18
source share

One way to do this is to create some random numbers (and possibly bind them to the current date in a number format) and give the image a sequence of numbers.

  if(isset($_POST['submitimage'])){ //generate 3 sequences of random numbers,you could do more or less if you wish $randomNumber=rand().rand().rand(); move_uploaded_file($_FILES['file']['tmp_name'],"pictures/".$randomNumber."jpg"); $con = mysqli_connect("localhost","root","","database"); $q = mysqli_query($con,"UPDATE users SET image = '".$randomNumber.".jpg' WHERE user_id = '".$_SESSION['user']."'"); header("Location: index.php"); } ?> 

Note: you can also learn about generating random strings if numbers are not your thing.

0
May 29 '16 at 22:44
source share



All Articles