2016-03-02 58 views
-1

我使用此代码将多个图像上传到路径,然后将此路径存储在我的数据库中。数据库表被称为“图像”,其字段为:id,name(varchar),image(longblob)。我可以上传它们,但每当图像存储在路径中时,它们的名称都会改变。例如,如果我上传名为“cat.png”的图像并将其存储到uploads/目录,则其名称变成类似“366331bd4c8bcb503ceda1ae229b79e0.png”的名称。另外,上传时我看不到图像 - 只有一个破碎的图像图标。下面是代码:上传并查看多个图像而不重命名 - php

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

upload_file.php

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

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

'$ target_path = $ target_path。 md5(uniqid())。 “” 。 $ ext [count($ ext)-1]; '如果你不想为这些文件创建uniq /改变文件名,那么将名称更改为唯一标识 –

+1

,那么为什么每次文件上传时都生成唯一的哈希值。 “每次我把这个叉子刺进我的眼睛,它都会伤害,我怎么让眼睛不受伤害?” –

+0

是的,但你知道我该如何解决这个问题? – joasa

回答

0

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

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

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

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

+0

非常感谢它现在的作品!我也希望在上传图片时看到这些图片,并且在上传一张图片时此行正在运行,但现在它显示的图片图标已损坏。 '$ lastid = mysql_insert_id();回声“”;'你有什么想法为什么? – joasa

+0

没有看到get.php的代码,我不能确定 – ADyson

+0

它在那里,最后一个框:) – joasa