Download image using a different name

<?php require_once 'location.php'; if (isset($_POST['submit'])) { $screenshot = $_FILES['screenshot']['name']; $screenshot_type = $_FILES['screenshot']['type']; $screenshot_size = $_FILES['screenshot']['size']; if (!empty($screenshot)) { if ((($screenshot_type == 'image/gif') || ($screenshot_type == 'image/jpeg') || ($screenshot_type == 'image/pjpeg') || ($screenshot_type == 'image/png')) && ($screenshot_size > 0) && ($screenshot_size <= GW_MAXFILESIZE)) { if ($_FILES['screenshot']['error'] == 0) { // Move the file to the target upload folder $target = GW_UPLOADPATH .hash("md5", $screenshot); if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) { // Connect to the database $dbc = mysqli_connect('locl', 'abc', '', 'xyz') or die('Error connecting to MySQL server.'); // Write the data to the database $query = "INSERT INTO guitar VALUES ('$screenshot')"; mysqli_query($dbc, $query); echo '<img src="' . GW_UPLOADPATH . $screenshot . '" alt="Screen shot" /> </p>'; echo '<p><a href="index.php">&lt;&lt; Back to high scores</a></p>'; // Clear the score data to clear the form $screenshot = ""; mysqli_close($dbc); } else { echo '<p>Sorry, there was a problem uploading your screen shot image.</p>'; } } } else { echo '<p>The screen shot must be a GIF, JPEG, or PNG image file no greater than ' . (GW_MAXFILESIZE / 1024) . ' KB in size.</p>'; } // Try to delete the temporary screen shot image file @unlink($_FILES['screenshot']['tmp_name']); } else { echo '<p>Please enter all of the information to add your high score.</p>'; } } ?> <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="hidden" name="MAX_FILE_SIZE" value="32768" /> <label for="screenshot">Screen shot:</label > <input type="file" id="screenshot" name="screenshot" /> <input type="submit" value="Add" name="submit" /> </form> 

This code loads the image. But if their image with the same name already exists, he overwrites it. Sometimes a user uploads images with the same name.

How can I solve this problem. Or any way to add a random name before the image name.

+1
source share

All Articles