Jump to content

Recommended Posts

Hi Everyone

 

Please accept my apologies if this is a totally obvious question. I'm totally new to PHP and mySQL.

 

Heres my problem.

 

I'm trying to build a database that contains images. From looking around I know the best way to do this is by storing a file path not the binary file. So I've been trying a load of different things, very simple. At the moment I just have a database called 'tester' a table called 'pics' and one row called 'images' with an attribute Whitchurch.jpg.

 

All I want is to link to this database and see the image and after hours this is the code I have ended up with....

 

<html>

 

<head>

<title>test</title>

 

</head>

 

<body>

<?php

       

        $db_conn = mysql_connect('localhost', 'root', 'root');

        if(!$db_conn){ die('Could not connect to database.'); }

 

       

        mysql_select_db('tester', $db_conn) OR die('Could not find database.');

       

        if ($_REQUEST[gim] == 1) {

        header("Content-type: image/jpeg");

        print $bytes;

        exit ();

        }

 

        $sql = " SELECT images FROM pics";

        $query = mysql_query($sql);

 

        while($row = mysql_fetch_array($query)){

                echo "<img src=\"/Desktop/images/Whitchurch.jpg/{$row['images']}\"><br />\n";

        }

?>

 

 

</body>

</html>

 

The actual image is in a file on my desktop called 'images' and the image is called 'Whitchurch.jpg'

 

When I run this I get a small blue box with a question mark in it, which believe me is the best result I've had for a while!

 

My questions are this :

 

1) Why isn't this working (obviously).

2) Do I need the complete filepath to be entered in the database?

3) Should my 'images' folder be somewhere else?

 

I'm using a Macbook Pro with MAMP and entering data with PHPmyadmin. Coding on Text Wrangler.

 

Any help/ knowledge more appreciated than you could ever realise!

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/232889-display-images-from-database/
Share on other sites

you would need the full path. in your case since its an image on your harddrive, thats not on some form of server or another it would be

 

C:\Users\{username}\Desktop\ (of course this would have to be altered to your systems direct path)

 

Also to just display

 

<img src=\"/Desktop/images/Whitchurch.jpg/{$row['images']}\">

 

you wouldnt need

header("Content-type: image/jpeg");

 

as the image tag is standard html, the use of the header like you have it is for all different types of reasons other than what I am gathering here..

 

Also keep in mind most browsers don't directly allow access to directories on peoples computers (including yours despit it being your code your computer etc) sometimes theres settings to disable to by pass that, also depends on your Operating System to, newer versions of Windows for example don't like to let many apps access the desktop without previous permissions given to the app(s)

 

 

Hey

 

Thanks loads for replying, this is driving me slightly insane, it can't be that difficult right?

 

Anyway I tried what you said and thought about the images file being on the hard drive, not usual.

 

So I moved it to the htdocs, in the MAMP folder in applications, this is where I have a folder named 'data' with the .php files.

 

Then I renamed the attribute in the database with the full filepath

 

/Applications/MAMP/htdocs/Data/images/Whitchurch.jpg

 

Then I placed this filepath in the code

 

echo "<img src=\"/Applications/MAMP/htdocs/Data/images/Whitchurch.jpg{$row['images']}\"><br />\n";

 

Still nothing except the little blue box with question mark.

 

However when I right click on it and say 'open image in new window' it comes up with this error message

 

Not Found

 

The requested URL /data/images/Whitchurch.jpg//Applications/MAMP/htdocs/Data/images/Whitchurch.jpg was not found on this server.

 

Apache/2.0.64 (Unix) PHP/5.3.5 DAV/2 Server at localhost Port 8888

 

Its got both the filepaths (from the code and the database) and can't find the image.

 

I'm obviously doing something wrong with the filepaths but I don't know what, and I think I'm not putting the actual image in the right place either.

 

Any ideas?

 

 

 

 

 

 

/data/images/Whitchurch.jpg//Applications/MAMP/htdocs/Data/images/Whitchurch.jpg

 

somewhere your getting a double // which is likely resulting in an invalid path.

 

so lets do this.. ill give you a quickie sample, not specific to what you have currently but to how I would handle it..

 

my database would be:

  - Name: imagesDB with 3 columns.

      - Column 1 > name > ID : Type INT(11) AUTO INCREMENT

      - Column 2 > name > pic: Type varchar(255)

      - Column 3 > name > picpath : Type varchar(255)

 

sample of the data stored per row

 

1 | myimage1.jpg | path/to/my/img/folder

2 | myimage2.jpg | path/to/my/img/folder

3 | myimage3.jpg | path/to/my/img/folder

 

how I would call it via the Query

 

If I wanted to yield all of them and list them out.

mysql_connect("localhost", "dbusername", "dbpassword") or die(mysql_error());
mysql_select_db("imagesDB") or die(mysql_error());

$result = mysql_query("SELECT * FROM imagesDB") or die(mysql_error()); 
$row = mysql_fetch_array( $result );

foreach($row as $value){
echo "<img src=\"http://www.mydomain.com/".$value['picpath']."/".$value['pic']." border=\"0\" alt=\"".$value['pic']."\" /><br /><br />";
}

 

if I wanted one in specific I would

mysql_connect("localhost", "dbusername", "dbpassword") or die(mysql_error());
mysql_select_db("imagesDB") or die(mysql_error());

$result = mysql_query("SELECT * FROM imagesDB WHERE ID=2") or die(mysql_error()); 
$row = mysql_fetch_array( $result );

echo "<img src=\"http://www.mydomain.com/".$row['picpath']."/".$row['pic']." border=\"0\" alt=\"".$row['pic']."\" />";

 

The above is core basic concept mind you. If you want a good reference site, I used http://www.tizag.com/ so many years ago when I was learning. Its not a bad resource and they have multiple resources from PHP to mySQL to JavaScript and so on, as far as the basic 101 type stuff goes.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.