0

我之前正在重命名文件下载。一切正常,除了大小。我无法设置文件大小

header('Content-Length: ');

即使我将其设置为

header('Content-Length: 15444544545');

它不工作。我正在使用 PHP codeigniter 框架,问题出在哪里?

编辑:更多代码:

$file_data = array(
        'originalName' => $post_info['file_info'][0]['original_name'],
        'fakeName' => $post_info['file_info'][0]['file_name'],
        'modificationId' => $post_info['file_info'][0]['modification_article_id'],
        'extension' => end(explode('.', $post_info['file_info'][0]['original_name'])),
        'name' => str_replace(".".end(explode('.', $post_info['file_info'][0]['original_name'])), "", $post_info['file_info'][0]['original_name']),
        'filesize' => filesize($post_info['file_info'][0]['file_name'])
    );

    header('Cache-Control: public');
    header('Content-Description: File Transfer');
    header('Content-Disposition: attachment; filename=' . $file_data['name'] . '.' . $file_data['extension']);
    header('Content-Length: ' . filesize(base_url().$file_data['fakeName']));
    // Read file
    readfile(base_url().$file_data['fakeName']);

    //print_r($file_data);

    echo "<script>window.close();</script>";

编辑:解决方案

有一个服务器问题

4

3 回答 3

0

您尝试使用 download_helper 吗?辛塔克斯:force_download($filename, $data)。同样在您的代码中,您正在通过 URL 读取文件。请改用文件系统路径。从控制器动作:

<?php
public function download()
{
    //Your code here...
    $filePath = realpath(FCPATH.DIRECTORY_SEPARATOR.'uploads/myfile.pdf'); //FakeName????
    force_download($file_data['fakeName'], readfile($filePath)); 
}

如果我的解决方案不起作用,请给我一点帮助,给你其他方式。

注意FCPATH是前端控制器路径,服务器的公共文件夹,例如(/var/www/CodeIgniter)。其他路径常量已在 index.php(前端控制器)上定义。

打印出来$file_data['fakeName']会有用。

如果您的 CodeIgniter 版本没有 download_helper 自己制作...请参阅 CI 文档以获取完整说明。有force_download函数代码:

function force_download($filename = '', $data = '')
{
    if ($filename == '' OR $data == '')
    {
            return FALSE;
    }

    // Try to determine if the filename includes a file extension.
    // We need it in order to set the MIME type
    if (FALSE === strpos($filename, '.'))
    {
            return FALSE;
    }

    // Grab the file extension
    $x = explode('.', $filename);
    $extension = end($x);

    // Load the mime types
    if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
    {
        include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
    }
    elseif (is_file(APPPATH.'config/mimes.php'))
    {
        include(APPPATH.'config/mimes.php');
    }

    // Set a default mime if we can't find it
    if ( ! isset($mimes[$extension]))
    {
        $mime = 'application/octet-stream';
    }
    else
    {
        $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
    }

    // Generate the server headers
    if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)
    {
        header('Content-Type: "'.$mime.'"');
        header('Content-Disposition: attachment; filename="'.$filename.'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header("Content-Transfer-Encoding: binary");
        header('Pragma: public');
        header("Content-Length: ".strlen($data));
    }
    else
    {
        header('Content-Type: "'.$mime.'"');
        header('Content-Disposition: attachment; filename="'.$filename.'"');
        header("Content-Transfer-Encoding: binary");
        header('Expires: 0');
        header('Pragma: no-cache');
        header("Content-Length: ".strlen($data));
    }

    exit($data);
}
于 2013-11-12T16:06:28.000 回答
0

的错误用法base_url()

您的文件存储在哪里?

也许您可以尝试使用常量FCPATH而不是调用函数base_url()

并且您将文件大小存储在$file_data['filesize']

最后,当输出文件内容时,您的 php 脚本中不应该有一行echo "<script>window.close();</script>";

于 2013-11-12T15:43:59.923 回答
0

你可以这样尝试:

$mm_type="application/octet-stream";

header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($fullpath)) );
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary\n");

readfile($fullpath);
于 2013-11-12T14:47:33.577 回答