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. Now I thought this was a character that i had to escape. I've tried to escape it with:
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);
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';
$newstring = str_replace('(', '', $name);
echo $newstring;
It actually does work!
This is where i get my $name from:
$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?
I hope I was clear enough. Thanks in advance, Nkmol.
Example text name:
test(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)) . '"')
}
}
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