Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在仅存储路径的PHP上向mysql上传和下载文件?_Php_Mysql_File_Upload_Download - Fatal编程技术网

如何在仅存储路径的PHP上向mysql上传和下载文件?

如何在仅存储路径的PHP上向mysql上传和下载文件?,php,mysql,file,upload,download,Php,Mysql,File,Upload,Download,我通过在mysql中存储文件路径来上传文件: if(isset($_FILES['filefield'])) { $file = $_FILES['filefield']; $file_name = $file['name']; $file_size = $file['size']; $file_type = $file['type']; $file_tmpname = $file['tmp_name'];

我通过在mysql中存储文件路径来上传文件:

if(isset($_FILES['filefield']))
    {
        $file = $_FILES['filefield'];
        $file_name = $file['name'];
        $file_size = $file['size'];
        $file_type = $file['type'];
        $file_tmpname = $file['tmp_name'];  
        $upload_dir = "C:/xampp/htdocs/intranet/www/public/uploads/";
        $ext_str = "gif,jpg,jpeg,mp3,tiff,bmp,doc,docx,ppt,pptx,txt,pdf";
        $allowed_extensions=explode(',',$ext_str);
        $max_file_size = 10485760;
        $ext = substr($file['name'], strrpos($file['name'], '.') + 1);
        if (!in_array($ext, $allowed_extensions))
            echo "only".$ext_str." files allowed to upload";
        if($file['size']>=$max_file_size)
            echo "only the file less than ".$max_file_size."mb  allowed to upload";
        $path = $upload_dir.$file_name;
        if(move_uploaded_file($file_tmpname, $upload_dir.$file_name))
            ORM::factory('files')->uploadFile($teacher_id, $file_name, $file_size, $file_type, $path);
        else
            echo "The file cant moved to target directory.";

    }
然后如何按路径下载此文件?当然,如果我的上传代码是正确的

试试这个

 $file = 'C:/xampp/htdocs/intranet/www/public/uploads/'.$file_name;

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}