dipak dutta
dipak dutta

Reputation: 274

Show images from dynamically created path

I saved multiple images into dynamically created database path. But now the problem is that images are not showing while fetching.I have added my two tables in database and also php code and directory that I created when entering images. I think my problem is image source. I am unable to modify the image source path. Plz help.

I have two tables in database: 1st Table: advt

- ad_id
- cus_id
- ad_name
- ad_des
- date

2nd table : Full texts

- img_id
- ad_name
- img_name (type: longblob)
- img_type
- img_size

This is my php code to show images from database path:

<?php 
include('include/config.php');

 if($stmt2 = $connection->prepare("SELECT img_id, ad_name, img_name, img_type, img_size FROM `full texts`")){ 

       $stmt2->execute(); 
       $stmt2->store_result(); 
       $stmt2->bind_result($img_id, $ad_name, $img_name, $img_type, $img_size); 
       while($stmt2->fetch()){ 
         ?>
           <img src="'ad/data/img/'.$cus_id.'/'.$ad_id.'/'<?php echo $img_name; ?>" width="220" height="220"><br>

         <?php
       } 
       $stmt2->close();
    } 
?>

This is below how I created directory to save images:

$cus_id = $_POST['cus_id'];
$ad_id = $_POST['ad_id'];
$dir='ad/data/img/'.$cus_id.'/'.$ad_id.'/';
if(!file_exists($dir) || !is_dir($dir)){
    mkdir($dir, 0777, true);
    chmod($dir, 0777, true);
}

Upvotes: 2

Views: 131

Answers (4)

achal naskar
achal naskar

Reputation: 710

Try this:

<?php 
    include('include/config.php');

    if($stmt = $connection->prepare("SELECT 
                                     a.img_id, 
                                     a.ad_name, 
                                     a.img_name, 
                                     a.img_type, 
                                     a.img_size, 
                                     b.ad_id, 
                                     b.cus_id 
                                     FROM `full texts` a 
                                     INNER JOIN advt b
                                     ON a.ad_name=b.ad_name
                                     ")){ 


           $stmt->execute(); 
           $stmt->store_result(); 
           $stmt->bind_result($img_id, $ad_name, $img_name, $img_type, $img_size, $ad_id, $cus_id);   

           while($stmt->fetch()){ 

              echo  "<img src=ad/data/img/$cus_id/$ad_id/$img_name  width='220' height='220'><br>";


           } 
           $stmt->close();
           } 

?>

Upvotes: 1

jasinth premkumar
jasinth premkumar

Reputation: 1413

i think error is in your php code because of using php variable outside <?PHP?>

corrected code:

    <?php 
include('include/config.php');

 if($stmt2 = $connection->prepare("SELECT img_id, ad_name, img_name, img_type, img_size FROM `full texts`")){ 

       $stmt2->execute(); 
       $stmt2->store_result(); 
       $stmt2->bind_result($img_id, $ad_name, $img_name, $img_type, $img_size); 
       while($stmt2->fetch()){ 

          echo  "<img src=ad/data/img/$cus_id/$ad_id/$img_name  width="220" height='220'><br>";


       } 
       $stmt2->close();
    } 
?>

addition

$stmt3 = $connection->prepare("SELECT ad_id, cus_id, ad_name, ad_des, date FROM `advt`" //where condition to particular img_id)
       $stmt3->execute(); 
           $stmt3->store_result(); 
           $stmt3->bind_result($ad_id, $cus_id, $ad_name, $ad_des, $date); 

Upvotes: 1

Bikesh M
Bikesh M

Reputation: 8373

while($stmt2->fetch()){ 

    $src = 'ad/data/img/'.$cus_id.'/'.$ad_id.'/'.$img_name;
     ?>
       <img src="<?php echo $src; ?>" width="220" height="220"><br>

     <?php
   }

Upvotes: 1

Jarek J&#243;źwik
Jarek J&#243;źwik

Reputation: 77

It would be helpful if you've pasted what your browser got, html code.
But you're mixing php and plain html a bit and lost in it, it should rather be:
echo '<img src="ad/data/img/'.$cus_id.'/'.$ad_id.'/'.$img_name.'" width="220" height="220"><br>';

No ?> before that block and no <?php after.

Upvotes: 1

Related Questions