i have subdomain sub.domain.com. the subdomain points on a directory root/sub of my root dir on my Webserver. now i have pdfs on another dir on the server root/pdf. How can i check if a specific pdf exists and if it exists i want to copy the file to a temp dir of the subdomain.

if i call a php script sub/check.php an try to check a pdf that exists :

$filename = "http://www.domain.com/pdf/1.pdf";
if (file_exists($filename)) 
{
    "exists";
} 
else 
{
    "not exists";
}

It always shows : not exists. If i take the url and put it in a browser - the pdf will be shown.

How can my php script in the /sub-folder access files in the root or root/pdf ?

bye jogi

有帮助吗?

解决方案

file_exists() function does not work that way. It does not take remote URLs.
This function is used to check the file that exists on the file system.

Check the manual here

Make use of cURL to accomplish this.

    <?php
    $ch = curl_init("https://www.google.co.in/images/srpr/logo4w.png"); //pass your pdf here

    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if($retcode==200)
     {
      echo "exists";
     }
   else
     {
      echo "not exists";
     }

    ?>

其他提示

file_exists() looks locally on the machine if a file exists. But what you are doing is using a URL.

Since you say your script is in the root folder, you need to do change

$filename = "http://www.domain.com/pdf/1.pdf";

into

$filename = realpath(dirname(__FILE__)) . "/pdf/1.pdf"; // first part gets current directory
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top