Don S
Don S

Reputation: 134

Question about uploading a small image to a blob field in a MySQL database using Php.

I have a mysql database with a table named games and a blob field named cover. I am going to store a small image in the blob field. I'm putting the image in the field so I can send frequent updates of the database to an iphone app, without having to update the whole app every few days.

I got this code off a blog but there wasn't a lot of explanation with it. Do I also have to have fields named name, size, type and content?

$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: 1

Views: 320

Answers (1)

Calum
Calum

Reputation: 5326

You'll need fields in your database titled 'name', 'size', 'type' and 'content'

Also, consider using PDO for mysql connections http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

Upvotes: 3

Related Questions