I am working to create a bookmarklet that allows a user to navigate to a PDF document on a remote server, and then save it to their account on my website by clicking the bookmarklet while viewing the remote PDF. The bookmarklet redirects them to a (codeigniter) page on our server, passing over the PDF URL, which allows them to confirm they want the PDF, and then it gets saved. This is all working properly if they are viewing a URL ending in ".pdf", but with one problem... it seems some sites obscure the path of the PDF, by using an HTML embed, without ever displaying the actual path of the PDF, for example:
If this is the case, my transfer function won't work, it just creates a blank file on the server.
Is there a way to get around this so that the actual PDF will transfer over?
My transfer code, if it's relevant:
function transfer () {
$url = $this->session->userdata('url');
if (!$url) die();
$destination_folder = 'uploads/';
$newfname = $destination_folder . basename($url);
$file = fopen ($url, "rb");
if ($file) {
$newf = fopen ($newfname, "wb");
if ($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
}
Can you try this:
function transfer () {
$url = $this->session->userdata('url');
if (!$url) die();
$destination_folder = 'uploads/';
if(stripos($url,".pdf")===false)
$newfname = $destination_folder . basename($url).".pdf";
else
$newfname = $destination_folder . basename($url);
$pdf = file_get_contents($url):
file_put_contents($newfname, $pdf);
}
Edit, a small update. I think the error can be something with basename.