CakePHP read PDF generated and send it as bytes - php

In my webapp, I can generate pdf file from multiple odt files using gedooo on demand when the user go to this url: http://{base_url}/models/generer/{doc_id}, the modelsController generate the pdf and launch the download on the navigator.
Now I have to send to a distant application by webservice the content of pdf (like an attachment in email) from a model. The problem is to execute the pdf generation and integrate it in my request without store it on the server as file. I tried to do something like:
$pdf = new File('http://{base_url}/models/generer/{doc_id}');
$content = $pdf->read();
And I get an error. I am out of ideas, I need your help.

You can not transmit PDF (binary file).
if you want to transmit it in webservice, you have to convert it to base64
$file = file_get_contents('http://{base_url}/models/generer/{doc_id}');
$filedata = base64_encode($file);
now you can send this data in webservice
echo json_encode(array('file'=>$filedata));

I found the solution!
$this->requestAction($url);
that write the file in the folder of my webapp and
$content = file_get_contents($local_url)

Related

PHP: save a file (generated by aspx) to server

I have an url provided by a wholesaler. Url generates xml file which I need to save on my server.
I use PHP - file_get_contents and file_put_contents to do that:
$savepath = "path_to_my_server_folder";
$xmlurl = "http://usistema.eurodigital.lt/newxml/xmlfile.aspx?xml=labas&code=052048048048051057049050048049052";
file_put_contents($savepath.'eurodigital.xml', file_get_contents($xmlurl));
File is generated on my server, but its content is empty. I have no problems with other xml files if I provide direct xml url, but in this situation file is generated by aspx file dynamically. Xml url is actual url I use. When I open xmlurl in browser, xml file gets saved to device.
Can you help me with moving xml to my server? What function should I use? Is it even possible to do that using PHP 5? "allow_url_fopen" is ON.
Thank you in advance!

loading document with error when open with pdf js

I have a strange question regarding PDF.JS and PHP
Using PDF.JS, to open pdf you should use on browser:
http:// ...url... /viewer.html?file=[filename]
I have changed viewer.html extension to viewer.php and developed an additional file document.php to get data from database and load a local PDF file, and I use in this way to get PDF
... viewer.php?file=document.php&control=5E71581C52B96
All work great , AS ESPECTED, but i'm experiencing trouble when get variables from database.
When I Use fileurl like this, PDF load correctly:
$fileurl = 'D:\Drive\_DEV\storage\220\2020-03-17\00000000002\1584486427900.pdf';
When I use fileurl like this (variables from database) PDF not open and have error
Corrupted or inválid PDF Invalid PDF structure
$fileurl = $storage_path.'\\'.$storage_folder.'\\'.$document;
If I echo, $fileurl in both cases i have exactly same result.
Regarding SQL QUERY below, on WHERE Clause if I change '$doc_control' (from request) with '5E71581C52B96' (instead '$doc_control') PDF load correctly into pdf.js
If I echo $doc_control, number is exactly same, only difference is how put value on WHERE (number or variable)
If i open document.php work with no problems.
What do I Wrong? Any help is very appreciated.
DOCUMENT.PHP
// REQUEST
$doc_control = $_REQUEST['control'];
// Read database
SELECT *
FROM docs
WHERE doc_control = '$doc_control'
// FILE PATH
$fileurl = $storage_path.'\\'.$storage_folder.'\\'.$document;
// READ PDF
header("Content-type:application/pdf");
header("Content-Disposition:inline;filename=".$fileurl);
//#readfile($fileurl);
$file=fopen($fileurl, "r") or die('Unable to open file');
echo fread($file,filesize($fileurl));
fclose($file);

PHP Amazon S3 access private files through URL

I'm using AWS PHP sdk to save images on S3. Files are saved privately. Then, I'm showing the image thumbnails using the S3 file url in my web application but since the files are private so the images are displayed as corrupt.
When the user clicks on the name of file, a modal is opened to show the file in larger size but file is displayed as corrupt there as well due to the same issue.
Now, I know that there are two ways to make this working. 1. Make the files public. 2. Generate pre-signed urls for files. But I cannot go with any of these two options due to the requirements of my project.
My question is that is there any third way to resolve this issue?
I'd highly advise against this, but you could create a script on your own server that pulls the image via the API, caches it and serves. You can then restrict access however you like without making the images public.
Example pass through script:
$headers = get_headers($realpath); // Real path being where ever the file really is
foreach($headers as $header) {
header($header);
}
$filename = $version->getFilename();
// These lines if it's a download you want to do
// header('Content-Description: File Transfer');
// header("Content-Disposition: attachment; filename={$filename}");
$file = fopen($realpath, 'r');
fpassthru($file);
fclose($file);
exit;
This will barely "touch the sides" and shouldn't delay the appearance of your files too much, but t's still going to take some resources and bandwidth.
You will need to access the files through a script on your server. That script will do some kind of authentication to make sure the request is valid and you want them to see the file. Then fetch the file from S3 using a valid IAM profile that can access the private files. Output the file
Instead of requesting the file from S3 request it from
http://www.yourdomain.com/fetchimages.php?key=8498439834
Then here is some pseudocode in fetchimages.php
<?php
//if authorized to get this image
$key=$_GET['key'];
//validate key is the proper format
//get s3 url from a database based on the $key
//connect to s3 securely and read the file from s3
//output the file
?>
as far as i know you could try to make your S3 bucket a "web server" like this but then you would probably "Make the files public".Then if you have some kind of logic to restrict the access you could create a bucket policy

How do I directly upload a file stream to Flickr using a CURL POST?

I'm writing a web app that at one point allows a user to upload a photo to a flickr account (mine). I want to do this without saving the intermediate image on the server my web app is on.
What I've got so far is a page which implements phpFlickr and accepts a POST from a simple html form. I use $_FILES['file']['tmp_name'] as the path for phpFlickr to use. Here's the code:
<?php
require_once("phpFlickr.php");
$f = new phpFlickr("apikey", "secret", true);
$_SESSION['phpFlickr_auth_redirect'] = "post_upload.php";
$myPerms = $f->auth("write");
$token = $f->auth_checkToken();
$phid = $f->sync_upload($_FILES['file']['tmp_name']);
echo "Uploading Photo..." . $phid;
?>
I'm guessing that the tmp file is being lost because of the redirect that happens when $f->auth("write") is called, but I don't know. Is there a way to preserve it? Is there any way to do this without saving the file to the server?
Answer: There is No way to directly upload a file to Flickr without saving it as an intermediate file.
I've moved on to using move_uploaded_file() followed by a flickr API call, and its working perfectly.
I've also managed to get it to play nice with the excellent Jquery Uploadify, which lets me send multiple files to it in one go.

How to save a snapshot of a SWF to the server with AS3?

I am facing the task of having to upload a snapshot to the server. But I don't want the user to download the image to their computer.
I have explored a few solutions of generating an image serverside with PHP, but they all seem to use a method where the server sends the image to the user.
See for instance: http://mattkenefick.com/blog/2008/11/06/saving-jpegs-with-flash/
I'm wondering if it's possible to save $GLOBALS["HTTP_RAW_POST_DATA"], which in that example contains the ByteArray sent by Flash, to the server as an image file....
Use php code that is along these lines to save the contents of $GLOBALS["HTTP_RAW_POST_DATA"]
// untested code
$imageBytes = $GLOBALS["HTTP_RAW_POST_DATA"]
// in real code you better create a new file for every upload :-)
$file = fopen("uploads/test.jpg", "w");
if(!fwrite($file, $imageBytes)){
return "Error writing to file: $file";
}
fclose($file);

Categories