如何上传图片到MySQL数据库使用PHP /的Flex上传图片、数据库、Flex、MySQL

2023-09-08 11:44:50 作者:秋萝茗瑰缘

我一直在寻找的净几个星期,现在并不能找到解决我的问题。

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

我在Web应用程序中使用的工作的Flex(前端) PHP / MySQL的(后端)

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

推荐答案

首先在您从上传文件的形式,你应该使用以下标签:

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

TINYBLOB , BLOB , MEDIUMBLOB , LONGBLOB

Finaly修改此code为你的数据库,这将做到:)

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'); 

} 

?>