0

I want to read the file with the longest file name in a folder called "json".

This is my PHP for that: (inside file "open.php")

<?php
// Tell PHP that we're using UTF-8 strings until the end of the script
mb_internal_encoding("UTF-8");
// Tell PHP that we'll be outputting UTF-8 to the browser
mb_http_output("UTF-8");

$files = scandir( __DIR__ . '/json', SCANDIR_SORT_DESCENDING);
$newest_file = $files[0];
readfile($newest_file);
//$output = file_get_contents($newest_file);
//echo json_encode($output, JSON_HEX_TAG); 
?>

The "json" folder is in the same directory as "open.php". When I run this on my server, I get as a response false (or a HTTP 500 error).

When I run this on XAMPP, I get: Warning: readfile(thisone.json): failed to open stream: No such file or directory in C:\xampp\htdocs\test\open.php on line 15.
I don't think it's an issue with permissions since I'm on Win 7. I checked both folder and file permissions and all users can "read".

Question: Why is PHP failing to open the file? It correctly finds the file I want, but right afterwards tells me there is "no such file".


UPDATE:
Running the following code:

// Tell PHP that we're using UTF-8 strings until the end of the script
mb_internal_encoding("UTF-8");
// Tell PHP that we'll be outputting UTF-8 to the browser
mb_http_output("UTF-8");

$files = scandir( __DIR__ . '\\json', SCANDIR_SORT_DESCENDING);
print_r($files);
$newest_file = $files[0];
print_r($newest_file);
readfile('/json/'.$newest_file); // corrected this, as @Jeff pointed out

I get as output:

Array ( [0] => thisone.json [1] => .. [2] => . ) thisone.json
Warning: readfile(/json/thisone.json): failed to open stream: No such file or directory in C:\xampp\htdocs\test\open.php on line 18


Related SO question: PHP - Failed to open stream : No such file or directory
flen
  • 1,905
  • 1
  • 21
  • 44
  • because scandir returns and array with the _filenames_ - without the folder name. So `readfile('/json/'.$newest_file);` should be good. ....and the error message actually says exactly that: `No such file or directory in C:\xampp\htdocs\test\open.php`, where you actually intended to search in `../test/json` – Jeff Dec 10 '17 at 00:45
  • @Jeff Thanks for the comment! I just made the alteration you suggested, but I got the same error as result – flen Dec 10 '17 at 00:50
  • the error message is still the same (without the correct folder), so it might need to be `'\json\'.$newest_file`? ...or `'json\'`? – Jeff Dec 10 '17 at 01:02
  • 1
    @Jeff Amazing!! Yes, that worked! But I used `'json\\'.` instead – flen Dec 10 '17 at 01:05
  • 1
    @Jeff would you like to put it as an answer so I can pick it and close the question? – flen Dec 10 '17 at 01:08

2 Answers2

0

it is not related to the directory path, because in your state the path is probably correct. it depends on the file you are to read . since you are try to read a json file you get this message but if you put any other file it will be read . so you need to parse the json while reading like this

    $path = 'json/';
$files = scandir($path);
foreach ($files as $newest_file) {
    if ($newest_file === '.' or $newest_file === '..') continue;
    $data = json_decode(readfile($path.'/'.$newest_file));
    var_dump($data);
}
samehanwar
  • 3,280
  • 2
  • 23
  • 26
  • a json file is still a simple text file. – Jeff Dec 10 '17 at 01:00
  • Unfortunately, this still doesn't work, gets me the error: `Warning: readfile(): Filename cannot be empty in C:\xampp\htdocs\test\open.php on line 19` . But I think @Jeff nailed it – flen Dec 10 '17 at 01:06
0

scandir returns and array with the filenames - without the folder name.

But here readfile($newest_file); you didn't include the sub-folder (before your edit)

So include the desired folder in your path:

readfile('json\\'.$newest_file); 
Jeff
  • 6,895
  • 1
  • 15
  • 33