Re: cant view image from database

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 2015-02-13 at 00:58 +0300, hadi wrote:
> 
> > -----Original Message-----
> > From: Maciek Sokolewicz [mailto:tularis@xxxxxxxxx] On Behalf Of Maciek
> > Sokolewicz
> > Sent: Thursday, February 12, 2015 10:18 PM
> > To: hadi; 'Stuart Dallas'; 'Bastien Koert'
> > Cc: php-general@xxxxxxxxxxxxx
> > Subject: Re:  cant view image from database
> > 
> > On 12-2-2015 20:00, hadi wrote:
> > >>> Hi,
> > >>
> > >>>
> > >>
> > >>> I can't view my image from mysql database.
> > >>
> > >>> I used curl to download the image to mysql database, and im trying
> > >>> to view
> > >>
> > >>> the image, im getting error from firefox debugger.
> > >>
> > >>>
> > >>
> > >>> Here is my code for downloading the image
> > >>
> > >>>
> > >>
> > >>> <?php
> > >>
> > >>>
> > >>
> > >>> $servername = "localhost";
> > >>
> > >>> $username = "recorduser";
> > >>
> > >>> $password = "password123";
> > >>
> > >>> $database = "record";
> > >>
> > >>>
> > >>
> > >>> // Create connection
> > >>
> > >>> $conn = mysqli_connect($servername, $username, $password,
> > >> $database);
> > >>
> > >>> // Check connection
> > >>
> > >>> if (!$conn) {
> > >>
> > >>>      die("Connection failed: " . mysqli_connect_error());
> > >>
> > >>> }
> > >>
> > >>>
> > >>
> > >>>
> > >>
> > >>>
> > >>
> > >>>
> > >>
> > >>>
> > >>
> > >>> $ch = curl_init
> > >>
> > >>> ("http://www.albaldnews.com/upimages/news/thumb_albald11-
> > >>
> > >>> 04-2014-993734.jpg"
> > >>
> > >>> );
> > >>
> > >>> curl_setopt($ch, CURLOPT_HEADER, 0);
> > >>
> > >>> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
> > >>
> > >>> curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
> > >>
> > >>> $rawdata=curl_exec ($ch);
> > >>
> > >>> curl_close ($ch);
> > >>
> > >>>
> > >>
> > >>> $rawdata = addslashes($rawdata);
> > >>
> > >>>
> > >>
> > >>>
> > >>
> > >>> $sql = "INSERT INTO picture (image)VALUES ('$rawdata')";
> > >>
> > >>> $result = mysqli_query($conn, $sql);
> > >>
> > >>>
> > >>
> > >>> ?>
> > >>
> > >>>
> > >>
> > >>> And the code for viewing the image as flow;
> > >>
> > >>>
> > >>
> > >>> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
> > >>
> > >>> "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";>
> > >>
> > >>> <html xmlns="http://www.w3.org/1999/xhtml";>
> > >>
> > >>> <head>
> > >>
> > >>>    <title>dispaly image</title>
> > >>
> > >>>    <meta http-equiv="Content-Type" content="text/html;
> > >>> charset=utf-8" />
> > >>
> > >>> </head>
> > >>
> > >>>
> > >>
> > >>> <body>
> > >>
> > >>>
> > >>
> > >>>
> > >>
> > >>> <?php
> > >>
> > >>> #header("Content-type: image/jpeg");
> > >>
> > >>>
> > >>
> > >>> $servername = "localhost";
> > >>
> > >>> $username = "recorduser";
> > >>
> > >>> $password = "password123";
> > >>
> > >>> $database = "record";
> > >>
> > >>>
> > >>
> > >>> // Create connection
> > >>
> > >>> $conn = mysqli_connect($servername, $username, $password,
> > >> $database);
> > >>
> > >>> // Check connection
> > >>
> > >>> if (!$conn) {
> > >>
> > >>>      die("Connection failed: " . mysqli_connect_error());
> > >>
> > >>> }
> > >>
> > >>>
> > >>
> > >>> $sql = "SELECT * FROM picture";
> > >>
> > >>> $result = mysqli_query($conn, $sql);
> > >>
> > >>>
> > >>
> > >>> header("Content-type: image/jpeg");
> > >>
> > >>> $row = mysqli_fetch_array($result);
> > >>
> > >>> $img = $row["image"];
> > >>
> > >>>
> > >>
> > >>> echo '<img src="'.$img.'" />';
> > >>
> > >>>
> > >>
> > >>>
> > >>
> > >>> ?>
> > >>
> > >>>
> > >>
> > >>> Firefox error code:
> > >>
> > >>>
> > >>
> > >>> <img src="http://192.168.206.129/rssfeed/showimage.php"; alt="The
> > >>> image
> > >>
> > >>> "http://192.168.206.129/rssfeed/showimage.php"; cannot be displayed
> > >> because
> > 
> > well of course you are...
> > 
> > Let's establish a few baselines:
> > 1 - an HTML page is basically a bunch of text data, which may contain
> > *links* to other data via URIs (such as an URL like
> > http://www.example.com/image.jpg or /images/image.jpg)
> > 2 - an image is basically a bunch of BINARY data (so NOT text(!!)) on its own.
> > 3 - combining BINARY data INSIDE an HTML file does NOT work (except under
> > very specific circumstances).
> > 
> > So... what did you do wrong?
> > Well, simply put:
> > 1 - you altered your image data by use of addslashes() to binary data.
> > Which means you've just corrupted the image-data, making it impossible to
> > show without undoing the damage. Using Stuart's suggestion you would not
> > have to do this.
> > 2 - You are putting BINARY data inside an HTML tag. The tag however
> > requires an URL and NOT raw data.
> > 
> > How to fix this?
> > 1 - get rid of the addslashes and use a prepared statement to insert the
> > image data.
> > 2 - instead of echoing the data, link it to a new PHP page (for instance
> > showImage.php)
> > 3 - inside this showImage.php file retrieve the image data just as you have
> > done above, but do NOT echo or print ANYTHING except this image data
> > from the database and a header with the content-type.
> 
> Hi Tul,
> 
> I did exactly what you suggest me. 
> Created link.html pointing to  showimage1.php. but still not working im unable to view the picture.
> also replaced addslashes with mysqli_real_escape_string
> 
> Link.html 
> 
> <html xmlns="http://www.w3.org/1999/xhtml";>
> <head>
>   <title>Comet php backend</title>
>   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
> </head>
> <body>
> 
> 
> <a href="showimage1.php">view picture</a>
> 
> 
> 
> 
> 
> </body>
> </html>
> 
> showimage1.php
> 
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";>
> <html xmlns="http://www.w3.org/1999/xhtml";>
> <head>
>   <title>dispaly image</title>
>   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
> </head>
> 
> <body>
> 
> 
> <?php error_reporting(E_ALL); ini_set('display_errors', 1);
> 
> $servername = "localhost";
> $username = "recorduser";
> $password = "password123";
> $database = "record";
> 
> // Create connection
> $conn = mysqli_connect($servername, $username, $password, $database);
> // Check connection
> if (!$conn) {
>     die("Connection failed: " . mysqli_connect_error());
> }
> 
> $sql = "SELECT * FROM picture";
> $result = mysqli_query($conn, $sql);
> 
> header("Content-type: image/jpeg");
> $row = mysqli_fetch_array($result);
> $img = $row["image"];
> 
> 
> echo $img;
> 
> 
> ?>
> 
> </body>
> </html>
> 
> 
>  
> 
> 
> 

Why are you outputting loads of HTML with the image in your
showimage1.php script? That's what's making the image invalid.

It really looks like you're trying to run before you can walk given some
of the questions you've asked over the past couple of weeks. As a start
you should at least learn HTML as that would help you a lot here, in
terms of understanding what an image is and how you use it within a web
page.

-- 
Thanks,
Ash
http://www.ashleysheridan.co.uk





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux