Commit 4f2407f3 by nurchamim

modul: download helper

status: selesai
keterangan: tambah fungsi removeSpecialChar untuk filename download
parent 6fee5025
......@@ -35,7 +35,7 @@
* @since Version 1.0.0
* @filesource
*/
defined('BASEPATH') OR exit('No direct script access allowed');
defined('BASEPATH') or exit('No direct script access allowed');
/**
* CodeIgniter Download Helpers
......@@ -49,8 +49,7 @@ defined('BASEPATH') OR exit('No direct script access allowed');
// ------------------------------------------------------------------------
if ( ! function_exists('force_download'))
{
if (!function_exists('force_download')) {
/**
* Force Download
*
......@@ -63,23 +62,17 @@ if ( ! function_exists('force_download'))
*/
function force_download($filename = '', $data = '', $set_mime = FALSE)
{
if ($filename === '' OR $data === '')
{
if ($filename === '' or $data === '') {
return;
}
elseif ($data === NULL)
{
if ( ! @is_file($filename) OR ($filesize = @filesize($filename)) === FALSE)
{
} elseif ($data === NULL) {
if (!@is_file($filename) or ($filesize = @filesize($filename)) === FALSE) {
return;
}
$filepath = $filename;
$filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename));
$filename = end($filename);
}
else
{
} else {
$filesize = strlen($data);
}
......@@ -89,10 +82,8 @@ if ( ! function_exists('force_download'))
$x = explode('.', $filename);
$extension = end($x);
if ($set_mime === TRUE)
{
if (count($x) === 1 OR $extension === '')
{
if ($set_mime === TRUE) {
if (count($x) === 1 or $extension === '') {
/* If we're going to detect the MIME type,
* we'll need a file extension.
*/
......@@ -103,8 +94,7 @@ if ( ! function_exists('force_download'))
$mimes =& get_mimes();
// Only change the default MIME if we can find one
if (isset($mimes[$extension]))
{
if (isset($mimes[$extension])) {
$mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension];
}
}
......@@ -115,40 +105,35 @@ if ( ! function_exists('force_download'))
*
* Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/
*/
if (count($x) !== 1 && isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/Android\s(1|2\.[01])/', $_SERVER['HTTP_USER_AGENT']))
{
if (count($x) !== 1 && isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/Android\s(1|2\.[01])/', $_SERVER['HTTP_USER_AGENT'])) {
$x[count($x) - 1] = strtoupper($extension);
$filename = implode('.', $x);
}
if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE)
{
if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE) {
return;
}
// Clean output buffer
if (ob_get_level() !== 0 && @ob_end_clean() === FALSE)
{
if (ob_get_level() !== 0 && @ob_end_clean() === FALSE) {
@ob_clean();
}
// Generate the server headers
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Type: ' . $mime);
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Expires: 0');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$filesize);
header('Content-Length: ' . $filesize);
header('Cache-Control: private, no-transform, no-store, must-revalidate');
// If we have raw data - just dump it
if ($data !== NULL)
{
if ($data !== NULL) {
exit($data);
}
// Flush 1MB chunks of data
while ( ! feof($fp) && ($data = fread($fp, 1048576)) !== FALSE)
{
while (!feof($fp) && ($data = fread($fp, 1048576)) !== FALSE) {
echo $data;
}
......@@ -156,3 +141,14 @@ if ( ! function_exists('force_download'))
exit;
}
}
/**
* Fungsi untuk menghapus special characters
*/
if (!function_exists('removeSpecialChar')) {
function removeSpecialChar($str)
{
$res = preg_replace('/[^a-zA-Z0-9_ -]/s', ' ', $str);
return $res;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment