<?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"><< 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>
此代码上传图片。但如果他们的同名图像已经存在,那么它会覆盖它。有时,用户上传具有相同名称的图像。
我怎么能解决这个问题。或者在图像名称之前添加随机名称的任何方式。