简体   繁体   English

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

[英]Is “(” or “)” a special character for PHP/CodeIgniter?

Where i came across at first while i was push a file to the header to download it. 我刚开始将文件推送到标题以进行下载时遇到的地方。

Now it works just fine. 现在工作正常。 However, whenever one of my files has an "(" or ")" in the name, the push doesn't work. 但是,只要我的文件之一的名称中带有“(”或“)”,该推送均无效。

// 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();

It will simply excecute the exit() and will give a black page. 它只会执行exit()并给出一个黑页。 Now I thought this was a character that i had to escape. 现在我认为这是我必须逃避的角色。 I've tried to escape it with: 我试图用以下方法逃避它:

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

This didn't seem to work. 这似乎没有用。 Now i've tried to atleast get rid of the "(" to see if my replace function actually reacts to the "(" character. So i wanted to simply delete out of the string. 现在,我尝试至少摆脱“(”,看看我的替换功能是否实际上对“(”字符有反应。所以我只想从字符串中删除。

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

Now what's strange about this is that is doesnt seems to do anything. 现在,对此感到奇怪的是,它似乎什么也没做。 Even more strange is when i do this: 当我这样做时,更奇怪的是:

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

It actually does work! 它确实有效!

This is where i get my $name from: 这是我从中获得$ name的地方:

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

So it seems like my Uri segment is an object or something, atleast it doesn't act as a real string? 如此看来,我的Uri片段是对象还是其他东西,至少它不充当真正的字符串?

I hope I was clear enough. 我希望我足够清楚。 Thanks in advance, Nkmol. 在此先感谢Nkmol。

Example text name: 示例文字名称:

test(2) 测试(2)

Function 功能

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

Do one thing I just came across a similar problem, in your application/config/config.php : 在您的application/config/config.php ,做一件事我刚遇到类似的问题:

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

See the () at the end of the string. 请参阅字符串末尾的()

From the var_dump output, it appears that your filename is stored as HTML entities. var_dump输出中,您的文件名似乎存储为HTML实体。 You can use html_entity_decode() to get the actual filename. 您可以使用html_entity_decode()来获取实际的文件名。 Once you have the filename, you can do an str_replace() on the string as you tried to do before: 获得文件名后,您可以像以前尝试的那样对字符串执行str_replace()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM