I need help with reading a csv file from a folder. The file is sent to the folder via ftp after every 10minutes. The script I want should then read this file almost at the same interval. The file name is dynamic so there is a possibility that these files may accumulate over time. I have thought of deleting the file once it has been read from so that there is only a single file at any given moment.

Your help will be highly appreciated.

7 Years
Discussion Span
Last Post by digital-ether

A simple way to read files in a directory is to use the glob() function.


$files = glob('/path/to/directory/*');

You can use filemtime() to get the modification time of the file, so the latest file would be what you are after.
You can use is_file() and is_dir() to check if the path is a file or directory.

As for keeping the directory from filling up with files, I believe the best choice is to create a cron job to clean out the directory every now and then. You can use a simple shell script:


find /path/to/dir/ -type f -mtime +1 -exec rm -f "{}" \;

This is a bash shell script, that when executed will delete any files in /path/to/dir/ that is older then 1 day.
See http://linux.about.com/od/commands/l/blcmdl1_find.htm for documentation on the find command.

You could also write the shell script in PHP using the same functions I mentioned (glob(), filemtime() etc.). Also unlink() will delete files in PHP. If you use a PHP shell script, you need to prepend the PHP executable path script (called the shebang).



The actual location depends on your installation of PHP. (tip: usually you can get the location with the command "whereis php")

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.