Commit 4f2407f3 by nurchamim

modul: download helper

status: selesai
keterangan: tambah fungsi removeSpecialChar untuk filename download
parent 6fee5025
...@@ -26,133 +26,129 @@ ...@@ -26,133 +26,129 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
* *
* @package CodeIgniter * @package CodeIgniter
* @author EllisLab Dev Team * @author EllisLab Dev Team
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
* @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/) * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
* @license https://opensource.org/licenses/MIT MIT License * @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com * @link https://codeigniter.com
* @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
* *
* @package CodeIgniter * @package CodeIgniter
* @subpackage Helpers * @subpackage Helpers
* @category Helpers * @category Helpers
* @author EllisLab Dev Team * @author EllisLab Dev Team
* @link https://codeigniter.com/user_guide/helpers/download_helper.html * @link https://codeigniter.com/user_guide/helpers/download_helper.html
*/ */
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
if ( ! function_exists('force_download')) if (!function_exists('force_download')) {
{ /**
/** * Force Download
* Force Download *
* * Generates headers that force a download to happen
* Generates headers that force a download to happen *
* * @param string filename
* @param string filename * @param mixed the data to be downloaded
* @param mixed the data to be downloaded * @param bool whether to try and send the actual file MIME type
* @param bool whether to try and send the actual file MIME type * @return void
* @return void */
*/ 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;
{ } elseif ($data === NULL) {
return; if (!@is_file($filename) or ($filesize = @filesize($filename)) === FALSE) {
} return;
elseif ($data === NULL) }
{
if ( ! @is_file($filename) OR ($filesize = @filesize($filename)) === FALSE) $filepath = $filename;
{ $filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename));
return; $filename = end($filename);
} } else {
$filesize = strlen($data);
$filepath = $filename; }
$filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename));
$filename = end($filename); // Set the default MIME type to send
} $mime = 'application/octet-stream';
else
{ $x = explode('.', $filename);
$filesize = strlen($data); $extension = end($x);
}
if ($set_mime === TRUE) {
// Set the default MIME type to send if (count($x) === 1 or $extension === '') {
$mime = 'application/octet-stream'; /* If we're going to detect the MIME type,
* we'll need a file extension.
$x = explode('.', $filename); */
$extension = end($x); return;
}
if ($set_mime === TRUE)
{ // Load the mime types
if (count($x) === 1 OR $extension === '') $mimes =& get_mimes();
{
/* If we're going to detect the MIME type, // Only change the default MIME if we can find one
* we'll need a file extension. if (isset($mimes[$extension])) {
*/ $mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension];
return; }
} }
// Load the mime types /* It was reported that browsers on Android 2.1 (and possibly older as well)
$mimes =& get_mimes(); * need to have the filename extension upper-cased in order to be able to
* download it.
// Only change the default MIME if we can find one *
if (isset($mimes[$extension])) * Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/
{ */
$mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension]; 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);
}
/* It was reported that browsers on Android 2.1 (and possibly older as well)
* need to have the filename extension upper-cased in order to be able to if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE) {
* download it. return;
* }
* Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/
*/ // Clean output buffer
if (count($x) !== 1 && isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/Android\s(1|2\.[01])/', $_SERVER['HTTP_USER_AGENT'])) if (ob_get_level() !== 0 && @ob_end_clean() === FALSE) {
{ @ob_clean();
$x[count($x) - 1] = strtoupper($extension); }
$filename = implode('.', $x);
} // Generate the server headers
header('Content-Type: ' . $mime);
if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE) header('Content-Disposition: attachment; filename="' . $filename . '"');
{ header('Expires: 0');
return; header('Content-Transfer-Encoding: binary');
} header('Content-Length: ' . $filesize);
header('Cache-Control: private, no-transform, no-store, must-revalidate');
// Clean output buffer
if (ob_get_level() !== 0 && @ob_end_clean() === FALSE) // If we have raw data - just dump it
{ if ($data !== NULL) {
@ob_clean(); exit($data);
} }
// Generate the server headers // Flush 1MB chunks of data
header('Content-Type: '.$mime); while (!feof($fp) && ($data = fread($fp, 1048576)) !== FALSE) {
header('Content-Disposition: attachment; filename="'.$filename.'"'); echo $data;
header('Expires: 0'); }
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$filesize); fclose($fp);
header('Cache-Control: private, no-transform, no-store, must-revalidate'); exit;
}
// If we have raw data - just dump it }
if ($data !== NULL)
{ /**
exit($data); * Fungsi untuk menghapus special characters
} */
if (!function_exists('removeSpecialChar')) {
// Flush 1MB chunks of data function removeSpecialChar($str)
while ( ! feof($fp) && ($data = fread($fp, 1048576)) !== FALSE) {
{ $res = preg_replace('/[^a-zA-Z0-9_ -]/s', ' ', $str);
echo $data; return $res;
} }
fclose($fp);
exit;
}
} }
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