I am using ajax to call a php file passing along the data I want written to an Excel file. I do not want to save the Excel file on the server, but want to use the $objWriter->save('php://output'); (part of PHPExcel) to open it in the browser.
If I copy the ajax script call into my browser (outside my web application), the browser creates the file and the browser gives the user the option to open/save it.
But...when I run it through the ajax call in my web application, nothing shows up.
I've seen many posts where people had similar issues, but they do not really match what I am trying to do. Most seemed to actually create the file on the server and then return the file name back as the result to the ajax call and then open that file. Since I am not creating a file on the server, that won't work for me.
Is there a way to accomplish this with ajax, php, and PHPExcel? Is there a way to open the file as part of the $objWriter->save('php://output') statement?
Well I got around the issue....instead of calling the script using the ajax call, I did a window.open and used the script call I was using for the ajax call as the input. So what it does is temporarily open a window, which in return calls the php script (passing all parameters). Once the Excel file is created, the window closes but the browser file shows for the user. So except for a window temporarily flashing, the end result is what was originally desired.
Related
I'm trying to serve a file for download to a user, and I'm having trouble with fpassthru. The function I'm using to download a file is:
http://pastebin.com/eXDpgUqq
Note that the file is successfully created from a blob, and is in fact the file I want the user to download. The script exits successfully, and reports no errors, but the file is not downloaded. I can't for the life of me think what's wrong.
EDIT: I removed the error suppression from fopen(), but it still reports no error. Somehow the data in the output buffer is never being told to be downloaded by the browser.
I tried your code (without the blob part), and it worked fine. I can download a binary file. Based on my experience, here are something to check:
Has the file been completely saved before you initiate the reading? Check the return value of file_put_contents.
How large is the file? fpassthru reads the whole file into memory. If the file is too large, memory might be insufficient. Please refer to http://board.phpbuilder.com/showthread.php?10330609-RESOLVED-php-driven-file-download-using-fpassthru for more information.
Instead of downloading the file to local server (reading the whole file into server’s memory, and letting the client download the file from the server), you can create an SAS URL, and simply redirect the browser to the URL. Azure will take care of download automatically. You many want to refer to http://blogs.msdn.com/b/azureossds/archive/2015/05/12/generating-shared-access-signature-sas-using-php.aspx for a sample.
I was able to download the file by passing a stream obtained with the Azure API directly to fpassthru, without creating a file. Unfortunately, I can't show the code because it belongs to a project that I have finished working on and the code is no longer available to me.
I have a strange setup at the moment (busy with a migration)
I have a 4th Dimension application which calls php scripts (which is our soon to be, only application. Dropping 4D)
This script thats called from 4D needs to open a html file in the browser.
Is this possible?
Iv'e tried something along the lines of
header('Location: ./DischargeLetter.php?id='.urlencode($id));
This passes the html document (report) that I want to open along with the patient id.
calling it in the browser works (obviously hey), but how can I get php to open the browser or a tab and head to that page?
From what I understand you have a command line script in php and you are trying to open a browser on the server?
If you are using the php like a command scripting language you can use http://php.net/manual/en/function.shell-exec.php to call other commands so you can call something like
shell_exec('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe $url');
So I know how to upload a file to a web-server with PHP. Instead of uploading it though, I just want to read the data from the file and use it, WITHOUT the upload part. Could someone link me up or give me an example plz?
from HTML, the file is always uploaded to the server, to a temp directory. if from PHP you don't move it to another directory, it will be deleted later, but you can still use it and read it on the script that handles the upload, as shown in the example of is_uploaded_file()
The only way to do that is to send the data of the file via POST and work with it via something like
$postData = file_get_contents( 'php://input' );
PHP is a server-side language, which means it either needs a server-side copy of the file (since it can't access the client) or you need to send parts of the file via common HTTP request methods that PHP can work with (POST or even GET)
There may be a way using JavaScript, but I can't think of any
a javascript possible solution:
https://developer.mozilla.org/en/DOM/FileReader
not cross-browser, works only in firefox and webkit html5 api compatible versions
I have an iframe that loads a remote page (not hosted on the same domain). I would like to edit the contents of the page, but of course, this is not possible, since I don't have permissions.
So, I was wondering, if I have FTP access to the site, would there be a work around to the problem? With FTP, I could copy the files of the site over to my domain, and edit them via an iframe. But I was wondering if there is an alternate method.
Actually, yes. If you had FTP access to the site you could do it in theory.
Basically, something like:
// I used jQuery to speed up writing ajax code, really it could be anything else
jQuery.get('?refresh',function(){ // this function is called when the request finishes
// force the iframe to do a complete refresh (hence the random token)
jQuery('#iframe').attr('src','http://targetsite.com/somefile.php?r='+Math.random());
});
And:
// if the variable in question was set...
if(isset($_REQUEST['refresh'])){
// the following requires "allow_url_fopen" config to be on
// otherwise, you could use any other PHP FTP library
file_put_contents('ftp://username:password#targetsite.com/somefile.php','Hello');
}
Why use iFrames? If you need to load the content of a page hosted on another server, you could grab its content with cURL or some of the PHP file wrappers, e.g. the PHP readfile function. Viola!
If you used readfile(..) you can also make edits to the file content you've loaded before you display it. If you have permission, you could also use include() to read the file via HTTP if you are certain that a valid PHP file will be returned from your request.
I have a file located on my server and I want to remotely access that file from another server and execute source code of the file from another PHP file located on the another server.
I have had a look at “File_get_contents” however this only obtains the content displayed by that PHP file, as can be seen below.
So therefore is it possible for a PHP file from an external server to read the source code of the PHP file located on my server and execute the commands on the external server?
You can use an extension other than .php for the source file, then use file_get_contents (or similar) to retrieve the contents.
Not using the php extensions will prevent PHP from parsing it as code, and just send it over as text instead.
However, that will also make the source readable to anyone who navigates to the file in a browser, as well as introducing a possible major vulnerability. You should look into why this is necessary and if it can be avoided somehow (perhaps calling the file on the other server with GET or POSTed parameters).