dwaynej84 Posted August 3, 2010 Share Posted August 3, 2010 Hi All, This is my first post on here. I'd really appreciate any help with this, it's driving me mad. I'm trying to create a form to save a image into a mySQL database. I have a website hosted by UK2.net which has a mysql db with a table called gallery(name (varchar 30), size (int), type varchar(30), thePic(mediumBlob)). I have a form with this: <td><p><label>Pic: </label></td><td><input name="userfile" type="file" id="userfile" /></td></p> Which when submitted actions addImage.php. The code for this looks like this: <?php $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $con = mysql_connect("localhost", "userName", "password") or die(mysql_error()); mysql_select_db("dbName", $con) or die(mysql_error()); $query = "INSERT INTO gallery (name, size, type, thePic) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; if (!mysql_query($query,$con)) { die('Error: ' . mysql_error()); } echo "<br>File $fileName uploaded<br>"; ?> I get the following error message: Warning: fopen() [function.fopen]: Filename cannot be empty in /home/hiddenje/public_html/addImage.php on line 13 Does anyone know what this is about? I've been on out friend google and a lot of people seem to be pointing to permissions but I can't seem to apply it to my scenario and just can't get it to work. Im a developer by trade, but this is my first step into the...interesting world of PHP and mySQL. Again, I'd appreciate any help with this. I'd love someone to talk me through exactly what I'm missing or doing wrong. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/209660-php-saving-image-to-mysql-as-blob-issues/ Share on other sites More sharing options...
JonnoTheDev Posted August 3, 2010 Share Posted August 3, 2010 Why would you store the contents of an image file in a database field? Use your script to upload the file to the server, rename it so it is unique and doesn't contain any funny characters, save the filename to the database. When you output to screen all you need is the filename from the database, i.e <img src="/uploads/<?php print $row['filename']; ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/209660-php-saving-image-to-mysql-as-blob-issues/#findComment-1094554 Share on other sites More sharing options...
dwaynej84 Posted August 3, 2010 Author Share Posted August 3, 2010 Why would you store the contents of an image file in a database field? Use your script to upload the file to the server, rename it so it is unique and doesn't contain any funny characters, save the filename to the database. When you output to screen all you need is the filename from the database, i.e <img src="/uploads/<?php print $row['filename']; ?>" /> That sounds perfect. I did look for some script that does that but couldn't find any, can you point me in the direction of a tutorial/help that uses php to upload file to server? Thanks for your time with this. Quote Link to comment https://forums.phpfreaks.com/topic/209660-php-saving-image-to-mysql-as-blob-issues/#findComment-1094556 Share on other sites More sharing options...
JonnoTheDev Posted August 3, 2010 Share Posted August 3, 2010 http://www.tizag.com/phpT/fileupload.php Quote Link to comment https://forums.phpfreaks.com/topic/209660-php-saving-image-to-mysql-as-blob-issues/#findComment-1094559 Share on other sites More sharing options...
dwaynej84 Posted August 3, 2010 Author Share Posted August 3, 2010 http://www.tizag.com/phpT/fileupload.php Your a gent! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/209660-php-saving-image-to-mysql-as-blob-issues/#findComment-1094562 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.