0
    <?php

        function cmp($a, $b) {
            if (filemtime($a) == filemtime($b))
                return 0;

            return (filemtime($a) < filemtime($b)) ? -1 : 1;
        }


        $files = glob("/Users/xx/Desktop/2011/cdr/*.cdr");
        usort($files, "cmp");

        foreach($files as $file)

            //echo $file . "<br />";
            // echo "$file was last modified: " . date ("d-m-Y H:i:s.", filemtime($file))."\n";


    $file1 = file_get_contents($file, FILE_USE_INCLUDE_PATH);


    $arr1 = explode("\n", $file1);


    $data1 = array();



     foreach ($arr1 as $key => $value) {
 $split = explode(";", $value);

 $keys = md5(uniqid(rand(), true));
}
 print_r($data1);
?>

I am trying to get contents from each file but it keep getting below

Array ( )

Question is how can i fix the error when am trying to get contents from each individual file in a folder? Should i make a loop to read from each file separately ?

Belgarath
  • 104
  • 12
  • Are you trying to access a local file (not on server such as `localhost`) using php? I don't think it's possible, but I assume that if it is possible it should be done using `file://` protocol (e.g.: `file://C:\Users\xx\Desktop\2011\cdr\*.cdr`) –  Jul 05 '17 at 07:23
  • İ tried that too. its a directory from my computer file:///C:/Users/xxx/Desktop/2011/cdr/.I tried it too but still get the error – Belgarath Jul 05 '17 at 07:23
  • Then it's not possible. Work with some framework (such as wampserver or xampp) and run `localhost` on your system, then put the contents from this file inside your server and try to read it –  Jul 05 '17 at 07:25
  • If you want to access local DIR/File directly - no. This is not how PHP works. But after uploading to a server you can manipulate. – Isanka Wijerathne Jul 05 '17 at 07:25
  • Possible duplicate of [PHP file\_get\_contents() returns "failed to open stream: HTTP request failed!"](https://stackoverflow.com/questions/697472/php-file-get-contents-returns-failed-to-open-stream-http-request-failed) – LF00 Jul 05 '17 at 07:33
  • @ Kris Roofe errors might be similar but its not a duplicate – Belgarath Jul 05 '17 at 07:35

1 Answers1

1
$files = glob("/Users/xx/Desktop/2011/cdr/*.cdr");

This call to glob() will return an array of absolute paths like:

"/Users/xx/Desktop/2011/cdr/example.cdr"

The path is part of the returned value! (This is unlike some other functions like readdir().) You don't need to append the path again when you call file_get_contents().