php file_exists不适用于数组

时间:2012-09-19 16:01:19

标签: php

我之前发布过这个问题,但我现在已经使用了反馈并简化了php程序,以显示它仍然失败。 将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]#

我在前面建议的目录和文件名周围使用了双引号 它仍然无法正常工作。

3 个答案:

答案 0 :(得分:1)

不应该是:

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

而不是:

$testarray[$i]="test_file";

答案 1 :(得分:1)

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

你在testarray之前错过了$

您可能还需要将其包装在括号中,因为您使用的是两个变量。所以使用{$ testarray [$ i]}

答案 2 :(得分:0)

您在if子句中的testarray之前缺少$。试试这个:

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