简体   繁体   中英

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. using the file_exists with an array always fails: Here is a simple program I wrote that shows the failure:

[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

You might also need to wrap this in brackets because you are using two variables. so use {$testarray[$i]}

You are missing a $ before testarray in the if clause. try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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