0

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.

2
  • Thank you everyone for commenting. Here is a simple program anyone can try and you will get the same results I am getting. Every /etc/hosts file has a "localhost" entry for ip 127.0.0.1. Try populating an array grepping on the IP. The file_exists will not work. Then try hard coding it and see the results. Why when I poplulate an array using a command that returns the same value does the file_exists not work. Please try the following with and without the comment on each of the assignments. Commented Sep 19, 2012 at 20:00
  • #!/usr/bin/php -q <?php $i=1; $hostname=array(); $hostname[$i]=grep 127.0.0.1 /etc/hosts |awk '{print $3}'; #$hostname[$i]="localhost"; echo "hostname[$i] = $hostname[$i]\n"; if(file_exists("/home/steve/data/$hostname[$i]")) { echo "do nothing appserver file exists\n"; } else { echo "No file exists create one\n"; $touch=touch /home/steve/data/$hostname[$i]; } ?> Commented Sep 19, 2012 at 20:01

3 Answers 3

1

Shouldn't it be:

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

instead of:

$testarray[$i]="test_file";
Sign up to request clarification or add additional context in comments.

2 Comments

no i am not testing for the php script file, I am testing for the file in /home/steve/data/test_file.
The op showed output of ls command showing that the file was named "test_file"
1

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]}

1 Comment

Yes thank you and after adding the $ it did not make a difference. For some reason it is how the array is getting populated. When hard coding the name in the file exists it works fine. As far as I can see the array value has to have something different when I populate it using the following $appserver[$i]=grep $ip[2] /etc/hosts |awk '{print $2}'; If I assign the same name to appserver[$i] by actually assigning the value the file_exists works.
0

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

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

5 Comments

well, does the file actually exist? and what are the ** in front of the if?
what happens when you hard code the file name in the if condition?
First yes I realized I did not have the $ in front of the testarray.
However when I added the $ it still made no difference. Hard coding the name does work. I do not know why when I populate the array with $appserver[$i]=grep $ip[2] /etc/hosts |awk '{print $2}'; it does not work. Both hardcoding and the above code both yield the same name.
So I have determined it is not the double quotes, or a . before the variable or anything else. It is how the array is being populated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.