简体   繁体   English

php file_exists不适用于数组

[英]php file_exists does not work for array

I posted this question earlier but I have now used the feedback and simplified the php program to show how it still fails. 我之前发布了这个问题,但我现在已经使用了反馈并简化了php程序,以显示它仍然失败。 using the file_exists with an array always fails: Here is a simple program I wrote that shows the failure: 将file_exists与数组一起使用总是失败:这是我编写的一个简单程序,它显示了失败:

[root@dsmpp1 steve]# ls -l data
 total 4 -rw-r--r-- 1 root root 0 Sep 19 11:41 test_file 
[root@dsmpp1 steve]# cat test_file.php
`#!/usr/bin/php -q 
    <?php 
    $i=1; 
    $testarray=array(); 
    $testarray[$i]="test_file"; 
    echo "testarray $testarray[$i]\n";
     **if(file_exists("/home/steve/data/testarray[$i]")) {**
    echo "file exists\n"; } 
    else { echo "file does not exist\n"; } `    
[root@dsmpp1 steve]# php -q test_file.php 
testarray test_file 
file does not exist 
[root@dsmpp1 steve]#

I used the double quotes around the directory and file name as suggested earlier and it is still not working. 我使用了之前建议的目录和文件名周围的双引号,它仍然无法正常工作。

Shouldn't it be: 不应该是:

$testarray[$i]="test_file.php";

instead of: 代替:

$testarray[$i]="test_file";

try 尝试

if(file_exists("/home/steve/data/{$testarray[$i]}")) {**

You were missing the $ before testarray 你在testarray之前错过了$

You might also need to wrap this in brackets because you are using two variables. 您可能还需要将其包装在括号中,因为您使用的是两个变量。 so use {$testarray[$i]} 所以使用{$ testarray [$ i]}

You are missing a $ before testarray in the if clause. 在if子句中,你在testarray之前缺少$ try this: 尝试这个:

if(file_exists("/home/steve/data/".$testarray[$i])) {

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

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