简体   繁体   English

php file_exists() 只能在同一个 function 中工作一次

[英]php file_exists() only works once in the same function

I have a php function that renames two separate image files from a temporary to permanent path after first confirming that the temporary path exists.我有一个 php function 在首先确认临时路径存在后将两个单独的图像文件从临时路径重命名为永久路径。

When it checks for the fist file it works fine but, for some reason, the second file never passes the if(file_exists()) even though I can confirm with 100% certainty that the file path being checked does, in fact, exist.当它检查第一个文件时它工作正常,但由于某种原因,第二个文件永远不会通过 if(file_exists()) 即使我可以 100% 确定所检查的文件路径确实存在。

The image files have different names but the codes are otherwise structured exactly the same so I can't see why one would work and the other wouldn't.图像文件有不同的名称,但代码的结构完全相同,所以我不明白为什么一个可以工作而另一个不能工作。

if(file_exists('temp/'.strtolower($option['image1']))){
   $path1 = 'images/'.strtolower($option['image1']); // upload directory
   $tmp1 = 'temp/'.strtolower($option['image1']);
   if(rename($tmp1, $path1)){
      $error = 0;
   }else{
      $error = 4;
   }
}
if(file_exists('temp/'.strtolower($option['image2']))){
   $path2 = 'images/'.strtolower($option['image2']); // upload directory
   $tmp2 = 'temp/'.strtolower($option['image2']); 
   if(rename($tmp2, $path2)){
     $error = 0;
   }else{
     $error = 5;
   }
}

Is there an issue with calling file_exists() twice?两次调用 file_exists() 有问题吗? How else can I check for both paths?我还能如何检查两条路径?

The uploaded file names can have uppercase characters.上传的文件名可以包含大写字符。 If you use strtolower in the file_exists function, you probably wouldn't be looking for the original file path.如果您在file_exists function 中使用strtolower ,您可能不会寻找原始文件路径。

if(file_exists('temp/' . strtolower($option['image']))){
    // ...
}

Should be changed to:应改为:

if(file_exists('temp/' . $option['image'])){
    // ...
}

The only two possibilities (if you're absolutely sure the file path exists) I'm seeing are either 1.) a stat cache problem (you can clear the cache with clearstatcache ) or 2.) a permission issue.我看到的唯一两种可能性(如果您绝对确定文件路径存在)是 1.)统计缓存问题(您可以使用clearstatcache清除缓存)或 2.)权限问题。 Consider this:考虑一下:

$ touch /tmp/locked/file
$ php is_file_test.php
$ bool(true)
$ chmod -x /tmp/locked
$ php is_file_test.php
$ bool(false)

So it might be, that the parent directory of that file doesn't have the x (executable) permission bit set.因此,该文件的父目录可能没有设置x (可执行)权限位。 This prevents any process from iterating and accessing the directory's content.这可以防止任何进程迭代和访问目录的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM