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

问题描述 投票:1回答:5

我正在尝试使用以下代码强制下载.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";
php readfile dwg
5个回答
1
投票

尝试添加Content-length标头:

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

0
投票

试试:

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

0
投票

我在.htaccess中解决了以下问题:

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

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";
          }
?>

0
投票

试试吧 !!

/**
 * file download in MVC
 * 
 * @author VECTOR Ann <[email protected]>
 * @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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.