Jump to content

Can PHP do something like this???


elentz

Recommended Posts

I have several hundred files that are named in a way that will not work for the way I want to use them.  We saved these files with filenames and now we want to change the names.  For example the files were named with a 4 digit number.  It was an account number for the customer that the file was associated with.  Now we use the phone number when we save the file.  So now I have several hundred files in the wrong naming convention.  I have a list of the customer phone numbers and the account codes.  What I am looking for is some way to rename the files using the list to remove the account number and replace it with the phone number.  Can someone suggest a method of accomplishing this? 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/176861-can-php-do-something-like-this/
Share on other sites

Something like this?

 

foreach(glob("/path/to/files/*") as $f) {
    $acc = substr($f,0,strrpos($f,"."));
    $tel = mysql_result(mysql_query("SELECT `tel` FROM `table` WHERE `account`='$acc' LIMIT 1"),0);
    $c = file_get_contents($f);
    $h = fopen("/path/to/files/$tel","r");
    fwrite($h,$c);
    fclose($h);
}

 

This is NOT tested!

That was quick!!

That's what my girl friend always say!

 

I would probably do this

 

<?php
$lines = file('details.csv');
foreach ($lines as $line) {
$details = explode(",",$line);
    $account = $details[1]; //second row = account 
    $phone = $details[5]; //sixth row = phone
    $path = "/accounts/files/";
    if(file_exists($path.$account) && is_file($path.$account))
    {
    	rename($path.$account.".txt", $path.$phone.".txt"); //rename account to phone
    	echo "account $account changed to $phone<br />\n";
    }else{
    	echo "<strong>account $account NOT found</strong><br />\n";
    }
}
?>

 

I assumed the CSV had account on the second row and number on the sixths

remember back and trial test

 

EDIT: this isn't perfect but should give you the basic idea!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.