如何在数据库中显示PHP中的多个图像?

时间:2013-07-02 13:43:08

标签: php mysql

我使用以下代码上传图片。它运作良好。但我不知道如何在显示页面中显示所有图像。

请帮助显示代码。

    <?php
if(isset($_FILES['files'])){
    $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';
    }       
    $query="INSERT into upload_data (`USER_ID`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) 
            VALUES('$user_id','$file_name','$file_size','$file_type'); ";

    $desired_dir="gallery";
    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,"gallery/".$file_name);
        }else{                                  //rename the file if another one exist
            $new_dir="gallery/".$file_name.time();
             rename($file_tmp,$new_dir) ;               
        }
        mysql_query($query) or die(mysql_error());          
    }else{
            print_r($errors);
    }
 }
if(empty($error)){
    echo "Success";
}
}
?>

2 个答案:

答案 0 :(得分:5)

您可以显示如下图像:

$sql="select * from tbl_image";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query))
{
$image=$row ['photo'];

echo '<img src="path/'.$image.'" width="360" height="150">';

}

答案 1 :(得分:1)

试试这个:

$result= //select query for retrieving filenames for a particular userid 
         //or all depending upon your requirement.

while($row=mysql_fetch_array($result))
{
 echo "<div><img src=\"gallery/".$row['filename']."\" /></div>"
}