最好的办法是在数据库表中保存文件路径或文件名。并将您的文件保存在服务器中。
下面是一个示例MULTIPLE图像上传和检索脚本为您的需要。我要在这里使用3个文件。 front.html,upload.php的最后preview.php
- Front.html
该文件包含了基本的HTML表单的内容。
<form enctype="multipart/form-data" action="upload.php" method="post">
<input id="uploadFile" type="file" name="files[]" class="img" />
<button class="btn btn-primary" name="up_img">POST</button>
</form>
2.upload.php
if(isset($_POST['up_img'])){ $errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){
$file_name =$key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$pType='I';
$query="insert into img(user,FILE_NAME)values('$sUser','$file_name') ";
$desired_dir="images_folder";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
}else{ // rename the file if another one exist
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
mysql_query($query);
}else{
print_r($errors);
}
}
if(empty($error)){
header('Location:preview.php');
}
}
3.preview.php
现在,我已经成功上传我的图像/影像服务器,并准备对其进行检索。记住我的图像文件现在存储在文件夹call images_folder中,它们的路径保存在img表中。
<?php
$sql=mysql_query('SELECT * FROM img ');
while($row = mysql_fetch_array($sql))
{
$sPic=$row['FILE_NAME'];
echo '<img src="images_folder/'.$sPic.'">';
}
?>
就是这样。这就是你要如何上载图像到你的服务器,并检索它们。
上传服务器上的图像。将其路径保存在数据库中并在显示时将其从URL中拉出来 – Satya
您可以请解释一下 – Udhayakkrishna
首先提及[this](http://stackoverflow.com/questions/13470482/php-upload-image-to-directory) – Akshay