Jump to content

Loading Images To Database?


madmoose

Recommended Posts

Is it possible to load images into a database from a form and then reflect it back on a view page?

 

I have tried several script configurations taken from the php manual etc., but the all send this error message (whereas other people seem to have no trouble):

 

Parse error: parse error, unexpected T_STRING in /home/myaccount/public_html/test/sendimage.php on line 6

 

Is it possible to store images in a database at TCH?

 

Thanks

MM

Link to comment
Share on other sites

Yes, it is possible. Do keep in mind that it may kill your database, if the images are big and are going to be fetched many times.

 

As for how to do it, I could explain you how to do it since I've done it several times but instead, I'll just tell you "learn to use google" ;)

http://www.google.com/search?q=storing+ima...start=0&start=0

Link to comment
Share on other sites

Hi,

 

Just curious about a couple of things. I'm a beginner at server-side stuff!

 

Do keep in mind that it may kill your database, if the images are big

 

What does "kill your database" mean?? And how big is big for what you are talking about here...Many megabytes worth or just several KBs worth?

 

Tracy

Link to comment
Share on other sites

but instead, I'll just tell you "learn to use google" ;)

Actually, I have several great scripts including those found at the top of your google search. The problem is that each time I attempt any one of many scripts, I receive the same unexpected T_STRING error. I cannot seem to get past it.

 

If you have thoughts on this issue, please let me know.

 

MM

Link to comment
Share on other sites

madmoose, it seems you are not a google illiterate after all. My apologies ;)

 

Could you please be a little more specific about your problem? Are you setting the right column types in MySQL? Are you sending the MIME type in the headers when retrieving the image from the database and sending it to the browser? Actually, are you having problems getting the images from the database or storing them there?

 

 

As for natimage's questions, by "kill your database" I meant that it could hog the server down, making it slow or even unavailable. As for the size, it's actually more a matter of how much will the image be requested. For instance, you can have one single big image with one megabyteor you can also have lots of small images with just a few Kb each and only one person viewing each of them but if you have thousands of visitors to your site...

 

Basically, one big image loaded once isn't any better/worse than lots of smaller images loaded several times.

 

The key is the traffic the images are going to generate on the server. You have to think in terms of how much data will the MySQL server have to transfer, either with big images or small ones.

 

 

Raul

Link to comment
Share on other sites

I'm not sure if this helps... but I've gotten that same error several times during the transfer of one of my sites that is database driven.

 

I wish I could tell you exactly what the problem is... but I'll try to point you in the right direction.

 

I had to recode MANY pages of my site to account for register_globals == off. If you look in the php(info) file, you'll see what I'm talking about re. globals.

 

Anyhow, my old scripts would assign variables that were passed by forms without any special code. In other words, if I had a field in a form that was 'email' then the script receiving the data would say $var=$email.

 

I've had to go through my pages and put in code that looks like

$var = $_POST['email'];

 

I don't know if that's the issue, but some older scripts seem to be written based on the assumption that register_globals is on. TCH has it off.

 

I'd hate to send you on a wild goose chase but maybe this will help.

Link to comment
Share on other sites

Could you please post line 6 as well as 5 in sendimage.php.

I am not having trouble viewing the image once posted to the db (i have tested this by hand loading the image from phpmyadmin). My trouble is with the sendimage script. They all are experiencing error at the very start of the script not even getting to the variables. Here is just one of several I have tried:

 

1. Form:

 

<html>

<body>

<FORM ENCTYPE="multipart/form-data" ACTION="upload.php" METHOD=POST>

<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="300000">

Send this file: <INPUT NAME="userfile" TYPE="file">

<INPUT TYPE="submit" VALUE="Upload file">

</FORM>

</body>

</html>

 

2. Sendimage: Here is the scipt that receives the unexpacted T_STRING error.

 

<?

if (is_uploaded_file($userfile)) {

   copy($userfile, $HTTP_POST_FILES['userfile']['name']);

} else {

   echo "error. File: '$userfile'.";

}

$filename=$HTTP_POST_FILES['userfile']['name'];

$fd = fopen ($filename, "r");

$contents = fread ($fd, filesize($filename));

fclose($fd);

$escaped_contents=mysql_escape_string($contents);

// Connecting, selecting database

$link = mysql_connect("dbhost", "dbuser", "dbpassword")

   or die("Could not connect");

mysql_select_db("databasename")

   or die("Could not select database");

// Performing SQL query; assuming that the "image" is a BLOB field

$query = "UPDATE tablename SET image='$escaped_contents' WHERE id=1";

$result = mysql_query($query)

   or die("Query failed");

// Closing connection

mysql_close($link);

?>

 

3. To view the image, I am using this script successfully:

 

<?

// Connecting, selecting database

$link = mysql_connect("dbhost", "dbuser", "dbpassword")

   or die("Could not connect");

#print "Connected successfully";

mysql_select_db("databasename")

   or die("Could not select database");

header(" Content-Type: image/gif");

header(" Content-Disposition: inline");

$sql = "SELECT picture FROM tablename WHERE id=1";

$result = mysql_query($sql);

$row = mysql_fetch_row($result);

$image = $row[0];

echo $image;

?>

 

 

Thanks for the attention.

MM

Link to comment
Share on other sites

Actually, are you having problems getting the images from the database or storing them there?

I can get them with no trouble. My problem is sending.

 

Each script I have tried generates a T_STRING error right at the start of the script. I have hand loaded images and reflected them with no trouble, just can't seem to find a script to load them.

 

I have only thus far been able to use a move_file script that moves the image to a perm server directory. This is fine but only valuable to me if I could find a way to rename the file with a specific name (ie. img01, img02...) as it move through to the directory. No ideas on this to date.

 

This move script is the only one I have been able to use without receiving the STRING error:

 

<?php

$uploaddir = 'images/';

print "<pre>";

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $_FILES['userfile']['name'])) {

print "File is valid, and was successfully uploaded.";

} else {

print "ERROR: File transfer was NOT successful. Possible invalid file.";

}

?>

 

Thanks Raul,

MM

Link to comment
Share on other sites

but some older scripts seem to be written based on the assumption that register_globals is on.  TCH has it off.

which server are you on? register globals is on on server 11

I swear that must have been changed recently... but maybe I was seeing things.

 

Dunno. Ignore my previous suggestion re globals

Link to comment
Share on other sites

How about this...

Does anyone have a good (complete) script that will allow users to load image files from a html form to an upload directory AND rename the file to a very specific name as it moves to the end directory?

 

If I use a move function to relocate the image to a directory, I need to rename the file to a very specific name as it passes through. any ideas?

 

MM

Link to comment
Share on other sites

not sure exactly how to do it, but you could have the script, before it stores the file, get a list of all the files in the directory, use a reg exp to cut off all but the end of the newest file name, increment the number by 1 and stick the filename back together, then proceed to store the file.

 

For Example:

if you wanted them to be named

  • image001.jpg
  • image002.jpg
  • image003.jpg

and so on, have the script strip off the image and the .jpg from the filename, increment the number by 1 and then stick the filename back together.

 

so if image0010.jpg was the newest file in the directory, take image0010.jpg, take off the image and the .jpg so you're left with only 0010 then increment 0010 by 1 so you get 0011, then stick it back together as image0011.jpg, give image0011.jpg as the filename, and proceed to store the file.

 

I'll sit down later and see if I can bring this theory to life, but no promises. I'm still trying to figure out this regular expression stuff.

Link to comment
Share on other sites

Join the conversation

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

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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...