473,378 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

view photos in a folder via the mysql

I have read and read about mysql, which I am sure is just like the quoted, "linux is very friendy, but it just chooses who it wants to be friends with..."

My first attempt to read the database in my forum actually works! Well, almost. It does query the correct places, lists the information, and I have even figured out how to see a full path to where the photo is stored.

But is never actually SHOWS the image.

My eventual goal is to simply input a member id# and it will list all of the photo attachments by that member.

Right now I would settle for it to work by displaying the photos found, (which does actually shows the path now, but not as a link or photo.) src and all that simply does not work, header ("Content-type: image/jpeg"); print $imagebytes;, fails as well, at least in any way I have tried.

Expand|Select|Wrap|Line Numbers
  1. <?
  2. $username="MyUserName";
  3. $password="MyPassWord";
  4. $database="MyDataBase";
  5. $x=date("j");
  6.  
  7. mysql_connect(localhost,$username,$password);
  8. @mysql_select_db($database) or die( "Unable to select database");
  9. $query="SELECT * FROM ibf_attachments";
  10. $result=mysql_query($query);
  11.  
  12. $num=mysql_numrows($result);
  13.  
  14. mysql_close();
  15.  
  16. echo "<b><center>Database Output</center></b><br><br>";
  17.  
  18. $i=0;
  19. while ($i < $num) {
  20.  
  21. $member=mysql_result($result,$i,"attach_member_id");
  22. $isimage=mysql_result($result,$i,"attach_is_image");
  23. $imagedate=mysql_result($result,$i,"attach_date");
  24. $imagesize=mysql_result($result,$i,"attach_filesize");
  25. $imagewidth=mysql_result($result,$i,"attach_img_width");
  26. $imageheight=mysql_result($result,$i,"attach_img_height");
  27. $imagelocation=mysql_result($result,$i,"attach_location");
  28.  
  29. // this sets the image location to a full path
  30. $full_location = "http://www.MySite.com/forum/uploads/$imagelocation";
  31.  
  32.  
  33.  
  34. echo "<b>$member $isimage</b><br>
  35. Image Date: $imagedate<br>
  36. Image Size: $imagesize<br>
  37. Image Width: $imagewidth<br>
  38. Image Height: $imageheight<br>
  39. location: $imagelocation  <br>
  40. <br><br>
  41.  
  42. // this actually shows the link, but not clickable, nor will it display image here.
  43. // I can copy and paste it in browser and it is correct path.
  44. full location: $full_location<br>
  45.  
  46. <hr><br>";
  47.  
  48. ?>
  49.  
  50. <?PHP
  51.  
  52. $i++;
  53. }
  54.  
  55. ?>


any help would be apprecitated!
Nov 29 '09 #1

✓ answered by Frinavale

Your problem has very little to do SQL.
In fact it sounds like you're connecting to the database and retrieving the data correctly!
Your MySQL Database simply holds the URLs to the images that you want to display.

MySQL is a database of information...it's not supposed to "Display" anything at all. It just holds information. It is the browser's job to display things...and the only way the browser is going to know How you want to display something is if you use HTML to describe how you want things to be displayed.

So, when you retrieve the data from the SQL database you need to create/generate HTML to tell the web browser how to display the data. This means that if you want the web browser display an image, you have to use the HTML <img> tag to display the image.

Did you try:
Expand|Select|Wrap|Line Numbers
  1. echo "<b>$member $isimage</b><br>
  2. Image Date: $imagedate<br>
  3. Image Size: $imagesize<br>
  4. Image Width: $imagewidth<br>
  5. Image Height: $imageheight<br>
  6. location: $imagelocation  <br>
  7. Image: <img src='$full_location' alt='the image' />  <br>
  8. <br><br>";
???

-Frinny

22 3995
Dormilich
8,658 Expert Mod 8TB
if you want to display an image in HTML you have to use the <img> tag pointing to the picture, which may be either the file of the image or a script file, that fetches the image and returns the binary data.
Expand|Select|Wrap|Line Numbers
  1. // in HTML (image from DB)
  2. <img src="image.php?id= xyz" width="…" height="…" alt="…">
Expand|Select|Wrap|Line Numbers
  1. // image.php
  2. header("Content-Type: image/jpeg"); // or whatever image type you have
  3. // connect to DB
  4. // query DB for image (using $_GET)
  5. // return data:
  6. echo $image;
  7. // close DB
see also the Insights Article Uploading files into a MySQL database using PHP (chapter 4)

PS. the only functions/packages that can fetch the whole result set into an array are PDO and MySQLi, both available in PHP 5 …
Nov 29 '09 #2
thanks. I appreciate your attempt to assist, It doesn't help me, as I do not understand it, but I appreciate it. (and I do have php5 on the server)

I need to know how to make $imagelocation in the above PHP file show as a photo, not a non-clickable link.
Nov 30 '09 #3
Frinavale
9,735 Expert Mod 8TB
Hi skysober,

I am by no means a PHP expert but I can explain why your image doesn't show up.

First of all, in order to display an image in your web page you have to use the HTML <img> tag. See w3c for more information about the HTML img tag.

