0

I am writing a test to count the number of pages in PDFs stored in a folder on a server. I have been able to get it to work on my local machine, I am unable to get it to work on a remote file.

This is my code that works on local files:

 require_once 'C:\..\application\libraries\fpdi\fpdf.php';
 require_once 'C:\..\application\libraries\fpdi\fpdi.php';
 $pathToFile = 'C:\Users\..\Desktop\filename.pdf';
 $pdf = new FPDI();
 $pageCount = $pdf->setSourceFile($pathToFile);
 echo $pageCount;

But if I change the $pathToFile to a link on a remote server, I get an error message.

I tried this:

$pdfname = 'http://../filename.pdf';
$pdftext = file_get_contents($pdfname);
$num = preg_match_all('/\/Page\W/', $pdftext, $dummy);
echo 'Num: ' . $num;

But again, when I use a local file, it works fine, but the remote file gives me an error (failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden).

From searching online, it seems like that is a common error and I've seen code to use curl, but it makes no sense to me and I can't get it to work either. I saw code to use pdfinfo, but the link in that post is going to another site.

I don't want to have to download anything, so using something like Imagick is not an option either.

All I'm looking for is a simple page number from a file on a remote server. Any help would be much appreciated.

PortyR
  • 355
  • 1
  • 2
  • 12
  • if you get a 403 forbidden, so grant the rights on the user used by the web server on the folder of the remote server if you have access to it – Frankich May 29 '19 at 07:40
  • @Frankich, thank you. Can you point me in the direction of something that shows me how to do that? I'm assuming that if I don't have access to it, I'm out of luck? – PortyR May 29 '19 at 07:42
  • on linux you have the `chmod` command to grant access, on windows there is the `icacls` command or go on the folder -> right click -> properties and add the web user to grant access. If you don't have access to the server, either you download the file if you can, or contact the administrator to discuss about your problem if he agrees – Frankich May 29 '19 at 07:55
  • @Frankich, thank you. I think I'm going to have to go with the downloading. I appreciate your help. Could you post it as an answer, so I can accept? – PortyR May 29 '19 at 07:56

0 Answers0