-1

I'm wondering why file_get_contents() is not taking my variable (contains a linux path). This is what I'm doing:

    find $PWD -name "*.png" > myList

Then I use my php script:

    $list_of_files = file('myList');

So far so good. But when I do

    echo file_get_contents($list_of_files[2]);
    echo $list_of_files[2];

I get a Warning that file_get_contents cannot find specified file. While the 2nd echo prints the path just fine.

Totally confused why its doing this. Any insights would be greatly appreciated.

Thanks.

errorprone
  • 421
  • 7
  • 10

4 Answers4

1

First var_dump($list_of_files) to see what you have, I suspect a list of file names only, ie not an absolute path. So unless run from the same folder, php won't find the files.

In any case you're probably better off using glob which will return an array of file names directly from within php

Here's an example of using glob recursively PHP get file listing including sub directories

Community
  • 1
  • 1
andrew
  • 9,313
  • 7
  • 30
  • 61
  • var_dump($list_of_files): These are the correct pathnames. i.e. I have a folder _1_ which contains another folder _11_. `array(5) { [0]=> string(24) "./1/11/alksdjflkdjf.png " [1]=> string(13) "./1/whoo.png " [2]=> string(17) "./2/alkjdfjj.png " [3]=> string(16) "./3/sfkkkks.png " [4]=> string(12) "./large.png " }` – errorprone May 13 '14 at 18:22
  • `./` is relative to the current folder, it should be ok provided that the shell command is run from the folder as the php script. As stated better to use glob, you can still use it to return `*.png` – andrew May 13 '14 at 18:31
0

You have the path, but you cannot get the contents of the file. This could be because you do not have the permission to do so or the file does not exist.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • I have executed chmod 777 on the files in that dir. It still gives me error Warning: file_get_contents(./1/11/alksdjflkdjf.png ): failed to open stream: No such file or directory in – errorprone May 13 '14 at 18:25
0

Make sure what's paths are relative to folder where you want to read file content:

Just before using file_get_contents add:

 echo getcwd();

and now check if files (with path) exist in that folder

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
0

This was stupid of me but i found out that I wasn't trimming the newlines :(

So basically a

    foreach($list_of_files as $i){ $j = trim($i); echo file_get_contents($j);}
errorprone
  • 421
  • 7
  • 10