Treat variable as a file

/ Published in: PHP
Save to your folder(s)

Occasionally you'll have a string and need to pass it to a function that will only take a filehandle (such as fgetcsv). Here's how to create a virtual filehandle in memory.

Copy this code and paste it in your HTML
  1. $value = "Ennie,Meenie,Minie,Mo";
  2. $fh = fopen('php://memory', 'rw');
  3. fwrite($fh, $value);
  4. rewind($fh);
  5. $array = fgetcsv($fh, 1024);
  6. fclose($fh);

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.