When a webpage renders an image it needs to retrieve that image from the web server. That means that the image has to exist on the web server in order for the browser to retrieve it.

In your case there is no image on the web server for the web page to retrieve....your image is stored in a database.

So, how do you get around this?
Well, instead of having the web page retrieve the image from a file on the web server, have it retrieve the image by calling a php script that retrieves the image from the database.

This php script does not return HTML like a normal php script would.
Instead this php script will retrieve the bytes that are the image from the database. Once the php script has retrieved the image it will send the bytes (that are the image) to the browser.

Since the script does not return HTML you have to change the Response Content-Type Header. That way when the image is sent to the browser, the browser will know that it is an image (and not HTML) so that it can render it properly.

With this in mind, take a look at the suggestion that Dormilich posted.
He suggested that you create a php file that reads the image from the database into memory...change the Content-Type to "image"...and send the image to the browser.

In order for your page to display the image you need to use an HTML <img> tag (Dormilich has posted code showing you how to use this).

The <img> tag has to call the php script that returns the image in order to display the image in the web page. You will have to provide the ID of the image as a parameter to the php script so that it can retrieve the right image......

In order words, take a look at what Dormilich has recommended ;)

-Frinny
Nov 30 '09 #4
The image is on the server. It is in the uploads folder. If I copy paste the result of $imagelocation into my browser, it shows the image just fine.

I have no clue as to what a somthing.php?something means. I only know html. basic PHP, an almost no SQL. This was my first attempt at reading a SQL, something that is probably quite easy for some, but is quite confusing to me.

I do know the images themselves are not stored in mysql, but actually stored in the upload folder and the mysql records that location. That confuses me a lot when someone says they are stored in the mysql.

Thanks!
Nov 30 '09 #5
Frinavale
9,735 Expert Mod 8TB
I'm not really sure that I understand your problem completely but one thing is for sure: the image is not stored in a database.

If your image is on the server, then it has to be in a directory that is accessible to web browsers.

Make sure that your image is moved to a directory that is in your website.
Once it is there your img tag will be able to download it.

For example, if you move your image to a folder called "images" that is within your website on your sever your img tag would look like:

Expand|Select|Wrap|Line Numbers
  1. <img src='www.mydomain.com/images/theImage.jpg' alt='the image' />
  2.  
In your case the URL to the image is stored in the $full_location variable...so you would have something like:

Expand|Select|Wrap|Line Numbers
  1. <img src='<?php echo $full_location; ?>' alt='the image' />
I have no idea how your database is even involved in this problem...???

-Frinny
Nov 30 '09 #6
Thanks, but this is turning away from the sql problem. HTML I know. I can make a link to a photo in a folder.

The mySQL simply stores the photos locations. When a member uploads a photo into uploads, the sql records this. I wish to read the sql, have it finds the name and location of the photos, and and then show the photos by that member.

My original code that I made at the beginning of this thread does all this, except actually showing the photos. Instead of the photos, it gives a text of "http://www.MySite.com/forum/uploads/NameOfThe1stPhoto.jpg." , "http://www.MySite.com/forum/uploads/NameOfThe2ndtPhoto.jpg." , etc.
Nov 30 '09 #7
this is my present result when I run the peek.php

319 1
Image Date: 1235181668
Image Size: 88175
Image Width: 739
Image Height: 1000
location: monthly_02_2009/post-319-1235181668.jpg


full location: http://www.MySite/forum/uploads/mont...1235181668.jpg
Nov 30 '09 #8
Frinavale
9,735 Expert Mod 8TB
Your images are stored on the web server, in a folder in the website.
Your database stores URL information that can be used to display the images uploaded by the user.

You have SQL that retrieves the URLs for the photos.....

You know that you need an <img> tag to display an image....

Why can't you display the images??

-Frinny
Nov 30 '09 #9
Now that is the $64,000 question that I asked here in the first place ;)

As I just posted the result when I run peek.php, it does NOT show the photo.
Nov 30 '09 #10
Frinavale
9,735 Expert Mod 8TB
Oh wait a second, you don't want to display the image (maybe??)...you just want to create a hyperlink to the image???

A hyperlink (a "link") is created using the HTML <a> tag (an anchor).

In this case you would use the <a> tag instead of the <img> tag and use the href property instead of the src property....

Do you have a public link to this website?

-Frinny
Nov 30 '09 #11
Frinavale
9,735 Expert Mod 8TB
I have a feeling that you aren't creating the URL to the image properly.
Have you researched the $_Server variable?

$_SERVER[’PHP_SELF’], $_SERVER['REQUEST_URI'], and $_SERVER[’SCRIPT_NAME’] all return information about the php script that was requested. Maybe you could use one of these to find the URL to the images?

Like I said I'm not a PHP expert, so I'm not entirely sure what the best way to get the URI of your website.....


One other thing, are you Sure that the uploads folder is accessible?

-Frinny
Nov 30 '09 #12
the resulting url is perfect. I can copy paste it into a new browser page and it works 100% showing the photo.

thanks. I give up on this. reading SQL is not going to show the photo no matter what I try. I have spent about 25 hours fighting this. I'm sure an sql xpert does this on a daily basis and is a joke for him, but I can't figure it out.

