The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Best method to access pictures from mySQL.
Discuss Best method to access pictures from mySQL. in the PHP Development forum on Dev Shed. Best method to access pictures from mySQL. PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 28th, 1999, 12:57 AM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
|
Hi,
I'm planning on letting my users add pictures to their pages using the browser upload method.
My question is, which is better? adding the picture to the database? or storing them on a separte directory and add a link to the page?
I also will be letting each user have quota on the disk space.
Your thoughts and suggestions appreciated.
Cheers!
|

September 28th, 1999, 05:05 PM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
|
Its usually best to store the picture as a file, and then store the location of that file in the database.
------------------
URL
www.devshed.com
|

September 28th, 1999, 09:52 PM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
|
Along the lines of this thread: more questions.
I've been able to configure my debian/linux server to use numerous php scripts including horde/imp, phorum etc., which are great apps that I love. I've gone through a number of tutorials and searched numerous archives for answers to my questions. At this point, I am confused enough to be considering renting all available Captain Kangaroo videos and ...
My concerns are entering imgage file locations into a mysql database, extracting the correct data, associated by "id," and then displaying the php dynamically generated pages.
The mysql database, table, column is a "varchar." My entry currently reads:
images/test01.gif
Is this the correct syntax for telling mysql where an image file is?
I've attempted numerous self construed abominations to printing the appropriate image; including, seperate "printf" commands for the image files, two of which are commented out in the full script below.
The following works perfectly for my text fields:
<?php
snip
printf("<tr><td><a href="%s?id=%s">%s %s </a></td></tr
><br>n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]);
snip
?>
This is the full code for my script which is modified from the hotwire webmonkey tutorial "http://www.hotwired.com/webmonkey/99/21/index2a.html?tw=frontdoor":
<?php
include("class.layout");
include("header.inc");
$db = mysql_connect("localhost", "username", "passwd");
mysql_select_db("mplsesc",$db);
// display individual record
if ($id) {
$result = mysql_query("SELECT * FROM poets WHERE id=$id",$
db);
$myrow = mysql_fetch_array($result);
printf("<P>Poet: %sn<br>", $myrow["thumbs"]);
printf("<P>First name: %sn<br>", $myrow["first"]);
printf("Last name: %sn<br>", $myrow["last"]);
printf("E-mail: %sn<br>", $myrow["email"]);
printf("HomePage: %sn<br>", $myrow["homepage"]);
printf("Description: %sn<br>", $myrow["bio"]);
printf("Availability: %sn<br>", $myrow["avail"]);
} else {
// no records to display
echo "Sorry, no records were found!";
}
}
include("footer.inc");
?>
Sorry about writing a book. All help is appreciated and reciprocated.
ron parker
www.mi-recordz.com (Free services for all independent artists.)
rtp@mi-recordz.com
|

September 29th, 1999, 12:30 AM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
|
whoops, chopped the code.
I've been able to configure my debian/linux server to use numerous php scripts including horde/imp, phorum etc., which are great apps that I love. I've gone through a number of tutorials and searched numerous archives for answers to my questions. At this point, I am confused enough to be considering renting all available Captain Kangaroo videos and ...
My concerns are entering imgage file locations into a mysql database, extracting the correct data, associated by "id," and then displaying the php dynamically generated pages.
The mysql database, table, column is a "varchar." My entry currently reads:
images/test01.gif
Is this the correct syntax for telling mysql where an image file is?
I've attempted numerous self construed abominations to printing the appropriate image; including, seperate "printf" commands for the image files, two of which are commented out in the full script below.
The following works perfectly for my text fields:
<?php
snip
printf("<tr><td><a href="%s?id=%s">%s %s </a></td></tr
><br>n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]);
snip
?>
This is the full code for my script which is modified from the hotwire webmonkey tutorial "http://www.hotwired.com/webmonkey/99/21/index2a.html?tw=frontdoor":
<?php
include("class.layout");
include("header.inc");
$db = mysql_connect("localhost", "username", "passwd");
mysql_select_db("mplsesc",$db);
// display individual record
if ($id) {
$result = mysql_query("SELECT * FROM poets WHERE id=$id",$
db);
$myrow = mysql_fetch_array($result);
printf("<P>Poet: %sn<br>", $myrow["thumbs"]);
printf("<P>First name: %sn<br>", $myrow["first"]);
printf("Last name: %sn<br>", $myrow["last"]);
printf("E-mail: %sn<br>", $myrow["email"]);
printf("HomePage: %sn<br>", $myrow["homepage"]);
printf("Description: %sn<br>", $myrow["bio"]);
printf("Availability: %sn<br>", $myrow["avail"]);
} else {
// show list
// show list
$result = mysql_query("SELECT * FROM poets",$db);
if ($myrow = mysql_fetch_array($result)) {
// display list if there are records to display
do {
echo "<TABLE BORDER=1 >n";
//printf ("$thumbs = file_exists("../images/thumbs/$id.jpg
//") ? "<IMGSRC="/images/thumbs/$id.jpg">" : "No Photo //Yet<br>n", $PHP_SELF, $myrow["id"], $myrow["thumbs"] );
printf("<tr><td><a href="%s?id=%s">%s %s </a></td></tr
><br>n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]);
//printf ("<tr><td><img src="/images/$id.jpg"></td></tr>
//<br>n", $PHP_SELF, $myrow["id"], $myrow["thumbs"] );
echo "</TABLE>n";
} while ($myrow = mysql_fetch_array($result));
} else {
// no records to display
echo "Sorry, no records were found!";
}
}
include("footer.inc");
?>
Sorry about writing a book. All help is appreciated and reciprocated.
ron parker
www.mi-recordz.com (Free services for all independent artists.)
rtp@mi-recordz.com
|

