Download file in Zend 1 (PHP): File name is japanese - zend-framework

I using Zend FW 1 to download file.
File need download have file name japanese : るファイルを選択.pdf
$this->path: is file path . Ex : D:\るファイルを選択.pdf
This is my code in PHP
public function send() {
if($this->checkPath()) {
// fileinfo extention enable
$type = mime_content_type($this->path);
if ($this->getRequest()->isSecure()) { // HTTPS sites - watch out for IE! KB812935 and KB316431.
header('Content-Description: File Transfer');
header('Cache-Control: max-age=10');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: ');
} else { //normal http - prevent caching at all cost
header('Content-Description: File Transfer');
header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: no-cache');
}
if ($this->isIE()) {
$name = rawurlencode($this->name);
} else {
$name = $this->s($this->name);
}
$name = urlencode($name);
$Disposition = "attachment;filename*=UTF-8''$name";
$this->getResponse()->setHeader('Content-Type', $type . ";charset=utf-8")
->setHeader('Content-Disposition', $Disposition, true)
->setHeader('Content-Transfer-Encoding', 'binary', true)
->setHeader('X-Sendfile', readfile($this->path), true)
->sendResponse();
unlink($this->path);
}
}
But mime_content_type($this->path) retun :
mime_content_type(D:\るファイルを選択.pdf): failed to open stream: No such
file or directory in ...
I had set UTF-8 in getResponse

Try to use finfo
$result = new finfo();
echo $result->file($filename, FILEINFO_MIME_TYPE);

Related

Codeigniter 3 | Copy zip file and extract

I've function for demo import database:
/**
* Demo importer post [Template 01 - Fashion / Clothing]
*/
public function demo_importer_post_template01()
{
$folder_name = 'template01';
$file_name = 'template01.sql';
$path = 'assets/backup_db/'; // Codeigniter application /assets
$file_restore = $this->load->file($path . $folder_name . '/' . $file_name, true);
$file_array = explode(';', $file_restore);
foreach ($file_array as $query)
{
$this->db->query("SET FOREIGN_KEY_CHECKS = 0");
$this->db->query($query);
$this->db->query("SET FOREIGN_KEY_CHECKS = 1");
}
$this->session->set_flashdata('success', trans("msg_updated"));
redirect($this->agent->referrer());
}
This function insert database from /assets/backup_db/template01/template01.sql
This working fine. Now I need with this function extract some static files from: /assets/backup_db/template01/template01.zip
Can anyone help me How to copy template01.zip to root public_html and then unzip files from template01.zip

Joomla website is getting 303 redirect loop

My website is sometimes getting a 303 redirect loop on the main page. This gives me an error when trying to view the site "the webpage has a redirect loop", but most of the time is loads fine. But, the 303 redirect doesn't look good for SEO. I'm running the Joomla 3.2.3 with the Zo2 framework. I used the firefox live http headers tool and this is what it shows:
HTTP/1.1 303 See other
Date: Sat, 19 Apr 2014 06:42:44 GMT
Server: Apache mod_fcgid/2.3.10-dev
X-Powered-By: PHP/5.4.26
Set-Cookie: 81f5073a1f9d10dc244e07c98216335e=hb7mb5k3v0vftstcgtdbonikq6; path=/; HttpOnly
Location: /
Cache-Control: max-age=600
Expires: Sat, 19 Apr 2014 06:52:44 GMT
Content-Length: 0
Keep-Alive: timeout=5
Connection: Keep-Alive
I went through and disable every plugin in Joomla one at a time and when I disable the Zo2 framework plugin the 303 redirect went away. I've asked on their forums and they haven't been able to help me. So, I searched through all of the files in the zo2 plugin directory for the word redirect and this is what I found.
These two are in the site.megamenu.js
!function ($) {
$(document).ready(function ($) {
// when clicking on menu
redirect();
var duration = 0;
var $parent = $('.zo2-megamenu');
var hover_type = $parent.data('hover');
if ($parent.data('duration')) {
duration = $parent.data('duration');
}
function redirect() {
$('.dropdown-toggle').on('click',function(e){
if($(this).parent().hasClass('open') && this.href && this.href != '#'){
window.location.href = this.href;
e.preventDefault();
}
});
}
I have other website running on this server using the same .htaccess and php.ini files so I don't think that could be the problem, because they are not getting a redirect. I would appreciate some help with this. The website is http://www.betterfreestuff.com
/**
* Zo2 Framework (http://zo2framework.org)
*
* #link http://github.com/aploss/zo2
* #package Zo2
* #author Hiepvu
* #copyright Copyright ( c ) 2008 - 2013 APL Solutions
* #license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or Later
*/
!function ($) {
$(document).ready(function ($) {
// when clicking on menu
redirect();
var duration = 0;
var $parent = $('.zo2-megamenu');
var hover_type = $parent.data('hover');
if ($parent.data('duration')) {
duration = $parent.data('duration');
}
if (duration && (hover_type == 'hover')) {
var timeout = duration ? duration + 50 : 500;
$('.nav > li, li.mega').hover(
function(e) {
onMouseIn(this, timeout);
}
,
function (e) {
onMouseOut(this);
}
);
} else if (hover_type == 'click') {
$('.mega-nav').find('.dropdown-submenu').hover(
function(e) {
onMouseIn(this, 100);
}
,
function (e) {
onMouseOut(this);
}
);
}
// for first li tag
function redirect() {
$('.dropdown-toggle').on('click',function(e){
if($(this).parent().hasClass('open') && this.href && this.href != '#'){
window.location.href = this.href;
e.preventDefault();
}
});
}
function onMouseIn (e, timeout) {
var $this = $(e);
if ($this.hasClass('mega')) {
$this.addClass ('hovering');
clearTimeout ($this.data('hoverTime'));
$this.data('hoverTime',
setTimeout(function(){$this.removeClass ('hovering')}, timeout));
clearTimeout ($this.data('hoverTime'));
$this.data('hoverTime',
setTimeout(function(){$this.addClass ('open')}, 100));
} else {
clearTimeout($this.data('hoverTime'));
$this.data('hoverTime',
setTimeout(function () {
$this.addClass('open')
}, 100));
}
}
function onMouseOut (e) {
var $this = $(e);
clearTimeout($this.data('hoverTime'));
$this.data('hoverTime',
setTimeout(function () {
$this.removeClass('open')
}, 100));
}
/** BEGIN: off canvas menu **/
var showOffCanvasMenu = function () {
var $offcanvas = $('.offcanvas');
var $body = $('body');
var $wrapper = $('.wrapper');
$body.addClass('overflow-hidden');
$wrapper.addClass('offcanvas-push');
var $overlay = $('<div />').addClass('offcanvas-overlay').appendTo('body');
$overlay.css({
top:0,
right:0,
bottom:0
}).fadeIn();
$overlay.click(function() {
$body.removeClass('overflow-hidden');
$wrapper.removeClass('offcanvas-push');
$offcanvas.removeClass('active');
$('.offcanvas-overlay').remove();
});
};
var hideOffCanvasMenu = function () {
var $body = $('body');
var $wrapper = $('.wrapper');
var $offcanvas = $('.offcanvas');
$body.removeClass('overflow-hidden');
$wrapper.removeClass('offcanvas-push');
$offcanvas.removeClass('active');
$('.offcanvas-overlay').remove();
};
$('[data-toggle=offcanvas]').click(function() {
var $offcanvas = $('.offcanvas');
$offcanvas.toggleClass('active');
if ($offcanvas.hasClass('active')) {
showOffCanvasMenu();
}
else {
hideOffCanvasMenu();
}
});
$('body').on('click', '.sidebar-nav a', function() {
if (!$(this).hasClass('nav-oc-toggle')) hideOffCanvasMenu();
});
$('body').on('click', '.sidebar-close', function() {
hideOffCanvasMenu();
});
// new off canvas submenu
$('body').on('click', '.nav-oc-toggle', function() {
var $this = $(this);
var $parent = $this.closest('.nav-parent');
if ($parent.find('> .submenu').hasClass('in')) $this.removeClass('icon-caret-up').addClass('icon-caret-down');
else $this.removeClass('icon-caret-down').addClass('icon-caret-up');
});
/** END: off canvas menu **/
});
}(jQuery);
I searched for 303 in all of the files on my server and these files came back with redirect statements.
cms.php
public function redirect($url, $moved = false)
{
// Handle B/C by checking if a message was passed to the method, will be removed at 4.0
if (func_num_args() > 1)
{
$args = func_get_args();
/*
* Do some checks on the $args array, values below correspond to legacy redirect() method
*
* $args[0] = $url
* $args[1] = Message to enqueue
* $args[2] = Message type
* $args[3] = $moved
*/
if (isset($args[1]) && !empty($args[1]) && !is_bool($args[1]))
{
// Log that passing the message to the function is deprecated
JLog::add(
'Passing a message and message type to JFactory::getApplication()->redirect() is deprecated. '
. 'Please set your message via JFactory::getApplication()->enqueueMessage() prior to calling redirect().',
JLog::WARNING,
'deprecated'
);
$message = $args[1];
// Set the message type if present
if (isset($args[2]) && !empty($args[2]))
{
$type = $args[2];
}
else
{
$type = null;
}
// Enqueue the message
$this->enqueueMessage($message, $type);
// Reset the $moved variable
$moved = isset($args[3]) ? (boolean) $args[3] : false;
}
}
application.php
public function redirect($url, $msg = '', $msgType = 'message', $moved = false)
{
// Check for relative internal links.
if (preg_match('#^index2?\.php#', $url))
{
$url = JUri::base() . $url;
}
// Strip out any line breaks.
$url = preg_split("/[\r\n]/", $url);
$url = $url[0];
/*
* If we don't start with a http we need to fix this before we proceed.
* We could validly start with something else (e.g. ftp), though this would
* be unlikely and isn't supported by this API.
*/
if (!preg_match('#^http#i', $url))
{
$uri = JUri::getInstance();
$prefix = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
if ($url[0] == '/')
{
// We just need the prefix since we have a path relative to the root.
$url = $prefix . $url;
}
else
{
// It's relative to where we are now, so lets add that.
$parts = explode('/', $uri->toString(array('path')));
array_pop($parts);
$path = implode('/', $parts) . '/';
$url = $prefix . $path . $url;
}
}
// If the message exists, enqueue it.
if (trim($msg))
{
$this->enqueueMessage($msg, $msgType);
}
// Persist messages if they exist.
if (count($this->_messageQueue))
{
$session = JFactory::getSession();
$session->set('application.queue', $this->_messageQueue);
}
// If the headers have been sent, then we cannot send an additional location header
// so we will output a javascript redirect statement.
if (headers_sent())
{
echo "<script>document.location.href='" . str_replace("'", "&apos;", $url) . "';</script>\n";
}
else
{
$document = JFactory::getDocument();
jimport('phputf8.utils.ascii');
if (($this->client->engine == JApplicationWebClient::TRIDENT) && !utf8_is_ascii($url))
{
// MSIE type browser and/or server cause issues when url contains utf8 character,so use a javascript redirect method
echo '<html><head><meta http-equiv="content-type" content="text/html; charset=' . $document->getCharset() . '" />'
. '<script>document.location.href=\'' . str_replace("'", "&apos;", $url) . '\';</script></head></html>';
}
else
{
// All other browsers, use the more efficient HTTP header method
header($moved ? 'HTTP/1.1 301 Moved Permanently' : 'HTTP/1.1 303 See other');
header('Location: ' . $url);
header('Content-Type: text/html; charset=' . $document->getCharset());
}
}
$this->close();
}
web.php
public function redirect($url, $moved = false)
{
// Import library dependencies.
jimport('phputf8.utils.ascii');
// Check for relative internal links.
if (preg_match('#^index\.php#', $url))
{
// We changed this from "$this->get('uri.base.full') . $url" due to the inability to run the system tests with the original code
$url = JUri::base() . $url;
}
// Perform a basic sanity check to make sure we don't have any CRLF garbage.
$url = preg_split("/[\r\n]/", $url);
$url = $url[0];
/*
* Here we need to check and see if the URL is relative or absolute. Essentially, do we need to
* prepend the URL with our base URL for a proper redirect. The rudimentary way we are looking
* at this is to simply check whether or not the URL string has a valid scheme or not.
*/
if (!preg_match('#^[a-z]+\://#i', $url))
{
// Get a JUri instance for the requested URI.
$uri = JUri::getInstance($this->get('uri.request'));
// Get a base URL to prepend from the requested URI.
$prefix = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
// We just need the prefix since we have a path relative to the root.
if ($url[0] == '/')
{
$url = $prefix . $url;
}
// It's relative to where we are now, so lets add that.
else
{
$parts = explode('/', $uri->toString(array('path')));
array_pop($parts);
$path = implode('/', $parts) . '/';
$url = $prefix . $path . $url;
}
}
// If the headers have already been sent we need to send the redirect statement via JavaScript.
if ($this->checkHeadersSent())
{
echo "<script>document.location.href='" . str_replace("'", "&apos;", $url) . "';</script>\n";
}
else
{
// We have to use a JavaScript redirect here because MSIE doesn't play nice with utf-8 URLs.
if (($this->client->engine == JApplicationWebClient::TRIDENT) && !utf8_is_ascii($url))
{
$html = '<html><head>';
$html .= '<meta http-equiv="content-type" content="text/html; charset=' . $this->charSet . '" />';
$html .= '<script>document.location.href=\'' . str_replace("'", "&apos;", $url) . '\';</script>';
$html .= '</head><body></body></html>';
echo $html;
}
else
{
// All other cases use the more efficient HTTP header for redirection.
$this->header($moved ? 'HTTP/1.1 301 Moved Permanently' : 'HTTP/1.1 303 See other');
$this->header('Location: ' . $url);
$this->header('Content-Type: text/html; charset=' . $this->charSet);
}
}

multiple pdf download using zend framework

i got this code in attaching a pdf file to my site in order for the viewers to download such pdf file..
$fullPath = "../public/pdffiles/FolioPlusUserGuide(v3.0).pdf";
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
header("Cache-control: private");
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
html code:
baseUrl().'/DownloadPdf'?>">DOWNLOAD BROCHURE
this code only accommodates 1 file path, I have more pdf files with different filename and different buttons for each to handle the download event. how can i achieve this?..thanks ahead!..=)
You can pass PDF file name via HTTP request, like this:
DownloadPDF.php?filename=test1.pdf
Then in your DownloadPDF file have something like this:
$pdfdir = "/pdffiles/folder/location/here";
$pdffilename = $_GET['filename'];
$fullPath = $pdfdir.$pdffilename;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
header("Cache-control: private");
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
Now your code is dynamic and you can download any PDF with links like this:
<a href=downloadpdf.php?filename=something.pdf>Something</a>
<a href=downloadpdf.php?filename=something2.pdf>Something2</a>
etc.

How download application in zendframe work

I am using zend frame works . I want to include file download section in my application. I am using this code
> header('Content-Type: application/doc'); header('Pragma: no-cache');
> header('Content-Disposition: attachment; filename="'.$resume.'"');
> readfile(RESUME_PATH_WS . $resume);
But this code is not working .It return file with 0byte . Please help me how to i download files in zend frame work
public function downloadAction() {
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$filename = $this->_request->getParam('filename');
$filePath = folder/path to file/ . $filename;
if (file_exists($filePath)) {
$fileName = basename($filePath);
$fileSize = filesize($filePath);
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: " . $fileSize);
header("Content-Disposition: attachment; filename=" . $fileName);
readfile($filePath);
exit();
} else {
die('The provided file path is not valid.');
}
}
Simply in your html side
Download

Downloaded FLV file is not working

Here is my code..
<?php
ob_clean();
$params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
// block any attempt to the filesystem
if (isset($params['file']) && basename($params['file']) == $params['file']) {
$filename = $params['file'];
} else {
$filename = NULL;
}
// define error message
$err = '<div class="right-panel fl">
<h1> Download Question </h1> <a href="JavaScript:void(0)" class="button" onclick="javascript:history.go(-1)" > Back </a>
<div class="gradient-box" style="margin-top:20px;" ><p style="color:#990000">Sorry, the file you are requesting is unavailable.</p></div></div>';
if (!$filename) {
// if variable $filename is NULL or false display the message
echo $err;
} else {
// define the path to your download folder plus assign the file name
$path = BASE_PATH.QUESTIONS_FILE_PUBLIC.$filename;
$path2 = REL_PATH.QUESTIONS_FILE.$filename;
// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {
// get the file size and send the http headers
$size = filesize($path);
header("Content-type: video/flv");
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error messages if the file can´t be opened
echo file_get_contents($path);
//#readfile($path);
exit;
$file = # fopen($path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
} else {
echo $err;
}
}
?>
Try to use function from this comment to force browser to open download file dialog. It must help.