好的,所以我遇到了将文件作为blob插入数据库的问题。我能够将文件上传到我的数据库上的文件夹,但我现在想要将该文件作为blob插入到我的数据库中。我知道这一般不行,但我想做。无论如何我想知道是否有人有任何PHP代码将文件插入blob表?我试过用这个无济于事我可能只是不写SQL语句吗?
<?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 :(得分:2)
我会避免在数据库中存储文件。这就是FileSystems的用途,并且做得更好。博客圈里有数以百万计的帖子解释了为什么不这样做。即使是小型应用程序,我也不会这样做。它只是感觉很脏。数据库中的blob不包含有关文件本身的元数据,因为可以通过多种方式查询FS来获取文件大小,修改时间,权限等信息...我的$ 0.02
答案 1 :(得分:1)
你需要将文件读入一个变量然后插入它。给它文件名只会不做。
$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 :(得分:0)
$ file只包含文件的名称而不是它的内容。
尝试以下方法:
<?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();