简体   繁体   中英

How to Upload images to mysql database Using PHP/Flex

I have been searching the net for some weeks now and can't find a solution to my issue.

I'm working on a Web application using Flex(front-end) and PHP/MySql(Back-end) .

Now I want to enable users to upload images for their profile Pictures... I was Uploading them to a Directory on the server. Now my Boss "feels" that they should be stored directly to the Database not only store the link of the Image but the whole content.

Please Help guys....I'm stuck!!!

Generally, it's best to store images on the filesystem (for speed and size). For this, you could store the images in a location such as "images/profiles/{userID}.jpg". If you did want to store the image in the database, just set the column type to blob and insert the image contents in there. A good article that details storing images in a mysql database is described here .

First of all in the form which you're uploading the file from, you should use the following the tag:

enctype="multipart/form-data" 

also you need to specify that field as one of the following:

TINYBLOB , BLOB , MEDIUMBLOB , LONGBLOB

Finaly modify this code for your db and that will do :)

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
   $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);
}

$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed'); 

} 

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM