Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Upload with MySQL 1

Status
Not open for further replies.

digatle

Technical User
Oct 31, 2003
85
0
0
US
I'm wondering if there is a way to store an image in MySQL or if I have to put it somewhere that would be fine too. Here's what my code looks like:

<?php
print &quot;<html><head><title>StockNet</title></head><body bgcolor=\&quot;#FFFFFF\&quot;>&quot;;
print &quot;<center><form><table width=\&quot;400\&quot;><tr><td colspan=\&quot;2\&quot;><center><b>Add an Image to StockNet</b></center></td></tr>&quot;;
print &quot;<tr><td width=\&quot;50%\&quot;>Description:</td><td width=\&quot;50%\&quot;><input type=\&quot;text\&quot; name=\&quot;description\&quot; size=\&quot;25\&quot;></td></tr>&quot;;
print &quot;<tr><td width=\&quot;50%\&quot;>Department:</td><td width=\&quot;50%\&quot;></td></tr>&quot;;
// Department List Here
// Height Variant on # of Options
// Allows more of a breakdown for multiple categories
print &quot;<tr><td width=\&quot;50%\&quot;>Global:</td><td width=\&quot;50%\&quot;></td></tr>&quot;;
print &quot;<tr><td width=\&quot;50%\&quot;>Location of File:</td><td width=\&quot;50%\&quot;><input type=\&quot;file\&quot; name=\&quot;image\&quot; size=\&quot;25\&quot;></td></tr>&quot;;
print &quot;</table></body></html>&quot;;
?>


What I'm trying to do is consolidate our images into something I'm calling StockNet. Our intranet name is SpiderNet (Don't ask me, I didn't name it).

What I'm wanting to do totally is I have a database with the following structure:

description, url, blog_id, global

Totally what I would like it to do is blog_id and category_id really control our site's layout. When we add a division to our company we going and ad a category_id with the key blog_id. Blog_id of course is our Divisions. Fortunantly our divisions are color coded internally (You can see a sample at:


You say with the description of the image is (alt), what division it goes under (we have 6 divisions all within a database (code to be added in a bit) and an option whether it is &quot;global&quot; meaning on the right hand side we have a &quot;non color coded&quot; image for our applications and external links.

My question really is can a PHP page be written so that the image is stored in MySQL (url) or do I have to actually &quot;upload it&quot; somewhere? It'd be a lot easier if it was in MySQL IMHO.

Digatle
 
I strongly recommend that you not store images in a database, but rather on the filesystem.

If you store images on the filesystem of your server and store image filenames in the database,then all you have to do in PHP is fetch the filename and output it in an <IMG> tag. The browser will then request the image directly from your web server.

If you store the actual image in the server, you'll have to fetch some unique identifier for the image and output it in an <IMG> tag. Then you will have to create a second script that will send the appropriate Content-type header, fetch the data, and stream it out through the server to the browser.

In the first scenario, you have to write a minimum of code, and the image is streamed by compiled code in your web server that is specifically optimized to this task.

In the second scenario, the image data will have to be streamed first from MySQL to PHP then from PHP out (through the server twice) using fast but still interpreted code.



But whether you use the images from the filesystem or from MySQL, the image data will still have to be uploaded. And again, you can either use compiled code to stream the data to the filesystem or interpreted code to put the image data in MySQL.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
I have done something similar to this recently using an MSSQL image field. I'd imagine it to be the same in MySQL except using a long BLOB field rather than an image field. Here is the code I got from php.net:

// storing a file
$datastring = file_get_contents(&quot;img.jpg&quot;);
$data = unpack(&quot;H*hex&quot;, $datastring);
mssql_query(&quot;insert into images (name, data)
values ('img.jpg', 0x&quot;.$data['hex'].&quot;)&quot;);

// retrieving
$result = mssql_query(&quot;select data from images where name = 'img.jpg'&quot;);
$row = mssql_fetch_assoc($result);
header(&quot;Content-type: image/jpeg;&quot;);
echo $row['data'];

If you save the &quot;retrieving&quot; code as its own file, you can then enclose it in the <img> tag. Remember the whole thing is to be enclosed in <?PHP... ?> tags. Do not put any HTML headers before hand as it is an image not an HTML file. You may also want to create a variable with the img_id that can be passed from the HTML code. For instance if your file was named get_image.php and your variable was named $img_id. You'd make your tag like:

<IMG SRC=&quot;get_image.php?$img_id=242&quot;>

Hope this helps.

Devon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top