0

I used the following code to upload the images. It works well. But I dont know how to display all the images in a display page.

Please help for the code for display.

    <?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";
}
}
?>
1
  • 2
    Before you write any more SQL code, you must read up on proper SQL escaping to avoid serious SQL escaping bugs. You cannot use string interpolation to compose queries. It's also a bad idea to be using mysql_query as it's deprecated and will be removed from future versions of PHP. A modern replacement like PDO is not hard to learn. Commented Jul 2, 2013 at 13:55

2 Answers 2

5

You can show images something like this:

$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">';

}
Sign up to request clarification or add additional context in comments.

4 Comments

What's up with all the error-suppressing? Terrible for debugging
the fact you have edited your code doesn't mean much. Is there a reason behind the fact you are suppressing even a variable declaration with the @ sign? Are you aware of it's purpose?
Yes I know the purpose of it. It temporarily sets the error reporting level to 0 for that line. If that line triggers an error, the error handler will still be called, but it will be called with an error level of 0. I sometimes use it while testing some scripts, but it's better to not show it him that way. You guys are right.
It dos n't shows any error. But image not displayed. Image border displays for all images, but image shows like unavailable or broken image.
1

Try this:

$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>"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.