在我当前的代码中,我只插入文件的文件名,文件存储在文件夹中。我还想将文件存储在我的mysql数据库中。我怎样才能做到这一点。
我的表:id file_name fcontent (longblob)
include 'db.php';
if(isset($_FILES['image'])){
$errors= array();
$tablename = "files";
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$content =$_FILES['image']['fcontent']; // content in database
$sql="INSERT INTO $tablename(file_name,content)VALUES('" . mysql_real_escape_string($file_name) . "')"; // here i need to insert the content i assume
$file_ext=strtolower(end(explode('.',$_FILES['image']['name']))); //convert to lower
$extensions = array("jpeg","jpg","png","txt","html","php","gif"); // File extension that are
allowed
if(in_array($file_ext,$extensions )=== false){ // check if value exists in array
$errors[]="extension not allowed.";
}
if($file_size > 2097152){ // cant be greater than 2mb
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
mysql_query($sql);
move_uploaded_file($file_tmp,"upload/".$file_name);
echo "Success";
header("location:files.php"); // Send back to main page
}else{
print_r($errors);
}
}
?>