readfile()在尝试强制下载时返回空文件

时间:2013-11-11 13:47:46

标签: php readfile dwg

我正在尝试使用以下代码强制下载.dwg个文件:

$filename = $_GET['f'];
$upload_path = wp_upload_dir();
$src = $upload_path['basedir'] . '/' . $filename;

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header("Content-disposition: attachment; filename=\"{$filename}\"");
header("Content-Type: application/octet-stream");
header('Content-Transfer-Encoding: binary');
readfile($src);
die();

文件的路径是正确的,但它只返回 6字节的文件,而且无法打开。它应该 754字节。谁可以帮助我在这里?

//Example file
$src = "/var/www/wp/wp-content/uploads/Glasparti-modell-Silence.dwg";

5 个答案:

答案 0 :(得分:1)

尝试添加Content-length标题:

$size = filesize($src);
header("Content-length: $size");

答案 1 :(得分:0)

您能否使用.dwg文件内容类型:

  • 应用/ ACAD
  • 应用程序/ x-ACAD
  • 应用/ autocad_dwg
  • image / x-dwg,application / dwg
  • 应用程序/ x-DWG
  • 应用程序/ x-的AutoCAD
  • 图像/ vnd.dwg
  • 绘图/ DWG

header("Content-type: 'application/pdf'");
header("Content-Type: application/force-download");

你能试试吗,

<?php
    $file = '';// file path here 

        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;
        }else{
                  echo "File not found";
          }
?>

答案 2 :(得分:0)

尝试:

    $name = $_GET['f'];
    $url = './files/'.$name;
    header("Content-type: application/octet-stream");
    header("Content-disposition: attachment;filename=$name");
    readfile($url);

答案 3 :(得分:0)

我在.htaccess中通过以下内容解决了这个问题:

<FilesMatch "\.(dwg)$" >
    ForceType application/octet-stream
    Header add Content-Disposition "attachment"
</FilesMatch>

答案 4 :(得分:0)

尝试一下!

/**
 * file download in MVC
 * 
 * @author VECTOR Ann <ann@vector.cool>
 * @since 1.0.1
 * 
 * @param string $file          提供下載的檔案絕對路徑
 * @param string $download_name 下載的檔名
 * @return void
 */
function v_file_download($file,$download_name=''):void
{   
    if (is_file($file)) {
        if($download_name=='')$download_name = date('Y_m_d',time()).".zip";
        header_remove(); 
        ob_end_clean();
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($download_name));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        ob_end_flush();
        exit;
    }
}