PDA

View Full Version : Problem uploading images to MySQL


MarkW
05-09-2001, 07:21 PM
Having problems with the image not loading into the db table.[nbsp][nbsp]The filename, size and type get successfully stored in the table.[nbsp][nbsp]It's just the image/binary which doesn't.[nbsp][nbsp]I can get this to work on my dev machine (NT) and on another host (Linux), but it refuses to cooperate on FQ.[nbsp][nbsp]I'm sure it's probably just a setting I need to make - just don't know what setting!

Here's my table:
----------------
CREATE TABLE bin_tbl (
[nbsp][nbsp] id int(4) DEFAULT '0' NOT NULL auto_increment,
[nbsp][nbsp] description varchar(50),
[nbsp][nbsp] bin_data longblob,
[nbsp][nbsp] filename varchar(50),
[nbsp][nbsp] filesize varchar(50),
[nbsp][nbsp] filetype varchar(50),
[nbsp][nbsp] PRIMARY KEY (id)
);

storeimg.php script:
--------------------
<HTML>
<BODY>
<?php
if ($submit) {
[nbsp][nbsp]@mysql_connect (&quot;mysql.xxx.com&quot;, &quot;yyy&quot;, &quot;zzz&quot;);
[nbsp][nbsp]mysql_select_db (&quot;mydb&quot;);

[nbsp][nbsp]$data = addslashes(fread(fopen($form_data,[nbsp][nbsp]&quot;r&quot;), filesize($form_data)));

[nbsp][nbsp]$result=MYSQL_QUERY( &quot;INSERT INTO bin_tbl (description,bin_data,filename,filesize,filetype) VALUES ('$form_description','$data','$form_data_name','$form_data_size', '$form_data_type')&quot;);

[nbsp][nbsp]$id= mysql_insert_id();
[nbsp][nbsp]print[nbsp][nbsp]&quot;

This file has the following Database ID: $id&quot;;

[nbsp][nbsp]MYSQL_CLOSE();

[nbsp][nbsp]print[nbsp][nbsp]&quot;<img src='showimg.php?id=$id'> &quot;;
} else {
?>
[nbsp][nbsp]<form method=&quot;post&quot; action=&quot; <?php echo $PHP_SELF; ?>&quot; enctype=&quot;multipart/form-data&quot;>
[nbsp][nbsp]File Description:

[nbsp][nbsp]<input type=&quot;text&quot; name=&quot;form_description&quot;[nbsp][nbsp]size=&quot;40&quot;>
[nbsp][nbsp]<input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;1000000&quot;>
[nbsp][nbsp]
File to upload/store in database:

[nbsp][nbsp]<input type=&quot;file&quot; name=&quot;form_data&quot;[nbsp][nbsp]size=&quot;40&quot;>
[nbsp][nbsp]

<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;submit&quot;>
[nbsp][nbsp]</form>
<?php
}
?>
</BODY>
</HTML>

showimg.php script:
-------------------
<?php
if($id) {
[nbsp][nbsp]@mysql_connect (&quot;mysql.xxx.com&quot;, &quot;yyy&quot;, &quot;zzz&quot;);
[nbsp][nbsp]mysql_select_db (&quot;mydb&quot;);
[nbsp][nbsp]$query =[nbsp][nbsp]&quot;select bin_data,filetype from bin_tbl where id=$id&quot;;
[nbsp][nbsp]$result = @MYSQL_QUERY($query);
[nbsp][nbsp]$data = @MYSQL_RESULT($result,0, &quot;bin_data&quot;);
[nbsp][nbsp]$type = @MYSQL_RESULT($result,0, &quot;filetype&quot;);

[nbsp][nbsp]Header([nbsp][nbsp]&quot;Content-type: $type&quot;);
[nbsp][nbsp]echo $data;
};
?>

Any suggestions would be very much appreciated.

Thanks
Mark

Have just noticed this post:
Strange behaviour of /tmp
http://www.aota.net/ubb/Forum15/HTML/000806-1.html
Could it be related to my problem?

[This message has been edited by Mark Wolfgramme (edited 05-09-01@6:34 pm)]

MarkW
05-10-2001, 09:37 AM
Still no joy on this problem.

Have echoed out the $data var as follows:
[nbsp][nbsp]$data = addslashes(fread(fopen($form_data,[nbsp][nbsp]&quot;r&quot;), filesize($form_data)));
[nbsp][nbsp]echo &quot;ANYTHING?:&quot;.$data.&quot;<&quot;;

