PHP checks if a file exists on a different domain name

Source: Internet
Author: User
PHP Check if the file exists under different domain names

Earlier today I needed to the find out if a file exists on a different domain. Initially I used the file_exists function, but then if that threw back a error I remembered that file_exists only check s whether a file or directory exists on the same server as the script.

After I played around with various functions, I came up with a few lines of code that actually works:

How to check if file exists on a different domain

 
  
?

The logic explained

OK, so if the file exists (1.jpg) The fopen function would throw a "resource ID" response. So I check the response to see if "Response ID" exists with the Strpos function. It ' s really as simple as that.

I ' m not entirely sure if my method was the best, nor the most efficient, but it seems to work pretty well, and I can ' t thin K of any other methods. Anyone know of any other/better methods?

Better Solution

Thanks to a comment left by Paul I ' ve been made aware of a better solution.

?

$url = "http://www.example.com/index.php"; $header _response = Get_headers ($url, 1); if (Strpos ($header _response[0], " 404 ")!== false) {  //file DOES not EXIST} else {  //file exists!!}

?

?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.