I have a call to a web service that gives me a response back in the format of a base64 encoded pdf file. I need to save the pdf to a folder on the server.
I can't get the document saved in the folder. It always saves the file locally on the computer and not to the server.
Here is the code i have at this moment.
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="doc-'.$document.'.pdf"');
file_put_contents('~/pdf/'.$document.'.pdf', $wc->out->document);
Does someone have a suggestion where i can find the solution?
thx
Headers are for serving data to the client - you don't need these. Just use file_put_contents
As for the file save location, you know that will save into a folder within the user profile? If you want the current folder, use . instead of ~.
Related
Hello currently I am working on a tool that converts an otherwise file-based stored text array(not really important though what it does). It works great and I am able to download and use it if I download the file off the FTP, but when I try to directly download it to the person using the .php file, it says Invalid file. My current code is:
header('Content-Type: application/octet-stream');
header("Content-Length: " . filesize($dbname));
header('Content-Disposition: attachment; filename="object database.db"');
readfile($dbname);
What is the issue? Is the content-type or something else wrong? Or should I be using readfile(I tried this?)
When I download with the above code, the name of the file seems to be correct or whatever but when I attempt to open it with the SQLite3 Browser(it works when I download directly off the FTP) the program displays "Invalid file format". When I use readfile it downloads as "download" with no file extension and when I attempt to open windows says Invalid file. Any help would be awesome.
There's no application/db content type. You should use application/octet-stream instead. Also filename in Content-Disposition is just filename you suggest client to save file as, but nothing more, so you yet needs to send the file content itself to the remote peer.
readfile(....);
die();
should do the work if your data is stored in the file already, otherwise you can just echo() it instead.
I have a PHP script which is downloading file after it is created.
Everything is working correctly when using PC but when I want to load same script on my iPad for example I can't download it. I'm getting error that file is broken.
What could be the problem?
My headers in PHP file:
header('Content-Description: File Transfer');
header("Content-type: application/ms-word");
header("Content-Disposition: attachment;Filename=offer.doc");
I have a PHP script which is downloading file after it is created.
My bet is you're using HTML to create the file, and giving it a .doc extension to make it look like a Word document.
While this is totally okay and supported by all versions of Word AFAIK, a 3rd party viewer program like on an iPad may be more strict. A HTML based file is technically not a Word document, and the viewer may not be equipped to parse it accordingly.
You may need to resort to generating a real .doc file.
Hey guys I was wondering how I could download a file that is generated on the fly by PHP. The file I want to download would be an XML file. At the moment all my code does is create a long string with all of the data that is to put in the file, it then simply writes the string to a file and saves it with a .XML extension. This is currently working in my local machine using a copy of the website, it won't work on the web server though due to read/write permissions.
So is there a way to generate a file in the fly to be immediately downloaded without storing it on the web server?
If the same script is generating the file, you could echo or print the contents on to the page and use header to force download.
<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="file.xml"');
echo $XMLString;
?>
And if you have a different file for downloading the file, just use file_get_contents and output the file data!
That should do you :)
Just give these two things on the top of the document:
<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="myxml.xml"');
?>
I am using the FTP class in CodeIgniter, they have a function for downloading the file from the FTP, however, its only to the server itself. I am trying to get it to download straight to the user.
I know that i could just save it to the server and then force download and then delete. But its a bit of a hassle if the file is large and it would be slow.
So i am wondering from this code, if there is anyway just to use the force_download CI function?
Example;
$this->ftp->download('/public_html/myfile.html', '/local/path/to/myfile.html', 'ascii');
Thanks!
You simply download the file to PHP's standard output stream instead of a file [stream] like so:
<?php
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="test.txt"');
$this->ftp->download('/public_html/test.txt', 'php://output', 'ascii');
(Note: headers are used to force the download, otherwise the browser would simply print the contents)
You're welcome!
On my server I have a directory with music files, generally in .mp3 format. I'm writing a web application to search for and play these tracks.
All the files are stored, with their absolute server path, artist, album and title info in a MySQL database.
What I want to do is have a PHP file that "outputs" an mp3 file on the server that would normally be inaccessible from the web. So, this is what I want to achieve:
client requests play.php?id=10
PHP gets absolute server path from MySQL database where ID = 10
PHP outputs the file (which would really be at e.g. '/home/user/files/no_web/mp3/Thing.mp3')
To the client, it looks like there is an mp3 file called http://myserver.com/play.php?id=10 and it starts to play.
I'm sure this sort of thing is possible, just not sure how. Thanks in advance :)
You need to send correct content-type header and then just output the file:
header('Content-type: audio/mpeg3');
readfile('filename.mp3');
For reading the file and sending it, you can use the readfile function.
For setting the mime-type, so the browser actually knows what type of file is sent by the webserver, use the header function like:
header('Content-Type: audio/mpeg');
Additionally, you may also want to set the Content-Length HTTP header.
header('Content-Length: ' . filesize($filepath) );
If all you're trying to do is let the user download the mp3, just use the readfile command which will read the mp3 file and pass it along to the client. However you need to make sure to set the mime-type correctly.