【问题标题】:How to upload and download files to/from mysql on PHP storing only path?如何在仅存储路径的 PHP 上向/从 mysql 上传和下载文件?
【发布时间】:2013-04-09 13:56:14
【问题描述】:

我通过这种方式将文件的路径存储在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.";

    }

我怎样才能通过它的路径下载这个文件?当然,如果我的上传代码是正确的。

【问题讨论】:

    标签: php mysql file upload download


    【解决方案1】:

    试试这个

     $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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      相关资源
      最近更新 更多