this is a call to a controllers function :
function downloadFile(fn,con)
{
var loadUrl = "/Fast-Docs/index.php/Docs/downloadFile";
alert('hi');
$.post(
loadUrl,
{content: con, filename: fn});
}
controllers function :
public function downloadFile()
{
$this->load->helper('download');
$content=$this->input->post('content');
$fn=$this->input->post('filename');
return force_download($fn,$content);
}
but file is not downloading.
i know the reason why its not working but i dnt know how to fix it. so whats the solution. you need some more info , feel free to ask.
I don't completely understand what you are trying to do. Anyway, there is a fundamental problem in your code.
You are doing an ajax request to the "/Fast-Docs/index.php/Docs/downloadFile" URL, but you are ignoring the response.
My suggestion is: forget about ajax and use a simple link.
Download file
Then in the controller:
public function downloadFile($filename)
{
//Validate file name
if($filename != 'validFileName') die('Invalid file name');
//Set headers
$this->load->helper('download');
$content = file_get_contents('/images/' . $filename);
force_download($filename, $content);
}
If for some reason you can't use a link, you can redirect to the download page (by setting window.location).
If you really need to use ajax, give a look at this.
Related
I am trying to display records from query in a table and on button clicked to make an ajax call to download the information in an xml format. The query is executed from method inside a class and the ajax request makes call to a different method inside the same class. The first method fills two private properties inside the class and the second property(the one called through the ajax request) must read the properties and fill the data inside table and make the file downloadable. When I try to read the properties from the same class though I get nulls and the foreach returns an error.
This is my ajax request(downloaderScript.js):
;
$("#downloadBtn").click(function (event) {
event.preventDefault();
event.stopPropagation();
$.ajax({
url: 'allClients.php',
type: 'post',
data: {action: 'downloader'},
complete: function (result) {
console.log(result);
},
error: function () {
console.log('Error');
}
});
});
This is the class from which I call the first and the second methods:
class HttpHandlerClient extends HttpHandlerAbstract
{
private $clientsService;
public $storedClientsHeadings;
public $storedClientsData;
public function viewAllClients()
{
$data = $this->clientsService->getAllClients(clientEntity::class);
if(isset($data)) {
$this->storedClientsHeadings = ["Client Names:", "Delivery Address:", "Phone number:"];
$this->storedClientsData = $data;
$this->render('allClientsView', $data);
}
else
{
$this->redirect('clientAdd');
}
}
public function downloader()
{
header("Content-Type: text/plain");
var_dump($this->storedClientsHeadings);
foreach ($this->storedClientsHeadings as $arrayName)
{
echo implode("\t", $arrayName)."\r\n";
}
/**
* #var clientEntity $clientData
*/
foreach ($this->storedClientsData as $clientData)
{
echo implode("\t", $clientData->getClientName())."\r\n";
echo implode("\t", $clientData->getAddressForDelivery())."\r\n";
echo implode("\t", $clientData->getPhone())."\r\n";
}
$filename = "clients_".date("Y-m-d").".xls";
header("Content-Disposition:attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
}
And this is the php file that i use between my ajax request and the php class(The file name is: allClients.php, in common.php I make an instance of the class HttpHandlerClient):
require_once 'common.php';
if(isset($_POST['action'])){
$myHttpHandlerClient->downloader();
} elseif (isset($_GET['typer'])) {
$myHttpHandlerClient->viewClientByNumber($_GET['typer']);
} else {
$myHttpHandlerClient->viewAllClients();
}
Sorry if my question is trivial, I even started doubting that after require_once I re-run the code in common.php, making a new instance of HttpHandlerClient and because of this I get nulls in the properties. But when I was reading the documentation in php's site I did not read such a thing. Any help will be appreciated, thanks.
It sounds like what may be happening is that there is a disconnect between the javascript initially loading and then more markup being added to the DOM.
The page loads up which runs your javascript. At this point the JS only knows what is currently on the page.
Your make the first call which changes the DOM. Your javascript does not know about these changes.
You try to reference something that has not been recognized by your initial load of the Javascript.
After you make the call that changes the DOM, you may have to reinitialize your JS to recognize the changes.
Good luck
I have an array that loaded data from excel, now I need to send this data to another view for a review of test, but this not found because is a array in a function, I try to use serialize and unserialize but this send a error of limit of characteres in the url.
public function excel() {
$this->loadModel('SoyaProductorCompra');
$excel=array();
$k=0;
if ($this->request->is('post')) {
$datos = new Spreadsheet_Excel_Reader();
$datos->read($this->request->data['SoyaProductorCompra']['excel']['tmp_name']);
for ($i = 2; $i <= $datos->sheets[0]['numRows']; $i++) {
$excel[$k]['producto']=$datos->sheets[0]['cells'][$i][1];
$excel[$k]['toneladas']=$datos->sheets[0]['cells'][$i][2];
$excel[$k]['precio']=$datos->sheets[0]['cells'][$i][3];
$excel[$k]['total']=$datos->sheets[0]['cells'][$i][4];
$excel[$k]['fecha']=$datos->sheets[0]['cells'][$i][5];
$excel[$k]['id']=$datos->sheets[0]['cells'][$i][6];
$k++;
}
$this->set('excels',$excel);
//return $this->redirect(array('action' => 'revision', serialize($excel))); not found
}
}
this is my other function that recive the array and show in my view but not found
public function revicionexcel($data) {
//$data=unserialize($data); not work
//debug($data); not work
}
I wouldn't send the data of a whole spreadsheet through a query string regardless, even if it were small enough to do that. I mean, what would happen if someone started manually editing the URL for instance? Either write the data to a file, or save it in the session instead.
It seems that you're calling the funciton inside the same controller, right?
If so, then why you just don't use:
public function excel()
{
//read excel into $excel variable
$this->revicionexcel($excel);
}
so, no need of redirects here
Although it's recommended that you have the excel reading in the model layer, as this layer is intended for all kind of data management.
Edit:
Then, based on your comments, you can move all the reading to the model, and call it from the controller:
SoyaProductorCompra model:
public function excel($data) {
//your excel reading function as you have it on the controller.
//change all "$this->request->data" references for the parameter "$data"
//be sure to return the excel properly.
...
return $excel;
}
SoyaProductorCompras controller:
public function revision()
{
$excel = $this->SoyaProductorCompra->excel($this->request->data);
$this->set('excels', $excel);
}
What we're doing here is to call the excel opening in the model from the action revision(), and sending them to its view. You don't need to redirect here.
I am creating website in PHP. I am using MVC in PHP. My website works like this, if user go to example.com/about then it it will load About class and index() function. If user will go to localhost/about/founder then it will load founder() function from About class. but the thing is that if I go to localhost/About or localhost/AbOut or anything like that it is loading default index() function from About class file. So what to do with case sensitivity? I mean I want my script to load index() function from class file if it is localhost/about or localhost/terms. If anything is in uppercase, then it should load 404 error function. 404 error function is already set in my site.
Please help me friends.
here is my Bootstrap.php class file
<?php
/*
Bootstrap class to run functions by URL
*/
class Bootstrap {
public $_req;
public $_body;
public $_file;
public $_error;
function __construct(){
if(empty($_GET['req'])){
require 'classes/home.php';
$this->_body = new Home();
$this->hdr($this->_body->head());
$this->_body->index();
$this->ftr();
exit();
}
$this->_req = rtrim($_GET['req'], '/');
$this->_req = explode('/', $this->_req );
$_file = 'classes/'.$this->_req[0].'.php';
if(file_exists($_file)){
require $_file;
}
else {
$this->error(404);
}
$this->_body = new $this->_req[0];
$this->hdr($this->_body->head());
if(isset($this->_req[2])){
if(method_exists($this->_req[0], $this->_req[1])){
$this->_body->{$this->_req[1]}($this->_req[2]);
}else {
$this->error(404);
}
}else {
if(isset($this->_req[1])){
if(method_exists($this->_req[0], $this->_req[1])){
$this->_body->{$this->_req[1]}();
}else {
$this->error(404);
}
}else {
$this->_body->index();
}
$this->ftr();
}
}
//this function is to set header in html code
public function hdr($var = false){
echo '<!DOCTYPE HTML><html><head>'.$var.'</head><body>';
}
//this function is tp set footer in html code
public function ftr($var = false){
echo $var.'</body></html>';
}
//error handler
public function error($var){
require 'classes/er_pg.php';
$this->_error = new Error();
$this->_error->index($var);
}
}
You shouldn't use anything to load non-lowercase URLs because of the duplicate content, and that's a good thing you're doing. The wrong URLs should fail automatically in such cases.
However, since you didn't show how are you making those calls, then only thing I can suggest at this point is to check if the called method exists (case-sensitive), and if not, throw/redirect to a 404 page (header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");).
UPDATE
After all the chat in the comments, seems like file_exists is not case-sensitive in your case, which is really weird. Hopefully someone will be able to figure it out so I can delete this (keeping it because of the info in the comments).
I solved the problem. I used this
if(ctype_lower($this->_req[0])){
$_file = 'classes/'.$this->_req[0].'.php';
and now its working. Thanx anyways friends.
I'm using a plugin called jQuery file upload to upload images to a page. Currently it uploads with the original image name as the file name (IMG_1234). I need a specific format for the image name on the server (eg 1.123456.jpg)
I found this PHP code that works for changing the image name:
class CustomUploadHandler extends UploadHandler
{
protected function trim_file_name($name, $type) {
$name = time()."_1";
$name = parent::trim_file_name($name, $type);
return $name;
}
}
When I upload an image, it is named correctly, but the link for the image preview is undefined. This prevents me from deleting the image via the plugin.
The variable data.url is undefined... If I go back to the original code that doesn't rename the image, everything works fine.
Has anyone had any experience with this plugin that could help? Thanks!
EDIT:
I've found part of the problem at least...the function to return the download link (which is also used for deletion) is giving the original file name, not the updated one. I am really new to PHP classes, so I'm not sure where the variable originates and how to fix it. I'd really appreciate any help I can get!
Here's the PHP code for that function:
protected function get_download_url($file_name, $version = null, $direct = false) {
if (!$direct && $this->options['download_via_php']) {
$url = $this->options['script_url']
.$this->get_query_separator($this->options['script_url'])
.'file='.rawurlencode($file_name);
// The `$file_name` variable is the original image name (`IMG_1234`), and not the renamed file.
if ($version) {
$url .= '&version='.rawurlencode($version);
}
return $url.'&download=1';
}
if (empty($version)) {
$version_path = '';
} else {
$version_url = #$this->options['image_versions'][$version]['upload_url'];
if ($version_url) {
return $version_url.$this->get_user_path().rawurlencode($file_name);
}
$version_path = rawurlencode($version).'/';
}
return $this->options['upload_url'].$this->get_user_path()
.$version_path.rawurlencode($file_name);
}
EDIT 2: I think it has something to do with 'param_name' => 'files', in the options. Anyone know what that does?
Fixed it by editing the trim_file_name function inside UploadHandler.php instead of extending the class in index.php.
In order to provide an ajax action I do the following:
public function preDispatch()
{
if ($this->getRequest()->isXmlHttpRequest()) {
Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
Zend_Layout::getMvcInstance()->disableLayout();
}
}
but the response comes with the following error:
script 'async/tax.phtml' not found in path (/var/www/app/trunc/application/views/scripts/:./views/scripts/)
Why not using the Ajaxcontext action helper ? http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch
// supposedly in your async controller
public function init() {
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('tax', 'html')
}
Then name your view "async/tax.ajax.phtml" and ensure that your ajax call ask for the html format.
That way you can even adapt your code to automatically respond with json, phtml or xml with the same controller/action.
Have you checked whether the file tax.phtml exists in /var/www/app/trunc/application/views/scripts/ .
If exists , then in tax.phtml place echo exit; at the bottom of the file.