Hi,

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.

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

Example:

$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:

eg:

#!/bin/bash
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).

eg:

#/bin/php

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.