PHP checks if a file exists on my server based on the full url

I have an application that creates invoices as PDF files stored in a directory on my server. Before sending out these invoices, I would like to make sure that the URL (not the server path) that I store in my database really points to a valid PDF file.

Perhaps something went wrong that wrote the value for this field in db, but, for example, the file name is missing; this means that the file is not specified in the specified directory.

AFAIK file_exists just checks server paths, but not URLs.

I could always store the server path or collect it, but I wonder if I can do this check based on the full http url ending with the file name?

EDIT: Yes, CURL also crossed my mind, but isn't it too hard for such an easy task? Maybe I should just save the server path.

+5
source share
3 answers

You can verify the request by opening the URL using PHP + cURL, as described here , and check the HTTP status code returned.

0
source

You can use the following:

$file = 'http://mysite.co.za/files/invoice.pdf';
$file_exists = (@fopen($file, "r")) ? true : false;

Worked for me when trying to check if image exists in URL

0
source


You can use the file_get_contents () or fopen () function

-2
source

All Articles