2

我正在尝试.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";
4

5 回答 5

1

尝试添加Content-length标题:

$size = filesize($src);
header("Content-length: $size");
于 2013-11-11T13:51:30.360 回答
0

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

  • 应用程序/学术
  • 应用程序/x-acad
  • 应用程序/autocad_dwg
  • 图像/x-dwg,应用程序/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";
          }
?>
于 2013-11-11T13:54:40.307 回答
0

尝试:

    $name = $_GET['f'];
    $url = './files/'.$name;
    header("Content-type: application/octet-stream");
    header("Content-disposition: attachment;filename=$name");
    readfile($url);
于 2013-11-11T14:04:46.873 回答
0

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

<FilesMatch "\.(dwg)$" >
    ForceType application/octet-stream
    Header add Content-Disposition "attachment"
</FilesMatch>
于 2013-11-26T11:58:02.997 回答
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;
    }
}
于 2019-02-21T18:52:39.390 回答