上传并查看多个图像,无需重命名 - php

时间:2016-03-02 19:08:31

标签: php html mysql image

我正在使用此代码将多个图像上传到路径,然后将此路径存储在我的数据库中。数据库表称为“images”,其字段为:id,name(varchar),image(longblob)。我可以上传它们,但每次将图像存储在路径中时,它们的名称都会改变。例如,如果我上传名为“cat.png”的图像并将其存储到uploads/目录,则其名称将变为“366331bd4c8bcb503ceda1ae229b79e0.png”。此外,我无法在上传时看到图像 - 只有损坏的图像图标。这是代码:

<!DOCTYPE html>
<html lang="en">
<head>
	<title>Upload Images</title>
</head>
 <body>
	<form action="upload_file.php" method="POST" enctype="multipart/form-data" >
		<p>Select Image (one or multiple):</p>
		<input type="hidden" name="MAX_FILE_SIZE" value="262144000"/>
		<input type="file" name="image[]" accept="image/jpeg" accept="image/jpg" accept="image/png" accept="image/gif" multiple="multiple" />
		<input type="submit" value="Upload file" name="submit" />
	</form>
 </body>
</html>

upload_file.php

<?php
include('../config.php');
if (isset($_POST['submit'])) {
	$j = 0;     // Variable for indexing uploaded image.
	$target_path = "uploads/";     // Declaring Path for uploaded images.
	for ($i = 0; $i < count($_FILES['image']['name']); $i++) {
		// Loop to get individual element from the array
		$validextensions = array("jpeg", "jpg", "png");      // Extensions which are allowed.
		$ext = explode('.', basename($_FILES['image']['name'][$i]));   // Explode file name from dot(.)
		$file_extension = end($ext); // Store extensions in the variable.
		$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) -1];     // Set the target path with a new name of image.
		$j = $j + 1;      // Increment the number of uploaded images according to the files in array.
		
		if (($_FILES["image"]["size"][$i] < 262144000) && in_array($file_extension, $validextensions)) {
			if (move_uploaded_file($_FILES['image']['tmp_name'][$i], $target_path)) {

				$final_name = explode('/', $target_path);
				$image_name_final=$final_name[$i];
				$sql="INSERT INTO images (id,name,image) VALUES ('','$image_name_final','$target_path')";
				$result = mysql_query($sql) or die(mysql_error());
				
				// If file moved to uploads folder.
				echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
				//Show selected image(s)
				$lastid = mysql_insert_id();
				echo "<img src=get.php?id=$lastid>";
			} else {     //  If File Was Not Moved.
				echo $j. ').<span id="error">please try again!.</span><br/><br/>';
				}
		} else {     //   If File Size And File Type Was Incorrect.
				echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
			}
	}
}
error_reporting(-1);
?>

get.php

<?php
include('../config.php');
$id = $_REQUEST['id'];
$rows = mysql_query("SELECT * FROM images WHERE id=$id");
$image = mysql_fetch_assoc($rows);
$image = $image['image'];

header('Content-type: image/png');

echo base64_decode($image);
?>

有人可以帮忙吗?感谢

1 个答案:

答案 0 :(得分:0)

正如Lashane在上面的评论中指出的那样,您的代码正在为每个文件生成唯一的MD5哈希值。这是这一行,特别是md5()函数:

$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) -1];

你说你只想让文件每次都有相同的名字。要做到这一点,你应该能够简单地写:

$target_path = $target_path . basename($_FILES['image']['name'][$i]);