HTML Tutorial


 Forum HomeForum Home   FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
RegisterRegister - Not registered yet? Got something to say? Join HTML Code Tutorial!
Display a picture (.jpg or .gif) from a mysql table
Goto page Previous  1, 2
Post new topic   Reply to topic    HTML Help Forum Index -> PHP
View previous topic :: View next topic  
Author Message
Keikura



Joined: 24 Mar 2007
Posts: 167
Location: U.K.

PostPosted: Sat Mar 24, 2007 5:24 am     Reply with quote

garykoz wrote:
So if I use files on the server I need to restrict access to the site somehow?

I'm still trying to figure out how to display them straight from the database. I have them loaded into the database as a MEDIUMBLOB file type. I've found a PHP function called "imagecreatefromstring". The format is imagecreatefromstring(image). The description says "Creates an image from the image stream in a string.

Now I'm searching for examples of the function.


If you want to restrict access to certain folders on your such as an images folder so that people can't just load it up and download all your images, use a .htaccess file. These will allow you to control who has direct access to folders etc, but you can still link to the images via a normal web page. For more info google .htaccess.

This may be of interest as well: http://www.javascriptkit.com/howto/htaccess10.shtml

For using images, PHP and MySQL this may of some help: http://www.wellho.net/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html

Keikura
garykoz



Joined: 25 Feb 2007
Posts: 13
Location: Rochester Hills, MI

PostPosted: Fri Apr 06, 2007 10:59 am     Reply with quote

Sorry it took so long to get back...

I went to that last link and it looks interesting. I'm going to give it a try..

Thanks again.
Keikura



Joined: 24 Mar 2007
Posts: 167
Location: U.K.

PostPosted: Fri Apr 06, 2007 2:23 pm     Reply with quote

No problem, let me know how it works out!

Very Happy
garykoz



Joined: 25 Feb 2007
Posts: 13
Location: Rochester Hills, MI

PostPosted: Sun Apr 15, 2007 7:20 am     Reply with quote

I haven't had much luck.

The file gets uploaded to the proper folder on the server. I seem to be stuck accessing the MySQL table. It look like I've connected to the database and table but inserting the record is not woking.

I used the code from the link:
http://www.wellho.net/solutions/php-example-php-form-image-upload-store-in-mysql-database-retreive.html

I have looked at some of my examples that work and see that the insert statement is a little different in the one I'm having trouble with.

I created the table with my GUI instead of using the code but it when I look at it with my GUI it looks OK.

*****************************************************
Here is the code I'm trying to run:
<?php

//
// This runs in httpdocs/PicLibTest
//


// Connect to database

$errmsg = "";
if (! @mysql_connect("localhost","htnkcc","hollis")) {
$errmsg = "Cannot connect to database";
}
Echo "Connected <br>";

@mysql_select_db("links");

Echo "Selected <br>";

// First run ONLY - need to create table by uncommenting this
// Or with silent @ we can let it fail every sunsequent time Wink

// $q = <<<CREATE
// create table pix (
// pid int primary key not null auto_increment,
// title text,
// imgdata longblob)
// CREATE;
// @mysql_query($q);

// Insert any new image into database

if ($_REQUEST[completed] == 1) {
// Need to add - check for large upload. Otherwise the code
// will just duplicate old file Wink
// ALSO - note that latest.img must be public write and in a
// live appliaction should be in another (safe!) directory.
move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
$instr = fopen("latest.img","rb");
$image = addslashes(fread($instr,filesize("latest.img")));
if (strlen($instr) < 149000) {
mysql_query ("insert into pix (title, imgdata) values (\"".
$_REQUEST[whatsit].
"\", \"".
$image.
"\")");
} else {
$errmsg = "Too large!";
}
}

// Find out about latest image

$gotten = @mysql_query("select * from pix order by pid desc limit 1");
if ($row = @mysql_fetch_assoc($gotten)) {
$title = htmlspecialchars($row[title]);
$bytes = $row[imgdata];
} else {
$errmsg = "There is no image in the database yet";
$title = "no database image available";
// Put up a picture of our training centre
$instr = fopen("../PicLibrary/Finale-1.jpg","rb");
$bytes = fread($instr,filesize("../PicLibrary/Finale-1.jpg"));
}

// If this is the image request, send out the image

if ($_REQUEST[gim] == 1) {
header("Content-type: image/jpeg");
print $bytes;
exit ();
}

?>

<html><head>
<title>Upload an image to a database</title>
<body bgcolor=white><h2>Here's the latest picture</h2>
<font color=red><?= $errmsg ?></font>
<center><img src=?gim=1 width=144><br>
<b><?= $title ?></center>
<hr>
<h2>Please upload a new picture and title</h2>
<form enctype=multipart/form-data method=post>
<input type=hidden name=MAX_FILE_SIZE value=150000>
<input type=hidden name=completed value=1>
Please choose an image to upload: <input type=file name=imagefile><br>
Please enter the title of that picture: <input name=whatsit><br>
then: <input type=submit></form><br>
<hr>
By Graham Ellis - grahamatwellho.net
</body>
</html>
*****************************************************

