Pergunta

I'm trying to copy a remote file to my server. Before doing this, I want to test of the file exists. The following code does not work, even though I have manually verified that the file exists on the remote server. I have PHP's allow_url_fopen set to On. Is there something I am missing? The old procedural method file_exists() is what I'm looking to replicate.

   //...
   $fileInfo = new \SplFileInfo($imageLocation);
   if($fileInfo->isFile()) {
      echo "doesn't exist";
   } else {
      echo "exists, so copy file here";
   }
Foi útil?

Solução

There is no equivalent of file_exists() for the SplFileInfo class.

SplFileInfo::isFile() is a different check, which correlates with the is_file() function (they share underlying code).

You could call the file_exists() function on $imageLocation, provided it references a stream wrapper that supports stat() functionality. Wrappers such as file://, phar:// and ftp:// (the latter only partially) support this.

Unfortunately the http:// (and https://) wrapper does not support stat() calls. This means file_exists(), and other functions that call stat(), will return false.

Outras dicas

$fileInfo->getRealPath();

return false if don't exist

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top