Thanks for trying!
Nov 30 '09 #13
Frinavale
9,735 Expert Mod 8TB
Your problem has very little to do SQL.
In fact it sounds like you're connecting to the database and retrieving the data correctly!
Your MySQL Database simply holds the URLs to the images that you want to display.

MySQL is a database of information...it's not supposed to "Display" anything at all. It just holds information. It is the browser's job to display things...and the only way the browser is going to know How you want to display something is if you use HTML to describe how you want things to be displayed.

So, when you retrieve the data from the SQL database you need to create/generate HTML to tell the web browser how to display the data. This means that if you want the web browser display an image, you have to use the HTML <img> tag to display the image.

Did you try:
Expand|Select|Wrap|Line Numbers
  1. echo "<b>$member $isimage</b><br>
  2. Image Date: $imagedate<br>
  3. Image Size: $imagesize<br>
  4. Image Width: $imagewidth<br>
  5. Image Height: $imageheight<br>
  6. location: $imagelocation  <br>
  7. Image: <img src='$full_location' alt='the image' />  <br>
  8. <br><br>";
???

-Frinny
Nov 30 '09 #14
Markus
6,050 Expert 4TB
I'm having a hard time following this thread (be it through no fault of your own, I'm just slightly sleepy with some wine in my blood).

Let us reiterate the exact problem; I don't want to know about anything that isn't specifically related to this problem - that only clouds my vision.

So, try this: *what* are you trying to do? What have you tried? What *isn't* working? What *is* working? What *errors* do you get?

No need to give up :)

Mark.

Edit: unless Frin's latest post clears the situation up.
Nov 30 '09 #15
wtf! You code works! I dunno what u did dif than I did with the src, but yours shows the photo! Now I can actually start to work with it where I can choose it to only show a member specific photos.

THANK YOU SO MUCH!!!!!
Nov 30 '09 #16
Frinavale
9,735 Expert Mod 8TB
Hah :)

I'm glad you solved your problem.

Happy coding!

-Frinny
Nov 30 '09 #17
Thanks again! I can honestly say I think SQL is a neccesary EVIL ;)


hehe...

THANKS for the help!!!
Dec 1 '09 #18
Dormilich
8,658 Expert Mod 8TB
I can honestly say I think SQL is a neccesary EVIL
I wouldn’t go as far as that… after all, it’s logical
Dec 1 '09 #19
Markus
6,050 Expert 4TB
@skysober
You'd be in a hell of a worse situation if you didn't have SQL (or any other query language). Flat file database? Ick!
Dec 1 '09 #20
Dheeraj Joshi
1,123 Expert 1GB
Yeah Mark, if there was no SQL, i will be killing myself :)

Regards
Dheeraj Joshi
Dec 1 '09 #21
Frinavale
9,735 Expert Mod 8TB
I can honestly say I think SQL is a neccesary EVIL
SQL is not evil once you understand it and how databases work.

It's funny but I do the same thing: I blame the thing I know the least about for the problems I'm experiencing. Sometimes it takes a while to see that it's something that I know that's causing the problem.

-Frinny
Dec 1 '09 #22
Markus
6,050 Expert 4TB
@Frinavale
It's always much easier and more satisfying to blame anyone and anything other than yourself ^_^
Dec 1 '09 #23

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

Similar topics

6
by: Larry R Harrison Jr | last post by:
I have Access XP, and the following code which is supposed to assign a JPEG to an image control: Me.Image9.Picture = "F:\Pictures\CP775_SonyDSCP50\FingerRockNight_Resize.jpg" I get this error...
3
by: Robert Lochon | last post by:
Hi, I'm trying to transfer the photos from my Canon EOS 20D to a folder on my hard-drive with a C# program (but the language doesn't matter). But the cam folder doesn't appear in the directory...
2
by: Ross | last post by:
Hi I have an application using asp.net that I am running on my PC. The web form has a text box where you can enter a name for a new Photo category then click on the button. The code is...
4
by: Dave G | last post by:
Firstly, apologies as this is not strictly an Access problem. I have a Access 2003 database containing records about people, and each person has 2 photos associated with the record. The photos...
2
by: =?Utf-8?B?TGlhbQ==?= | last post by:
My Photos folder opens automatically on startup and re-opens instantly when I close it. Occasionally a small black banner appears and disappears with the words "My Photos" on it. My Photos keeps...
10
by: K. | last post by:
Hello all! I have a question to you. I would like to create photo gallery. I wonder if I should store photos (uploaded by users) in database $data = file_get_contents($_FILES); $data =...
1
by: David C | last post by:
We have an intranet application that displays file folders assigned to that job. Each file folder has 5 subfolders under it and are listed in a TreeView. When we click on the folder it shows the...
1
by: cumupkid | last post by:
II am trying to create a form that will allow me to upload photos to a folder in the site root directory and add the information to the mysql db at the same time. I have created two forms, one...
6
by: gubbachchi | last post by:
Hi, Which is the best data type to store photos in mysql. BLOB is same as varchar type and increases the search time. Apart from BLOB, is there any other data type to store photos.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.