Home > Blockchain >  file_get_contents() returns "failed to open stream: File name too long", but the url opens
file_get_contents() returns "failed to open stream: File name too long", but the url opens

Time:04-13

I have some file_get_contents() in my code like this:

$thearray = file_get_contents('../folder/subfolder/sorting.php?arr='.json_encode($recordsarr));

where $recordsarr is a multiarray. i got an error like:

file_get_contents(../folder/subfolder/sorting.php?arr=[["463","0","68","68","0,1","68","68","0","7.50"],["463","0","61","68","2","68","68","0","8.00"],["463","0","68","68","0,1","68","68","0","8.50"],["463","0","68","68","","68,"68","0","9.00"],["463","0","68","68","","68","68","0","10.00"],["463","0","68","68","","68","01:15:00","0","6.00"]]): failed to open stream: File name too long

At the same time, if i will open from browser this url (https://example.com/folder/subfolder/sorting.php?arr=[["463","0","68","68","0,1","68","68","0","7.50"],["463","0","61","68","2","68","68","0","8.00"],["463","0","68","68","0,1","68","68","0","8.50"],["463","0","68","68","","68,"68","0","9.00"],["463","0","68","68","","68","68","0","10.00"],["463","0","68","68","","68","01:15:00","0","6.00"]]) works fine and it shows me some content. I have read about 255 characters limitation for the file_get_contents() but is there any workaround with this situation? I can't make these arrays smaller in size. I am thinking also about CURL, but i cannot find a working example of using CURL inside existing site (and even if it is possible, it will make some outgoing traffic and increase script running time and make more load), not outside. Maybe there is a function in php, that is the same as file_get_contents(), but that allows to use longer url's?

CodePudding user response:

It looks to me like you're hoping to run the php program in ../folder/subfolder/sorting.php while passing it your JSON as the arr parameter. You're hoping to get the output of that program.

But you're passing file_get_contents() a file name, not a URL. It will work if you pass it a URL. It will make an HTTP or HTTPS request to the server in question and return the results. But only if it can tell you're using a URL. Give a URL, something like https://example.com/folder/subfolder/sorting.php?arr=whatever and it may work.

Or, as you mentioned, use CURL.

Or, as @RiggsFolly suggested, refactor that sorting.php code to work as a function, and just call it.

Your filename-length failure comes from your operating system when get_file_contents() asks it to open that long file name you passed. If it were obviously a URL, get_file_contents() would not have tried to get your OS to open it as a file.

CodePudding user response:

The length of your url is ok ~ 344 characters. Then you can send your query like that:

$url = http_build_query(['arr' => '[["463","0","68","68","0,1","68","68","0","7.50"],["463","0","61","68","2","68","68","0","8.00"],["463","0","68","68","0,1","68","68","0","8.50"],["463","0","68","68","","68,"68","0","9.00"],["463","0","68","68","","68","68","0","10.00"],["463","0","68","68","","68","01:15:00","0","6.00"]]',]);

$opts = ['http' =>
   [
    'method'  => 'GET',
    'content' => $url,
]];

$context  = stream_context_create($opts);

$res = file_get_contents('http://example.com/send.php', false, $context);

CodePudding user response:

Push the full URL and it will work:

$thearray = file_get_contents('https://example.com/folder/subfolder/sorting.php?arr='.json_encode($recordsarr));

The traffic will be very low

  • Related