0

当我将文件推送到标题以下载它时,我第一次遇到的地方。

现在它工作得很好。但是,只要我的一个文件的名称中有“(”或“)”,推送就不起作用。

// required for IE
        if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }

        // Build the headers to push out the file properly.
        header('Pragma: public');     // required
        header('Expires: 0');         // no cache
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT');
        header('Cache-Control: private',false);
        header('Content-Type: application/octet-stream');  // Add the mime type from Code igniter.
        header('Content-Disposition: attachment; filename="'.basename($name).'"');  // Add the file name
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: '.filesize($path)); // provide file size
        header('Connection: close');
        readfile($path); // push it out
        exit();

它会简单地执行 exit() 并给出一个黑页。现在我认为这是一个我必须逃避的角色。我试图逃避它:

str_replace('(', '\(', $name);

这似乎不起作用。现在我试图至少摆脱“(”,看看我的替换函数是否真的对“(”字符做出反应。所以我想简单地从字符串中删除。

$newstring = str_replace('(', '', $name);
echo $newstring;

现在奇怪的是,它似乎什么也没做。更奇怪的是当我这样做时:

$name = 'test(ol';
$newstring = str_replace('(', '', $name);
echo $newstring;

它确实有效!

这是我从以下位置获取我的 $name 的地方:

$name = end($this->uri->segments);

所以看起来我的 Uri 段是一个对象或其他东西,至少它不像一个真正的字符串?

我希望我足够清楚。在此先感谢,Nkmol。

示例文本名称:

测试(2)

功能

function download()
{
    $this->load->helper('download');
    $path = ''; //default waarde

    //zet de uri segments in een array
    $aPath = $this->uri->uri_to_assoc(3);

    //zet de segment array om in een nieuwe path, zo word de current directory gesaved in een variable
    //de laast segment hoeft geen "/" achter, laaste segment van de volledige url
    $i = 0;
    $len = count($aPath);

    foreach($aPath as $key => $value) :
        if ($i == $len - 1)
        {
            $path .= $key .  DIRECTORY_SEPARATOR . $value;
        }
        else
        {
            $path .= $key .  DIRECTORY_SEPARATOR . $value .  DIRECTORY_SEPARATOR;
        }
        $i++;
    endforeach;

    $pathEdit = substr($path, -1);
    if($pathEdit == '/' || $pathEdit == '\\')
    {
        $path = substr_replace($path,"", -1);
    }


    //$data = file_get_contents($path); // Read the file's contents
    $name = end($this->uri->segments); //haal de file naam op(laaste segment in de uri)
    //echo $blub;
    $this -> push_file($path, $name);

    //force_download($name, $data);
}

function push_file($path, $name)
{
    // check of het een path is en niet een folder
    if(is_file($path))
    {
        // required for IE
        if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }

        // Build the headers to push out the file properly.
        header('Pragma: public');     // required
        header('Expires: 0');         // no cache
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT');
        header('Cache-Control: private',false);
        header('Content-Type: application/octet-stream');  // Add the mime type from Code igniter.
        header('Content-Disposition: attachment; filename="'. basename($name).'"');  // Add the file name
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: '.filesize($path)); // provide file size
        header('Connection: close');
        readfile($path); // push it out
        exit();

        //str_replace('"', '\\"', basename($file)) . '"')
        //str_replace('(', '\(', basename($file)) . '"')
    }
}
4

2 回答 2

0

做一件事我刚刚遇到了类似的问题,在你的application/config/config.php

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-()';

请参阅()字符串末尾的 。

于 2013-09-19T13:43:09.473 回答
0

From the var_dump output, it appears that your filename is stored as HTML entities. You can use html_entity_decode() to get the actual filename. Once you have the filename, you can do an str_replace() on the string as you tried to do before:

$name = end($this->uri->segments);
$name = html_entity_decode($name);
$newstring = str_replace('(', '', $name);
//convert it back to HTML entities if necessary
于 2013-09-19T13:52:40.290 回答