开发者

PHP check if file exists on my server, based on a full URL

开发者 https://www.devze.com 2022-12-22 23:00 出处:网络
I have an app that generates invoices in the form of PDF files stored in a directory on my server. Before mailing out this invoices I\'d like to make sure that the URL (not server path) which I store

I have an app that generates invoices in the form of PDF files stored in a directory on my server. Before mailing out this invoices I'd like to make sure that the URL (not server path) which I store in开发者_如何学运维 my DB does indeed point to a valid PDF file.

Something could have gone wrong that wrote a value to that field in the db, but for example the filename is missing; meaning the file is not in the specified directory.

AFAIK file_exists just checks server paths but not URL's.

I could always store the server path too, or assemble it, but I'm wondering if i can do this check based on a full http url ending with a filename?

EDIT: yeah CURL occurred to me too but doesn't that seem a bit heavy for such a simple task? Perhaps I should just store the server path too.


You could validate the request by opening the URL using PHP+cURL as described here and check the HTTP status code that's returned.


You could 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 an image exists on the URL



You can use function file_get_contents() or fopen()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号