PHP在MySql中存取文件

先建张表

create table if not exists binary_files(
id int(4) unsigned not null auto_increment primary key,
file_name varchar(250) not null,
file_data longblob not null);

PHP中测试

getMessage());
            }
                        //向表中插入文件数据
            $pdostmt = $pdo->prepare('insert into binary_files (file_name,file_data) values(?,?)');
            $pdostmt->execute(array($_FILES['myfile']['name'],$read_buffer));
            $read_buffer = '';
            
            @fclose($read_file_hanle);
            @unlink($_FILES['myfile']['tmp_name']);
            
            if( !file_exists('savefiles')){
                mkdir('savefiles');
            }
            
            //从数据库中读取文件,在输出 看看对不对
            $pdostmt = $pdo->prepare('select file_data from binary_files where file_name=?');
            $pdostmt->execute(array($_FILES['myfile']['name']));
            $fetch_result = $pdostmt->fetch(PDO::FETCH_ASSOC);
            $put_buffer = $fetch_result['file_data'];
            echo $put_buffer;
            file_put_contents('./savefiles/'.$_FILES['myfile']['name'],$put_buffer);
        }   
    }
}

你可能感兴趣的:(PHP在MySql中存取文件)