using fgetcsv from a file on a FTP server

I need to read a list of CSV files from an FTP and delete them after I successfully read them.

Until now, i opened the csv file using fopen into a resource and then used fgetcsv to read the csv lines from it.

  $res = fopen($url);
  while ($csv_row = fgetcsv($res, null, self::DELIMITER)) {
  .....
  }

The problem is that I need to read a list of csv files and delete them too. the ftp_get function save the file into a local file. I rather avoid that. any way I can keep using the fgetcsv function with the ftp_nlist & ftp_connect functions? ?

Answers


You can save the csv file to a temporary file stream using ftp_fget(). This allows you to avoid the "create-read-delete" cycle. Once you close the file stream it's like it magically never existed :)

$ftp_handle = ftp_connect($ftp_server);
$remote_path = "/path/to/file.csv";
$tmp_handle = fopen('php://temp', 'r+');

if (ftp_fget($ftp_handle, $tmp_handle, $remote_path, FTP_ASCII)) {
  rewind($tmp_handle);
  while ($csv_row = fgetcsv($tmp_handle)) {
    // do stuff
  }
}
fclose($tmp_handle);

If you wanted to loop over a directory of files just get the list of files and then put the above code in a loop.


Need Your Help

Pretty-Printing 2D List in Haskell (Explanation of Failure)

haskell io pretty-print

Answers exist for how to pretty-print in Haskell, including how to do it with packages, but this is more a question about why my first approach failed.

How does jQuery FadeIn know the used display mode?

javascript jquery html animation

Simple question: How does jQuery know what display mode an element should use when using fadeIn?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.