如何将图像插入mysql数据库?

时间:2011-05-25 11:59:20

标签: php mysql oop

我试图将上传的图像插入到mysql数据库中,但是我收到了这个错误消息。请帮忙。这是我得到的错误

Array
(
    [name] => Zonsondergang.jpg
    [type] => image/jpeg
    [tmp_name] => /tmp/phpU9qSru
    [error] => 0
    [size] => 71189
)

data / Zonsondergang.jpg测试文件成功加载

  

警告:file_get_contents(/ tmp / phpU9qSru)[function.file-get-contents]:无法打开流:/home/meer/domains/skyup.nl/public_html/upload_Class/upload中没有此类文件或目录。第68行的PHP   错误!无法插入file1

这是我上传图片和将图片插入数据库的代码

include('../inc/con_inc.php');

// Defining Class
$uploaded = new upload;

// Set Max Size
$uploaded->set_max_size(350000);

// Set Directory
$uploaded->set_directory("data");

// Set Temp Name 
$uploaded->set_tmp_name($_FILES['file']['tmp_name']);

// Set file size, 
$_FILES['file']['size'] is automaticly get the size
$uploaded->set_file_size($_FILES['file']['size']);


// Set File Type,
 $_FILES['file']['type'] 
$uploaded->set_file_type($_FILES['file']['type']);

// Set File Name, 
$uploaded->set_file_name($_FILES['file']['name']);


// Start Copy Process
$uploaded->start_copy();


$name=($_FILES['file']['name']);
$data=(file_get_contents($_FILES['file']['tmp_name']));


$query="INSERT INTO download(name,data) VALUES('$name','$data')";
if ($result){
 echo 'Success! Your file was successfully added!';
 }else{
 echo 'Error! Failed to insert the file' ;
 $result=mysql_query($query) or die ('query fout');

 }

这是表的结构:

create table download (
    id int primary key not null 
auto_increment,
    name varchar(60),
    data longblob)

1 个答案:

答案 0 :(得分:0)

$data=(file_get_contents($_FILES['file']['tmp_name']));

由于您刚刚使用upload课程上传了文件,因此无法执行此操作 你应该有一个方法或变量,你可以获得实际上传的文件,例如:$uploaded->get_file()
你应该从那里获取文件内容(上传到'data'文件夹中的文件)