0

Possible Duplicate:
How can one check to see if a remote file exists using PHP?
Check if file exists on remote machine

I'm having trouble with this code. I'm trying to search for a file in $url below, and the file does exist, but the code is returning No files located here instead. I either have the wrong syntax or I'm not able to search for a file in this manner.

Please help. Thanks in advance.

<?php

function formaction()
{

$url = "http://".$_SERVER['HTTP_HOST'] . "/" . basename(dirname (dirname (dirname (dirname
(__FILE__))))) . "/process.php"; 

$path = false;

      if (@file_exists($url))
      {
         $path = $url;
      }

else
      {
          echo "No file located here";
      }


return $path;

}

echo formaction();


?>
Community
  • 1
  • 1
Rob Myrick
  • 859
  • 11
  • 28
  • 11
    `file_exists()` doesn't work on URLs – Pekka Feb 27 '12 at 19:35
  • 1
    Perhaps this will be helpful: http://www.php.net/manual/en/function.file-exists.php#85246 – Travesty3 Feb 27 '12 at 19:36
  • HI Pekka, is there a way to call the file the correct way, and then convert that path to a URL like above? Any guidance would really help. I only need to convert this into a URL because the other way won't render correctly when I insert it into form action="". For example, the browser is getting confused when I call it this way...../home2/fortehome/richmindonline/testenvironment... – Rob Myrick Feb 27 '12 at 19:36
  • 5
    Please don't use `@` to suppress errors. It's an incredibly sloppy and lazy practice. – user229044 Feb 27 '12 at 19:41
  • Yeah, that was copied from another post. I don't even know what that does...haha. But I will remove. – Rob Myrick Feb 27 '12 at 19:42

1 Answers1

1

Since file_exists() don´t work with url´s:

You can use get_headers() to check its 404 or not go go directly to this snippet:

Taken from PHP Manual: http://www.php.net/manual/en/function.file-exists.php#85246

function url_exists($url) {
    // Version 4.x supported
    $handle   = curl_init($url);
    if (false === $handle)
    {
        return false;
    }
    curl_setopt($handle, CURLOPT_HEADER, false);
    curl_setopt($handle, CURLOPT_FAILONERROR, true);  // this works
    curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox    
    curl_setopt($handle, CURLOPT_NOBODY, true);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
    $connectable = curl_exec($handle);
    curl_close($handle);   
    return $connectable;
}

But in this case, it seens that you dont use to look from an URL.. but, looking for a local script. You should not use URL´s, but the actual absolute or relative path.

Check:

echo getcwd() . "\n";

Also, check dirname(), you´ll be able to determine the relative / absolute path this way.

Guilherme Viebig
  • 6,901
  • 3
  • 28
  • 30
  • I'm very concerned about this code because only one line is documented as `// this works`. How do I know the other lines work??? –  Feb 27 '12 at 20:32