September 29th, 1999, 05:06 AM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
|
I was able to get this to work. I'm not looping them though. I'm including this in as <IMG SRC="<? include("imgmain.php3")?>">:
<?php
$rootpath = getenv("REQUEST_URI");
$dirpath = dirname($full_path);
$filename = basename($full_path);
$filenum = substr($filename, strrpos($filename, "=") + 1);
print "$dirpath/images/$filenum.jpg";
printf("" ALT="%s", $row["title"]);
?>
Is the lack of a space between IMG and SRC in your thumbnail code above a direct copy from your working files?
[This message has been edited by louisra (edited 09-29-99).]
|

September 29th, 1999, 04:22 PM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
|
louisra,
Thanks for sharing that code and your time. I am try to interpret what your code does and how to use your strategy or see from your example what I might be missing. This is all pretty new and takes some time for me to understand. Excellent, than you!
The following printf produces the broken image icon and then the path statement, that's entered into the thumbs column of my database:
printf ("<tr><td><img src="%s?id=%s">%s </td></tr>
<br>n", $PHP_SELF, $myrow["id"], $myrow["thumbs"] );
The result is the path "/home/will/www/images/thumbs/test01.jpeg" printed to the html page with and a broken image icon.
I was wondering if my problem is not knowing how to correctly enter the file location within the database thumbs column. Does it require the full path or is it still something else?
If I can get the image to print then I won't worry about "file_exists" until a later time. This isn't a huge application with alot of changes so I can do some by hand. The printf with the file_exists is where you caught my typo. URL
|

October 18th, 1999, 01:48 AM
|
|
Guest
|
|
Posts: n/a
Time spent in forums:
Reputation Power:
|
|
|
There is a simplistic answer to the above that does NOT require that the database store any info about the image directly. I simply save the image AS the id number for that record, and call it with something like:
echo "<img src='thumbs/$id'>"
For example, the following script calls a recordset as an array, and loads the appropriate thumbnail image for each record automatically:
$db = mysql_connect("localhost", "webuser", "");
$query = "SELECT id, first, last, address, position FROM employees";
$result = mysql_db_query("mydb", $query);
if ($result) {
echo "Found these entries in the database:<ul>";
while ($r = mysql_fetch_array($result)) {
$id = $r["id"];
$first = $r["first"];
$last = $r["last"];
$address = $r["address"];
$position = $r["position"];
echo "<li><a href='imgdetail.php3?id=$id'><img src='thumbs/$id'></a> $first $last; $address; <b>$position</b>";
}
echo "</ul>";
} else {
echo "No data.";
}
mysql_free_result($result);
?>
HINT: you don't even need to add .gif or .jpg to the name of your image, just save it without an extension. The browser will just assume it's a standard web image and load it. Also, if you are afraid of having a "broken link" icon appear if there is no picture, you can either have PHP check for a file first or even more simply, have a standard image "picture not available" which can be replaced anytime by the user's image.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|