And here is the "view source" from when I execute it:
Connected <br>Selected <br><br />
<b>Warning</b>: mysql_query(): Access denied for user: 'apacheatlocalhost' (Using password: NO) in <b>/home/httpd/vhosts/lupusonline.org/httpdocs/PicLibTest/GaryLoadImages.php</b> on line <b>46</b><br />
<br />
<b>Warning</b>: mysql_query(): A link to the server could not be established in <b>/home/httpd/vhosts/lupusonline.org/httpdocs/PicLibTest/GaryLoadImages.php</b> on line <b>46</b><br />

<html><head>
<title>Upload an image to a database</title>
<body bgcolor=white><h2>Here's the latest picture</h2>
<font color=red>There is no image in the database yet</font>
<center><img src=?gim=1 width=144><br>
<b>no database image available</center>
<hr>
<h2>Please upload a new picture and title</h2>
<form enctype=multipart/form-data method=post>
<input type=hidden name=MAX_FILE_SIZE value=150000>
<input type=hidden name=completed value=1>
Please choose an image to upload: <input type=file name=imagefile><br>
Please enter the title of that picture: <input name=whatsit><br>
then: <input type=submit></form><br>
<hr>
By Graham Ellis - grahamatwellho.net
</body>
</html>
*****************************************************

As always any insight or suggestions will be greatly appreciated.
Keikura



Joined: 24 Mar 2007
Posts: 167
Location: U.K.

PostPosted: Sun Apr 15, 2007 11:16 am     Reply with quote

Going by the error message there's a few things that may be causing it...

Your user has access to MySQL otherwise the db select would of failed.
Your user may not have insert permissions for that db.
Your user might not have any permissions for that db at all.

What's on line 46?
garykoz



Joined: 25 Feb 2007
Posts: 13
Location: Rochester Hills, MI

PostPosted: Mon Apr 16, 2007 11:04 am     Reply with quote

I'm making progress. Good news and bad news...

Good news first. As soon as I included my own connection string it connected to the db.

Bad news... It doesn't display the image from the database. All I get is the image/icon you see when you try to display an invalid image. When I look at the database all the images I have loaded are there.

I'm confused by the "img src" code that is trying to display the image. I don't understand what the "?gim=1" is doing when it says:
<center><img src=?gim=1 width=144><br>

I saw this code:
if ($_REQUEST[gim] == 1) {
header("Content-type: image/jpeg");
print $bytes;
exit ();

Is "($_REQUEST[gim] == 1)" some special kind of PHP code? It seems as though this is the part taht isn't working.

*****************************************************


Here is all the code:
<?php

//
// This runs in httpdocs/PicLibTest
//

include ('/home/httpd/vhosts/lupusonline.org/httpdocs/includes/connect.txt');

// Connect to database

// $errmsg = "";
// if (! @mysql_connect("localhost","xxx","xxx")) {
// $errmsg = "Cannot connect to database";
// }
// mysql_select_db("xxxx");

// First run ONLY - need to create table by uncommenting this
// Or with silent @ we can let it fail every sunsequent time Wink

// $q = <<<CREATE
// create table pix (
// pid int primary key not null auto_increment,
// title text,
// imgdata longblob)
// CREATE;
// @mysql_query($q);

// Insert any new image into database

if ($_REQUEST[completed] == 1) {
// Need to add - check for large upload. Otherwise the code
// will just duplicate old file Wink
// ALSO - note that latest.img must be public write and in a
// live appliaction should be in another (safe!) directory.
move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
$instr = fopen("latest.img","rb");
$image = addslashes(fread($instr,filesize("latest.img")));
if (strlen($instr) < 149000) {
mysql_query ("insert into pix (title, imgdata) values (\"".
$_REQUEST[whatsit].
"\", \"".
$image.
"\")");
} else {
$errmsg = "Too large!";
}
}

// Find out about latest image

$gotten = @mysql_query("select * from pix order by pid desc limit 1");
if ($row = @mysql_fetch_assoc($gotten)) {
$title = htmlspecialchars($row[title]);
$bytes = $row[imgdata];
} else {
$errmsg = "There is no image in the database yet";
$title = "no database image available";
// Put up a picture of our training centre
$instr = fopen("../PicLibrary/Finale-1.jpg","rb");
$bytes = fread($instr,filesize("../PicLibrary/Finale-1.jpg"));
}

// If this is the image request, send out the image

if ($_REQUEST[gim] == 1) {
header("Content-type: image/jpeg");
print $bytes;
exit ();
}

?>

<html><head>
<title>Upload an image to a database</title>
<body bgcolor=white><h2>Here's the latest picture</h2>
<font color=red><?= $errmsg ?></font>
<center><img src=?gim=1 width=144><br>
<b><?= $title ?></center>
<hr>
<h2>Please upload a new picture and title</h2>
<form enctype=multipart/form-data method=post>
<input type=hidden name=MAX_FILE_SIZE value=150000>
<input type=hidden name=completed value=1>
Please choose an image to upload: <input type=file name=imagefile><br>
Please enter the title of that picture: <input name=whatsit><br>
then: <input type=submit></form><br>
<hr>
By Graham Ellis - grahamatwellho.net
</body>
</html>
Display posts from previous:   
Post new topic   Reply to topic    HTML Help Forum Index -> PHP All times are GMT - 8 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
HTML Help Archive
Powered by phpBB © 2001, 2005 phpBB Group
HTML Help topic RSS feed 

 
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   Link Directory   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial