문제

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";
   }
도움이 되었습니까?

해결책

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.

다른 팁

$fileInfo->getRealPath();

return false if don't exist

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top