skip to Main Content

I am fairly new at this, I was trying to display and download images stored in database as a image path. the display works fine but when I try to download the images from the download button the URL shows the full path of the image but the download dosent happen.

$sql_img = "SELECT * FROM cms
            JOIN images ON cms.C_ID = images.C_ID
            WHERE B_Name = '$select'";
$sql_img_run = mysqli_query($con, $sql_img);

            ?>
            
            <table class="display_img">
                    <tr>
                        <th>Image ID</th>
                        <th>Cms ID</th>
                        <th>Images</th>
                    </tr>

            <?php

            while ($row = mysqli_fetch_array($sql_img_run)) {

                $img_id = $row['Img_ID'];
                $image = $row['Image'];
                $cms_id = $row['C_ID'];
            
                // $path = "C:xampphtdocsAiranApi";
                $path = "http://localhost/Airan/api/";
                ?>
                    <tr>
                        <td><?php echo $img_id ?></td>
                        <td><?php echo $cms_id ?></td>
                        <td><?php echo '<img height="100" src='.$path.$image.'>'?></td>
                        <td>
                            <a href="home.php?Image=<?php echo $path.$image; ?>" class="btn-primary download-btn">Download</a>
                        </td>
                    </tr>

I have tried various methods but nothing works.

2

Answers


  1. Chosen as BEST ANSWER

    the solutions to this was purely simple. only adding the download attribute to the <a> was downloading the whole page because there was no specific path given to the download attribute.

    <a download="<?php echo $path.$image; ?>" href="<?php echo $path.$image; ?>" class="btn-primary download-btn">Download</a>

    This works like a charm.

    Thankyou guys, you gave your precious time to help resolve the issue. I did get to learn a lot of things from you guys. It wouldn't have been possible without you guys.


  2. An example, using hardcoded image paths and nonsense IDs to emulate what you might be drawing from the database.

    Use the GET method as you are using a hyperlink with Image parameter in querystring.

    Do not output HTML ( or any content ) before calling header function unless you explicitly flush the buffers before.

    Add the download attribute to each hyperlink.

    <?php
    
        error_reporting( E_ALL );
        ini_set( 'display_errors', 1 );
        
        
        # simple function to read file and send the binary contents
        function sendfile( $filename=NULL, $filepath=NULL ){
            if( file_exists( $filepath ) ){
                if( !is_file( $filepath ) or connection_status()!=0 ) return FALSE;
                header('Content-Type: application/image');
                header('Content-Length:'.filesize( $filepath ) );
                header("Content-Length: ".(string)( filesize( $filepath ) ) );
                header("Content-Disposition: attachement; filename={$filename}");
                header("Content-Transfer-Encoding: binaryn");
                if( $file = @fopen( $filepath, 'rb' ) ) {
                    while( !@feof( $file ) and ( connection_status()==0 ) ) {
                        print( fread( $file, 1024*8 ) );
                        flush();
                    }
                    @fclose( $file );
                }
                return( ( connection_status()==0 ) and !connection_aborted() );
            }
        }
        
        /*
            intercept the GET request, read the `Image` parameter
            and call the above function.
        */
        if( isset( $_GET['Image'] ) ){
            
            $name=pathinfo( $_GET['Image'], PATHINFO_BASENAME );
            $path=__DIR__ . '/'. $_GET['Image'];
            
            # send the file and terminate.
            exit( sendfile( $name, $path ) );
        }
    ?>
    
    
    <!DOCTYPE html>
    <html lang='en'>
        <head>
            <meta charset='utf-8' />
            <title>Download images...</title>
        </head>
        <body>
            <!--
                in this example images are located within a sub-directory of the current
                working script. The sub-folder is called "carousel" - the images are sequentially
                named using numbers (that is not important here however)
            -->
            <table class='display_img'>
                <tr>
                     <th>Image ID</th>
                     <th>Cms ID</th>
                     <th>Images</th>
                </tr>
                <tr>
                     <td>1</td>
                     <td>23</td>
                     <td><img height="100" src='./images/carousel/01.jpg'></td>
                     <td><a href='?Image=images/carousel/01.jpg' download><button class="btn-primary download-btn" name="download_btn">Download</a></td>
                </tr>
                <tr>
                     <td>2</td>
                     <td>23</td>
                     <td><img height="100" src='./images/carousel/02.jpg'></td>
                     <td><a href='?Image=images/carousel/02.jpg' download><button class="btn-primary download-btn" name="download_btn">Download</a></td>
                </tr>
                <tr>
                     <td>3</td>
                     <td>23</td>
                     <td><img height="100" src='./images/carousel/03.jpg'></td>
                     <td><a href='?Image=images/carousel/03.jpg' download><button class="btn-primary download-btn" name="download_btn">Download</a></td>
                </tr>       
            </table>
        </body>
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search