PHP to rename a file whos name changes

Hi all,

Complete newbie here with little PHP knowledge… so please be gentle :wink:

OK… I have a website which suppliers upload stocklists to via FTP. A cron job runs hourly on my Ubuntu server to import the datafiles into a MySQL database - which is then presented in a PHP frontend for users to compare pricing. This all works seamlessly and is completely automated.

Here is the twist - one supplier uploads a stock file whos name changes every day - it always starts with their company name but is appended by the date. So as an example the files I receive are;
company 1st July 2019.csv
company 2nd July 2019.csv
company 3rd July 2019.csv
etc.

My import script is obviously needs to know the name of the file to import into the databse. So I can think of two ways of fixing this;

  1. Change the name of the file to the same thing each day
  2. Truncate the filename at a certain length and add the extension on the end (eg truncating at 7 would become company.csv every time).

Incidentally the stock files are uploaded into a directory per supplier, eg;
/var/www/html/FTP/companyA
/var/www/html/FTP/companyB
/var/www/html/FTP/companyC
etc

I know that this probably isnt hard - but can anyone help with a solution?

Thanks in advance.

you can read what files are in each directory and just import all of them. I suggest moving the files afterwards to a “processed” or “imported” directory if needed

That’s what variables are for. It isn’t hard to replicate the filename. However, you should be doing what Jim said, and moving processed files out of that directory and importing whatever goes in.

Hi Jim - thanks for replying… the system is built and been running for several years so its only this one supplier thats causing an issue.

There is only 1 file in each supplier directory. Usually the supplier uploads a file called exactly the same name every day… so my script simply truncates the database and imports each file again - that way if a supplier has uploaded a fresh data file it has over written their old file and when the import script runs again (LOAD DATA INFILE) it imports their new stocklist. This works perfectly for all my suppliers (33 at the last count) as their files are always called the same name… e.g. stocklist.csv, fredsdata.csv, etc.

This one new supplier uploads a single file which is always called “company”+the date. After it is imported it can be deleted. I dont need to keep the old files, or moving them to a “processed” directory… I was hoping there was a simple way to just truncate the name of this one file so my import script can always import a file with the same name (e.g. company.csv).

If there is no easy way of doing this I’ll just have to keep remembering to log into the server via SSL and rename this one file every day… it would just have been nice to automate it.

Thanks for the reply though.

Thanks astonecipher

I have just replied to Jim explaining in more detail how my system works… I dont need to move or keep files once the import is run. Also as the system has been working for a few years I dont really want to re-write the whole thing from the ground up.

If its not possible to just rename this one file whos name changes daily then I’ll just have to keep remembering to login and manually rename it so the system can deal with it.

Thanks for the reply

It is possible, and if you are deleting the file there is no need to move it, it’s just typical that you would keep the file around for a period in case there are issues.

$date = new DateTime();
if( !rename("company {$date->format('jS F Y')}.csv", "company.csv"))
    echo "Issue changing filename";

Thanks - that certainly works… but I forgot to mention that they inserted a £ symbol in the end of the file name which caused major issues… in the end I used;

shell_exec( “mv /var/www/html/datafiles/FTP/company/*.csv /var/www/html/datafiles/FTP/company/stocklist.csv” );

Just in case this helps anyone else :slight_smile:

Thanks for all the pointers

Sponsor our Newsletter | Privacy Policy | Terms of Service