By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,485 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,485 IT Pros & Developers. It's quick & easy.

image display problem

P: 4
Hi all
the problem i am facing is different, i am trying to display images form database,i m giving code that i wrote
Expand|Select|Wrap|Line Numbers
  1.  
  2. $sql = "select * from upload "; 
  3.  
  4.                  $result = mysqli_query($link,$sql) or die ("Could not access DB: " . mysqli_error()); 
  5.  
  6.             echo "<table border=1>"; 
  7.                     while ($row = mysqli_fetch_array($result)) 
  8.                     { 
  9.  
  10.  
  11.  
  12.            //header("content-type: image/jpeg"); 
  13.           echo $row['name'] . "<br /> <br /><br />"; 
  14.           echo $row['content']; 
  15.          //echo "<img src=\"images/". $row['content'] . "\" alt=\"\"  /> <br /></td></tr>"; 
  16.                          //echo "</p>"; 
  17.                          //echo "</div>"; 
  18.  
  19.                     } 
  20.  
  21.          echo"</table>"; 
  22.  
  23.  
when i use the above code then the result come with whole bineary charactred,but the number of images shown are correct all 5 images are shown.

but if i remove the comment header statement then nothing happens
but just message is dispaly as below

"http://localhost/.../gallery.php"

I am confused wats happening
plz help me wat can be the problem. as the header statement is very important but if i uncommnet it above message come and if i comment it then every thing comes ok but it shows whole binary data insted of image.
so plz correct my code if u can
or any one has simple code as i am using BLOB datatype and images are getting uploaded properly in database.
thanks
in advance
the help will be really appriciated as i am unable to find solution since long time
Jan 26 '09 #1
Share this Question
Share on Google+
5 Replies


Atli
Expert 5K+
P: 5,058
Hi.

You can not simply echo binary data for an image into your HTML and expect it to be read as an image. The browser, expecting HTML, will simply display it as a bunch of random characters. (And might even cause random problems with your HTML).

And if you use the header function to set the content-type, you are setting it for the entire page, including the HTML, which will make the image render incorrectly. If you set the content-type to that of an image, the browser will be expecting image data only. Any HTML will be interpreted as a corruption in the image and it will not be rendered.

I made an article, explaining how to use files from a database: http://bytes.com/topic/php/insights/...base-using-php.
The part you would be interested in, specifically, is phase #4. The others, I assume, you have already done.

What needs to happen is: You need to create a page that fetches a single image and prints it using the appropriate headers (like phase 4 of my article does.). Each image needs to be fetched individually and placed into an <img> tag, so it will be displayed correctly.
Jan 26 '09 #2

P: 4
Hi Atli,
i read your article for displaying and uploading images.thanks for that, i have one question and in phase 3 , you passed the id of the file which u have to display , so that id came into phase 4, i want to ask is is that id necessary, or is that id just to pass the control to next phase.
Wat i am going to do in my web site is when use click the Gallery tab,it should show the gallery with 16pic of thumbnails on 1 page, i want to show the view only 48pic updated today and after login or by registering they can view whole gallery.
so after clicking the Gallery how can i pass the Id , is there any other solution to this, i dont know wat to do so since long time i am nt getting the solution for this , i request you to please give the solution i will be really thankful. And as u mention this code is not good as need to appy more security for uploading pic can i know wat sort of security i should apply while uploading pic.
Thanks in advance.
Urgently help and it will be highly appricated.
Jan 27 '09 #3

Atli
Expert 5K+
P: 5,058
@peacemaker2008
Well, if you don't pass the ID, the code in phase 4 will not know which file to display. You can of course use other methods of passing the ID between phases, like via a session variable.

@peacemaker2008
So you want to limit the number of pictures unregistered visitors can see?
You should know that there is really no way you can *reliably* partially restrict access to your files like this. Determined users will be able to get around any restrictions you put in place.
I would recommend trying to avoid such a tactic, but that's just me.

If you go ahead with this, there are a couple of methods you could try.

The easiest way to implement this would probably be to use Sessions.
(See http://bytes.com/topic/php/insights/644923-php-sessions if you are new to the concept).

You would simply create a session variable, and each time you display an image, you add 1 to the value of that variable.
Once that value reaches your intended maximum amount of free pictures, redirect the user to a "no more free stuff for you" message.

This would be pretty easy to bypass, however.
Simply closing and re-opening the browser would reset the count.

A more reliable method would be IP validation.
Meaning: log the IP of a user and keep the number of images he has viewed in a database, rather in the session.
This way the user would not be able to reset the count from his end. (Without getting a new IP address.)
(Note that this method can cause "collateral-damage", as in some cases a number of users could share a public IP)

@peacemaker2008
Ideally you would implement some sort of a user login mechanism (or at least visitor logging mechanism), to keep unwanted visitors from uploading files.

You would also want to verify that the data being uploaded is in fact the data you are expecting. Meaning; if you are expecting the user to upload an image, make sure that it is in fact an image.

The getimagesize function is a very easy way to do that. If it returns FALSE or if the mime-type it returns is not valid, you should reject the file being uploaded.
Jan 27 '09 #4

P: 4
hi Atil
thanks for fast reply again, sorry for my reply as i was busy with my work, but the question you raised abt the image viewing , i would like to comnent on it. Its not like that, if any one want to view my site then just the when they see gallary they should see only 2 or 3 pages of pics.They wil see the pics which are uploaded with in a weeks time or last 2 days , so accordingly the pics will be restricted.
And all the registered users can view whole gallary and upload pics and all.
So another thing is, as u said , i must pass the id, but if i pass the id, the sql query wil search pic for that perticular id. so how can i get the pics which i want, can i send the date to the query. i know its annoying for u to answer again the same, but sorry for that, i am confused with this thing how can i display image, i was able to upload image properly , but is creating headach for me. Can u solve my problem by correcting previous code for me plz.it will be great help.
thanks in advance.
Jan 30 '09 #5

Atli
Expert 5K+
P: 5,058
Ahh ok. So you don't want to restrict how many pictures they can see, but rather which images they can see?
That makes this a whole lot easier.

All you would have to do then, is create a SQL query that gets a list of images the user is able to view, based on the restrictions you want to put in place.
So if you only want then to see images added in the last two days, you could do something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT ImageID, ImageName 
  2. FROM ImageTable
  3. WHERE ImageCreated
  4.   BETWEEN DATE_SUB(NOW(), INTERVAL 2 DAY)
  5.   AND NOW();
Then, to display the image, you would pass the ID of the image to the code that actually gets the image data (like phase #4 in my article).
And to make sure the user could actually see the image, you would add the WHERE clause from the previous query to the query that fetches the data:
Expand|Select|Wrap|Line Numbers
  1. SELECT ImageData FROM ImageTable
  2. WHERE ImageID = {$imageID}
  3. AND ImageCreated
  4.   BETWEEN DATE_SUB(NOW(), INTERVAL 2 DAY)
  5.   AND NOW();
If this were to return 0 rows, the image would be restricted or the ImageID invalid, in which case you could print a nice X image or something to indicate that a forbidden image had been selected.
Jan 30 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.