Don S
Don S

Reputation: 134

Trying to insert image into blob field

I'm trying to insert a small image into a mysql database via a web form with php. But it's not working. What's wrong?

Form:

<form action="http://www.yeahthatrocks.com/update2.php" method="post" enctype="multipart/form-data">
Game Name:  <input name="game_name" type="text" size="25" maxlength="255" /><br></br>
Release Date:  <input name="release_date" type="text" size="25" /><p></p>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
Cover Image: <input type="file" name="cover" id="cover"><br><br>
<input name="upload" type="submit" id="upload" value="upload" />
</form>

Insert query for image:

$fileName = $_FILES['cover']['name'];
 $tmpName = $_FILES['cover']['tmp_name'];
 $fileSize = $_FILES['cover']['size'];
 $fileType = $_FILES['cover']['type'];

 $fp = fopen($tmpName, 'r');
 $content = fread($fp, filesize($tmpName));
 $content = addslashes($content);
 fclose($fp);

 if(!get_magic_quotes_gpc())
 {
$fileName = addslashes($fileName);
 }

$query = "INSERT INTO games set name='".$fileName."', size='".$fileSize."', type='".$fileType."', content='".$content."'";
 mysql_query($query) ;

Upvotes: 0

Views: 3861

Answers (2)

aib
aib

Reputation: 46921

Most probably addslashes(). Use a real escape function, or better yet, parameterized queries.

Upvotes: 1

Devon Hubner
Devon Hubner

Reputation: 74

Here's a simple example that has always worked for me:

# create table blobs( id int(11) NOT NULL auto_increment, myblob longblob, PRIMARY KEY (id));


$host     = "localhost";
$username = "blobtest";
$password = "blobtest";
$database = "blobtest";

$link_id = @mysql_connect($host, $username, $password);
@mysql_select_db($database);

$blob = file_get_contents('stripe.png');
$bdata = addslashes($blob);

$qry = "INSERT INTO blobs VALUES(NULL, '$bdata')";
mysql_query($qry);

Upvotes: 0

Related Questions