header('Content-type: image/jpeg'); echo "<img src='$img'>";

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bimyona
Forum Newbie
Posts: 4
Joined: Thu Dec 10, 2009 9:44 pm

header('Content-type: image/jpeg'); echo "<img src='$img'>";

Post by bimyona »

probably isn't the right way to do it, but i'm trying to echo images out of mySQL.
either returns nothing, or i also got whitespace problem "Cannot modify header information - headers already sent by ..."

output.php

Code: Select all

 <?php   
    include("config.php");
    $query = "SELECT * FROM upload ";
    $result = mysql_query($query,$db);
 
            if(mysql_num_rows($result) == 0){
                echo "<br>image database empty<br>
                no picture was selected<br>";
            }
            else{       
                $query = "SELECT * FROM upload";
                $result = mysql_query($query);
                while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                    
                    $img = $row['content'];
                    
                    //set the header for the image
                    header('Content-type: image/jpeg');
                    echo "<img src='$img'>";    
 
                    //echo "{$row['name']}<br>";
                    //echo "{$row['size']}<br>";
                }
            }//end else
?>
 

any ideas? thanks.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: header('Content-type: image/jpeg'); echo "<img src='$img'>";

Post by requinix »

bimyona wrote:probably isn't the right way to do it
Uh yeah, the problem is about the right way to do it.

Use the header() call but output the image data. The raw data. No HTML. No whitespace. Just the image.
bimyona
Forum Newbie
Posts: 4
Joined: Thu Dec 10, 2009 9:44 pm

Re: header('Content-type: image/jpeg'); echo "<img src='$img'>";

Post by bimyona »

header('Content-type: image/jpeg');
echo "$img";

nice, displays one image... but only one image, not each one in my db.
student project due tomorrow. can you help?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: header('Content-type: image/jpeg'); echo "<img src='$img'>";

Post by requinix »

bimyona wrote:header('Content-type: image/jpeg');
echo "$img";

nice, displays one image... but only one image, not each one in my db.
Yeah. That's how it works. Two pages: one is the gallery page, one actually displays the image. This is the latter page; maybe you still need to make the former?
bimyona wrote:student project due tomorrow. can you help?
Depends how much work you decided to put off until the night before.
bimyona
Forum Newbie
Posts: 4
Joined: Thu Dec 10, 2009 9:44 pm

Re: header('Content-type: image/jpeg'); echo "<img src='$img'>";

Post by bimyona »

Depends how much work you decided to put off until the night before.
lol! I've been working in AS3 all week! I know what I'm doing there.
The PHP was just a bad after thought. To meet curriculum requirements ;)

The idea is a form to upload and display images I will later use in the Flash app.
Ideally, I wanted each image to display as a thumbnail on one page.
This can't be done? At some point I had one image displayed multiple times.

Taking your advice, I'm now trying to just link to the images, and open them on a separate page.
It ain't working either!

output.php

Code: Select all

<?php   
    include("config.php");  
        
    //SQL Query to select data from MySQL
    $query = "SELECT * FROM upload ";
    $result = mysql_query($query,$db);
 
            if(mysql_num_rows($result) == 0){
                echo "<br>image database empty<br>
                no picture was selected";
            }
            else{
 
                while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                    
                    $send = $row['name'];   
                    echo "filename: <a href='pic.php?$send' target='_blank'>{$row['name']}</a>";
                }           
            }
?>
pic.php

Code: Select all

<?php
    include("config.php");
 
    $img = $_GET['send'];
    $query = "SELECT content FROM upload WHERE name='$img'";
    $result = mysql_query($query);
    
    header('Content-type: image/jpeg');
        echo "<img src='$result'>"; 
?>
Thanks for your time!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: header('Content-type: image/jpeg'); echo "<img src='$img'>";

Post by requinix »

A few issues:

1.

Code: Select all

<a href='pic.php?$send' target='_blank'>
The links will look like pic.php?filename. Which is okay if you know how to handle that.
Here, you need to use a send=, like

Code: Select all

<a href='pic.php?send=$send' target='_blank'>
In case the name has weird stuff in it you should escape it.

Code: Select all

echo "<a href='pic.php?send=" . htmlentities($send, ENT_QUOTES) . "' target='_blank'>";

2.

Code: Select all

$img = $_GET['send'];
$query = "SELECT content FROM upload WHERE name='$img'";
- You don't know for sure that $_GET["send"] is present. Someone might have gone to pic.php. Use empty, and do something if the "send" isn't there.
- You can't trust anything coming from $_GET. That means you can't put it directly into a SQL query - people can mess with the database. Since you're dealing with strings use mysql_real_escape_string to make the string safe.

Code: Select all

$query = "SELECT content FROM upload WHERE name='" . mysql_real_escape_string($img) . "'";

3.

Code: Select all

$result = mysql_query($query);
 
header('Content-type: image/jpeg');
    echo "<img src='$result'>"; 
 
- mysql_query only executes the query - it doesn't get the results. Use a function like mysql_fetch_array to do that.
- If there weren't any results (maybe someone manipulated the URL?) then you won't have any image to print. mysql_fetch_array will return false if there was nothing found.
- If you're outputting an image (hint: you are) then the only thing you output is the image. As I said: no HTML and no whitespace.
bimyona
Forum Newbie
Posts: 4
Joined: Thu Dec 10, 2009 9:44 pm

Re: header('Content-type: image/jpeg'); echo "<img src='$img'>";

Post by bimyona »

beautiful! well... it certainly works!
thank you for your help!
i learned a lot ;)

output.php

Code: Select all

<?php
    //connect to db
    include("config.php");
    //include stylesheet
    include("style.php");
        
    //SQL Query to select data from MySQL
    $query = "SELECT * FROM upload ";
    $result = mysql_query($query,$db);
 
            if(mysql_num_rows($result) == 0){
                echo "<br>image database empty<br>
                no picture selected";
            }
            else{
                while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                    
                    $send = $row['name'];
    
                    //in case name has weird stuff in it, escape it.
                    echo "<a href='pic.php?send=" . htmlentities($send, ENT_QUOTES) . "' target='_blank'>{$row['name']}</a>&nbsp;&nbsp;";
                    echo "{$row['size']} kb<br>";
                }           
            }
?>
pic.php

Code: Select all

<?php
    include("config.php");
 
    $img = $_GET['send'];
 
    $result = mysql_query("SELECT content FROM upload WHERE name='" . mysql_real_escape_string($img) . "'");
    
    if (empty($img)) {
        echo 'no image';
    }
    
    else{
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {            
                header('Content-type: image/jpeg');
                //echo content as raw data / not HTML
                echo "{$row['content']}";
            }
    }
?>
Post Reply