Jump to content

PHP - Saving Image to MySQL as Blob Issues


dwaynej84

Recommended Posts

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.

Link to comment
Share on other sites

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']; ?>" />

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.