But get nothing (except 'ANYTHING?:<').[nbsp][nbsp]On my other host the full $HTTP_POST_FILES array is displayed.[nbsp][nbsp]

I beginning to think there's something wrong with reading the file.

Have been looking around other forums where someone has picked up a problem related to RPMs from Redhat 7.0 and the PHP pl1 version.[nbsp][nbsp]They recompiled PHP by hand to fix their problem.[nbsp][nbsp]

Don't know if this is at all relevant at FQ.[nbsp][nbsp]Anyone at TechSupport care to comment?

Regards
Mark
[This message has been edited by Mark Wolfgramme (edited 05-10-01@08:39 am)]

Rich
05-10-2001, 02:20 PM
Haven't worked with form file uploads. But, perhaps some error checking/dumping around addslashes, fread, and fopen is appropriate for debugging this?

Rich

Neo
05-10-2001, 07:46 PM
Hello I have tested your code on my account at FutureQuest, it works fine, you can see it at work here http://www.aleph.org.mx/imgup/ could you post a link to your site so that we can see yours?

BTW very neat script...

Perhaps some parameters on the database connection are wrong?

Host=yourdomain.com User=xyouraccount Password=youPassword for mysql access and finally the db= for the select statement?

Hope this helps it proves your code works and that FutureQuest servers support it just fine... :)

Did you uploaded your scripts from a Win machine? if so did you upload them as ASCII?

------------------
FutureQuest
Staff Member
[This message has been edited by Neo (edited 05-10-01@6:48 pm)]

MarkW
05-10-2001, 09:18 PM
Finally got it working![nbsp][nbsp]

To be honest, science went out the window so I'm not 100% sure what the problem was.[nbsp][nbsp]For some reason I wasn't able to open/read the uploaded file until I moved it to my own 777 directory.

Here's how it worked for me (skinny version):

if (is_uploaded_file($form_data)) {
[nbsp][nbsp]$upload_dir=&quot;/big/dom/xmyplayground/www/cargobay&quot;; //777 permissions
[nbsp][nbsp]if (move_uploaded_file($form_data, &quot;$upload_dir/$form_data_name&quot;)) {
[nbsp][nbsp][nbsp][nbsp]chmod(&quot;$upload_dir/$form_data_name&quot;, 0444); /*convert to r--r--r-- */
[nbsp][nbsp][nbsp][nbsp]$filename = $upload_dir.&quot;/&quot;.$form_data_name;
[nbsp][nbsp][nbsp][nbsp]$data = addslashes(fread (fopen ($filename, &quot;r&quot;), filesize ($filename)));
[nbsp][nbsp][nbsp][nbsp]unlink($filename);[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]
[nbsp][nbsp][nbsp][nbsp]$result=MYSQL_QUERY( &quot;INSERT INTO bin_tbl (description,bin_data,filename,filesize,filetype) VALUES ('$form_description','$data','$form_data_name','$form_data_size', '$form_data_type')&quot;);
[nbsp][nbsp]}
}

Regards
Mark


[This message has been edited by Mark Wolfgramme (edited 05-10-01@8:19 pm)]

Terra
05-10-2001, 10:29 PM
$data = addslashes(fread (fopen ($filename, &quot;r&quot;), filesize ($filename))); This line concerns me due to large amounts of potential memory consumption...

What are the image file sizes:
1) Max (I'd like to see this <200Kb)
2) Avg

Make sure your upload routines have safeguards built in to deny large file sizes...

--
Terra
sysAdmin
FutureQuest

MarkW
05-11-2001, 04:37 AM
Terra

I understand your concerns about uncontrolled uploads - in terms of both volume and size of uploads.

The uploads, in this case, are performed by the site owners/operators.[nbsp][nbsp]

Volumes are expected to stay around 200-300 uploaded files.
Sizes of the current files average about 20kB.

I will fix the max file size to 100kB.

Mark
[This message has been edited by Mark Wolfgramme (edited 05-11-01@03:39 am)]

Terra
05-11-2001, 07:14 AM
Thank you much for your concerns in controlling a users input limits...

This is how the server resource honor system works...[nbsp][nbsp]This way I can have the limits a little bit higher for those that occasionally need 'burst' capability or just a tad bit more memory than normal...[nbsp][nbsp]My only other option is to tighten down across board which indiscriminately affects everyone...

--
Terra
--Trying to find the right balance for which is mutually beneficial to all--
FutureQuest

MarkW
05-11-2001, 08:26 AM
I think that's a very fair system.

You're up early![nbsp][nbsp]It's fine for me, I'm in the UK.

Regards
Mark

