Commit 4f2407f3 by nurchamim

modul: download helper

status: selesai
keterangan: tambah fungsi removeSpecialChar untuk filename download
parent 6fee5025
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
* @since Version 1.0.0 * @since Version 1.0.0
* @filesource * @filesource
*/ */
defined('BASEPATH') OR exit('No direct script access allowed'); defined('BASEPATH') or exit('No direct script access allowed');
/** /**
* CodeIgniter Download Helpers * CodeIgniter Download Helpers
...@@ -49,8 +49,7 @@ defined('BASEPATH') OR exit('No direct script access allowed'); ...@@ -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 * Force Download
* *
...@@ -63,23 +62,17 @@ if ( ! function_exists('force_download')) ...@@ -63,23 +62,17 @@ if ( ! function_exists('force_download'))
*/ */
function force_download($filename = '', $data = '', $set_mime = FALSE) function force_download($filename = '', $data = '', $set_mime = FALSE)
{ {
if ($filename === '' OR $data === '') if ($filename === '' or $data === '') {
{
return; return;
} } elseif ($data === NULL) {
elseif ($data === NULL) if (!@is_file($filename) or ($filesize = @filesize($filename)) === FALSE) {
{
if ( ! @is_file($filename) OR ($filesize = @filesize($filename)) === FALSE)
{
return; return;
} }
$filepath = $filename; $filepath = $filename;
$filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename)); $filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename));
$filename = end($filename); $filename = end($filename);
} } else {
else
{
$filesize = strlen($data); $filesize = strlen($data);
} }
...@@ -89,10 +82,8 @@ if ( ! function_exists('force_download')) ...@@ -89,10 +82,8 @@ if ( ! function_exists('force_download'))
$x = explode('.', $filename); $x = explode('.', $filename);
$extension = end($x); $extension = end($x);
if ($set_mime === TRUE) if ($set_mime === TRUE) {
{ if (count($x) === 1 or $extension === '') {
if (count($x) === 1 OR $extension === '')
{
/* If we're going to detect the MIME type, /* If we're going to detect the MIME type,
* we'll need a file extension. * we'll need a file extension.
*/ */
...@@ -103,8 +94,7 @@ if ( ! function_exists('force_download')) ...@@ -103,8 +94,7 @@ if ( ! function_exists('force_download'))
$mimes =& get_mimes(); $mimes =& get_mimes();
// Only change the default MIME if we can find one // 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]; $mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension];
} }
} }
...@@ -115,40 +105,35 @@ if ( ! function_exists('force_download')) ...@@ -115,40 +105,35 @@ if ( ! function_exists('force_download'))
* *
* Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/ * 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); $x[count($x) - 1] = strtoupper($extension);
$filename = implode('.', $x); $filename = implode('.', $x);
} }
if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE) if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE) {
{
return; return;
} }
// Clean output buffer // Clean output buffer
if (ob_get_level() !== 0 && @ob_end_clean() === FALSE) if (ob_get_level() !== 0 && @ob_end_clean() === FALSE) {
{
@ob_clean(); @ob_clean();
} }
// Generate the server headers // Generate the server headers
header('Content-Type: '.$mime); header('Content-Type: ' . $mime);
header('Content-Disposition: attachment; filename="'.$filename.'"'); header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Expires: 0'); header('Expires: 0');
header('Content-Transfer-Encoding: binary'); header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$filesize); header('Content-Length: ' . $filesize);
header('Cache-Control: private, no-transform, no-store, must-revalidate'); header('Cache-Control: private, no-transform, no-store, must-revalidate');
// If we have raw data - just dump it // If we have raw data - just dump it
if ($data !== NULL) if ($data !== NULL) {
{
exit($data); exit($data);
} }
// Flush 1MB chunks of data // Flush 1MB chunks of data
while ( ! feof($fp) && ($data = fread($fp, 1048576)) !== FALSE) while (!feof($fp) && ($data = fread($fp, 1048576)) !== FALSE) {
{
echo $data; echo $data;
} }
...@@ -156,3 +141,14 @@ if ( ! function_exists('force_download')) ...@@ -156,3 +141,14 @@ if ( ! function_exists('force_download'))
exit; 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