I am trying to download some files. I have written a php script that can gather all of the address of the pdf files I need to download into a array but the file names on that server are odd. How does one go about downloading pdf files with php?
you can try (if I understand your question correctly)
file_put_contents('/your/file/name.pdf',
file_get_contents('http://www.example.com/oddFile.pdf'))
Check out the file or file_get_contents functions. It can take URLs as well as local filenames.
http://www.php.net/manual/en/function.file.php
http://www.php.net/manual/en/function.file-get-contents.php
Related
I am using the dompdf library to create my table based PDF and I can view it online or I can have it download to the users folder of choice.
But what I would like to do is have it save it to the remote server( i dont need it to be save to the users PC), like an automatically upload script that would create the file then upload it to the remote server, so i can then use it within my application later on.
is it possible to point the $_FILES["file"] script say so fetch the php page that creates the pdf and it then uploads it from there.
You can do one thing like below which i am doing for my application. First create a folder in your server under the root directory for example. Then change read write permissions to that folder using chmod command.
Then get all the code in $html string.
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
$file_location = $_SERVER['DOCUMENT_ROOT']."app_folder_name/pdfReports/".$pdf_name.".pdf";
file_put_contents($file_location,$pdf);
Where pdfReports is the folder which i created to save all the pdf's. You can change to your folder name.
Quoted from PHP manual :
A URL can be used as a filename with this function if the fopen
wrappers have been enabled.See fopen() for more details on how to
specify the filename. See the Supported Protocols and Wrappers for
links to information about what abilities the various wrappers have,
notes on their usage, and information on any predefined variables they
may provide.
Source: http://php.net/manual/en/function.file-put-contents.php
Is it feasible to create the PDF directly on the server instead, and let the client download it if he needs it? If so, you could use wkhtmltppdf to convert any HTML page into a PDF on the server and save it and/or stream it to the client.
http://code.google.com/p/wkhtmltopdf/
Can't you use the FTP functions of PHP ?
I'm using Filepicker.io to upload PDFs to my application. I have all those URLs and now I am trying to merge some of those PDFs using the PDF Tool Kit PHP library. It was not working for me so I ran some tests using the "file_exists" on PHP and it kept returning false.
I think this has to do with the fact that the URL does not have a ".pdf" extension at the end. This is what they look like: "https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1"
Does anyone know how I can pull the PDF using PHP in order to merge those files using the PDF Toolkit Library?
Thanks!
Alain F.
file_exists doesn't work with URLs, only with local files. Instead download the file to the temp dir using the copy command.
If the file can't be downloaded, the copy command will return false.
$exists = copy('https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1', '/tmp/example.pdf');
if (!$exists) throw new Exception("PDF could not be downloaded");
Use the downloaded file in the PDF Tool Kit.
EDIT: This does not solve this particular problem but does address the theory that it didn't work because "the URL does not have a ".pdf" extension at the end."
You can add things to the end of the filepicker URL with a trailing +
The following urls are equivalent:
https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1
https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1+name.pdf
I am having an issue when using mime types in PHP.
As a security feature, I am placing any content uploaded to my server by users in a directory outside of public_html. When a user wants to access their uploaded files in the browser, I use php script to pull the file from the secure folder using readfile and header('content-type'), the issue I am having so far only pertains to mp3s.
When I try and use this for a mp3 file, the file doesn't actually play in the browser, but M4As work fine. And it isn't every mp3 file too, it just seems that when I get the mime-type of some mp3 files php can't tell that they are mp3s. The mp3s also playback fine if I download them, so it isn't an issue with the file per say (unless the file has the wrong mime-type somehow).
How do I get it to tell that they ARE mp3s?
My getFile code is here: codepad.org
Any help would be appreciated greatly, I'll even give you some of my tracking cookies!
For mp3s, did you try:
header("Content-Type: audio/mpeg");
I am a beginner at both Actionscript3 and PHP, and I'm trying to get Henry Jone's movie clip to jpeg code to work for me, as seen here:
http://henryjones.us/articles/using-the-as3-jpeg-encoder
I downloaded the source and then uploaded the .swf and the .php in the same directory. However, when I click "download your sketch," it downloads the .php file instead of a jpeg.
The .swf I uploaded can be seen here:
http://www.snut.org/storage/scrntest/sketch.swf
I didn't change any of the code from the source I downloaded, so I can't figure our what the problem is. Can anyone help?
Rule of thumb - any time your web server downloads .php files instead of rendering the output of the file it is a good sign that your server isn't set up to handle PHP. You need to tell Apache to use PHP, google around for "install PHP on Apache web server" and that should get you started.
Basically .php files are just text files, so unless you have something telling your web server to treat .php files different from normal text files then it'll just assume the user wants to download the file that's being linked.
Is there a way to add files to a zip file from another server with php's zip extension? ie.
addFile(array('localfile.txt,'http://www.domain.com/remotefile.txt'))
//(that obviously does not work)
I suppose I can download the files to a temporal folder and then add them to the zip file, but I was looking for a more automated solution or a function already made
use file_get_contents() and ZipArchive::addFromString()
$zipArchiveInstance->addFromString($filename, file_get_contents($mediaUrl));
This writes the contents fetched remotely straight into your php object (no need to write/read temp file)
It's not hard to read remote files in PHP.
file_get_contents("http://example.com/remote.txt");
Or to copy them locally:
copy("http://example.com/remote.txt", "/tmp/local.txt");
Whichever way you do it, the contents are going to have to be transferred to a local temp folder or memory before you can do anything with them.
Fetch them with cURL, add them from TEMP directory.