上传文件到mySQL数据库。代码帮助

Okay so I am running into issues inserting a file into my database as a blob. I am able to upload the file to a folder on my database but I now want to insert the file into my database as a blob. I know this isnt generally ok to do but I WANT TO DO IT. Anyways I was wondering if anyone had any PHP code to insert a file into a blob table? I've tried using this to no avail maybe I am just not writing the SQL statement right?

<?php 
 $target = "./"; 
 $target = $target . basename( $_FILES['uploadedfile']['name']); 
 $file=($_FILES['uploadedfile']['name']); 
 if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
 echo "The file ".  basename( $_FILES['uploadedfile']['name']).
 " has been uploaded";
 }else{
 echo "There was an error uploading the file, please try again!";
 }
 mysql_connect("localhost","root","");
 mysql_select_db("ivrsupport");
 $sql=mysql_query("insert into CUSTOMER (AUDIO_FILE)values
   ('$file') where PHONE_NUMBER = ('15555215554')");
 $r=mysql_query($sql);
 if(!$r){
 echo "Error in query: ".mysql_error();
 }  
mysql_close();  
?> 

#0

I would avoid storing files in the database. That's what FileSystems are for and are much better at doing so. There are millions of posts out there in the blogosphere explaining why not to. Even for a small application I wouldn't do it. It just feels dirty. A blob in the database holds no metadata about the file itself where as a FS can be queried in many ways to gleem information like file size, modification times, permissions etc... My $0.02

#1

You need to read the file into a variable and then insert it.. giving it the filename only wont do.

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

$sql=mysql_query("insert into CUSTOMER (AUDIO_FILE)values
   ('$content') where PHONE_NUMBER = ('15555215554')");

#2

$file simply contains the name of the file not it's contents.

Try the following:

<?php 
 $target = "./"; 
 $target = $target . basename( $_FILES['uploadedfile']['name']); 
 $file=($_FILES['uploadedfile']['name']); 
 $fileContents = file_get_contents($file);
 if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
 echo "The file ".  basename( $_FILES['uploadedfile']['name']).
 " has been uploaded";
 }else{
 echo "There was an error uploading the file, please try again!";
 }
 mysql_connect("localhost","root","");
 mysql_select_db("ivrsupport");
 $sql=mysql_query("insert into CUSTOMER (AUDIO_FILE)values
   ('$fileContents') where PHONE_NUMBER = ('15555215554')");
 $r=mysql_query($sql);
 if(!$r){
 echo "Error in query: ".mysql_error();
 }  
mysql_close();  

推荐文章

限制文本框仅接受10位数字

限制文本框仅接受10位数字

推荐文章

条件跳转或移动取决于未初始化的值

条件跳转或移动取决于未初始化的值

推荐文章

检测C中读取的文件#

检测C中读取的文件#

推荐文章

Perl:为什么要把“Perl”编译器和“perlcc”前端分开?

Perl:为什么要把“Perl”编译器和“perlcc”前端分开?

推荐文章

1/252=0在c#?

1/252=0在c#?

推荐文章

如何在链接页上自定义UIWebView点击保持

如何在链接页上自定义UIWebView点击保持

推荐文章

有可能有一个完全可移植的PHP开发环境吗?

有可能有一个完全可移植的PHP开发环境吗?

推荐文章

在PHP中,使用fopen()将文件指针资源分配给变量后,如何从变量中获取文件名?

在PHP中,使用fopen()将文件指针资源分配给变量后,如何从变量中获取文件名?

推荐文章

反向ip,在ip地址上查找域名

反向ip,在ip地址上查找域名

推荐文章

从具有最新“ID”的行中获取所有字段

从具有最新“ID”的行中获取所有字段

推荐文章

mysql仅当另一行的字段等于

mysql仅当另一行的字段等于

推荐文章

在托管服务器上的美国web应用程序中接收SMS消息

在托管服务器上的美国web应用程序中接收SMS消息

推荐文章

如何为Rails做出贡献?

如何为Rails做出贡献?

推荐文章

jqGrid tableToGrid函数-删除多选

jqGrid tableToGrid函数-删除多选

推荐文章

在Eclipse中创建可执行JAR文件

在Eclipse中创建可执行JAR文件

推荐文章

我需要帮助来编写javascript正则表达式

我需要帮助来编写javascript正则表达式