是";(“或”)或;PHP/CodeIgniter的特殊字符?

是";(“或”)或;PHP/CodeIgniter的特殊字符?,php,codeigniter,object,uri,special-characters,Php,Codeigniter,Object,Uri,Special Characters,当我把一个文件推到头上下载它时,我首先遇到了这个问题 现在它工作得很好。但是,每当我的一个文件的名称中有一个“(”或“)”时,推送就不起作用 // required for IE if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); } // Build the headers to push out the file properly.

当我把一个文件推到头上下载它时,我首先遇到了这个问题

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

// 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)) . '"')
    }
}

做一件事我刚刚在
应用程序/config/config.php
中遇到了类似的问题:

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

请参见字符串末尾的
()

var\u dump
输出中,您的文件名似乎存储为HTML实体。您可以使用来获取实际的文件名。获得文件名后,您可以像以前一样对字符串执行以下操作:

$name = end($this->uri->segments);
$name = html_entity_decode($name);
$newstring = str_replace('(', '', $name);
//convert it back to HTML entities if necessary

var#u dump(end($this->uri->segments))
output是什么?“string'test'l(;ol.txt'(length=17)”很有趣…更新:str#u replace('l(;','',$name)确实有效。但是还不能下载。它与空间相同,我应该将其转义,而不是删除它。很抱歉,我应该在我的帖子中这样说。设置该配置时,您会收到一个错误:“遇到一个错误,您提交的URI不允许使用字符。”我已经调整了配置文件:)在您的问题中发布一次您的整个函数。添加了下载和推送函数:)谢谢,我确实可以替换字符串:)但它仍然拒绝下载该文件。我不知道为什么我甚至应该避开这个字符。试图用它之前给我的Html代码替换它:(;但这也给了我一个黑屏:SUpdate:My path“shared\test\test(2.txt)”没有通过:
如果(is#file($path)){}
这同样只出现在“(”或“)在name/path.Update更新中:我尝试了对我的路径进行相同的解码。现在我删除了路径中的“(“and”)。结合文件名,它似乎对文件进行了重新登录:S(即使我放置了test2.txt)。有点奇怪,但它起了作用!:)非常感谢!