daclown
05-11-2001, 11:43 AM
Just a suggestion!![nbsp][nbsp]=)

I have built many upload scripts and would recommend against storing the binary in your database.[nbsp][nbsp]What i do to both save time, and space is to key the image file and upload it to a specific directory.[nbsp][nbsp]Then just store the image key name in the database![nbsp][nbsp]That way when I want to pull an image from the db all i have to do is grab the name and do an <img src=&quot;dir/<?=$image?>&quot;>.[nbsp][nbsp]This is a MUCH more efficient way of dealing with pictures... I found one of the best ways to store photos without anyone else using the same filename is to either use an &quot;id_&quot; with the $pic or to save it as &quot;[nbsp][nbsp]$a = time();[nbsp][nbsp][nbsp][nbsp]
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]$pic = &quot;$a&quot;.&quot;.jpg&quot;;

I hope some of this helps![nbsp][nbsp]Good luck =)

trafficg
05-12-2001, 12:59 PM
This is a slightly cut down/modified version of my banner upload script. The script first moves the file to a tmp directory, checks the file and then copies it to a permanent upload directory. The script also does a lot more error checking than the previous scripts.


<? include ('include/connection.inc.php'); ?>
<?

OpenDbConnection($con,$dbname);

$id = isset($id) ? $id : 0;
$username = isset($username) ? $username : &quot;&quot;;
$userfile = isset($userfile) ? $userfile : &quot;&quot;;

if($userfile == &quot;none&quot;)
{
// No file entered on form
Header(&quot;Location: banner_error.php&quot;);
exit;
}

if($userfile == &quot;&quot;)
{
// This is here as a second trap just incase
Header(&quot;Location: banner_error.php&quot;);
exit;
}

$size = filesize($userfile);

if ($size == 0)
{
// Make sure the file isn't empty
Header(&quot;Location: banner_error.php&quot;);
exit;
}

// max size check, same as MAXFILESIZE on the form
if($size > 20 * 1024) {
Header(&quot;Location: banner_error.php&quot;);
exit;
}

$upload_dir = &quot;/big/dom/xdom/www/tmp&quot;; // 777 directory

if(move_uploaded_file($userfile,&quot;$upload_dir/$userfile_name&quot;))
{
chmod(&quot;$upload_dir/$userfile_name&quot;, 0444); // rrr permission

// get banner size and type

$tags = GetImageSize(&quot;$upload_dir/$userfile_name&quot;);
$width = $tags[0];
$height = $tags[1];
$format = &quot;unknown&quot;;

switch ($tags[2])
{
case 1:
$format = &quot;gif&quot;;
break;
case 2:
$format = &quot;jpg&quot;;
break;
case 3:
$format = &quot;png&quot;;
}

if($format == &quot;unknown&quot;)
{
@unlink(&quot;$upload_dir/$userfile_name&quot;);
Header(&quot;Location: banner_error.php&quot;);
exit;
}

if($width != 468)
{
@unlink(&quot;$upload_dir/$userfile_name&quot;);
Header(&quot;Location: banner_error.php&quot;);
exit;
}

if($height != 60)
{
@unlink(&quot;$upload_dir/$userfile_name&quot;);
Header(&quot;Location: banner_error.php&quot;);
exit;
}

mt_srand ((double) microtime() * 1000000);
$path = &quot;upload/&quot;.$username.mt_rand(1,10000).&quot;.&quot;.$format;

if (!copy(&quot;$upload_dir/$userfile_name&quot;, $path))
{
@unlink(&quot;$upload_dir/$userfile_name&quot;);
Header(&quot;Location: banner_error.php&quot;);
exit;
}

// Do your database updates or inserts here

$sql = &quot;UPDATE $dbname.images SET pic='&quot;.$path.&quot;' WHERE id = $id&quot;;
$Result = mysql_query($sql, $con);

unlink(&quot;$upload_dir/$userfile_name&quot;);

Header(&quot;Location: show_banner.php&quot;);
}
else
{
Header(&quot;Location: banner_error.php&quot;);
}
?>


Notes:

Do not assume that the uploaded file is an image, on my site people have tried uploading the following file types, (doc, htm, txt, swf, ini, ...) even though it clearly says Gif, Jpg and Png are the only supported file types.

Certain browsers seem to have a problem with underscore characters, it's a bug in the browser software, so if the upload fails this could be the reason.
eg.. c:/temp/my_banner.gif could fail and c:/temp/mybanner.gif would work.

Pete Kelly
Free Traffic and Website Promotion!
www.TrafficG.com (http://www.TrafficG.com)