PHP to quickly rename files based on a CSV

Filed Under PHP/MySQL |

I love php/mysql. Almost all of my sites are written with this pair. I decided earlier this week that I needed to launch a new site. Basically splitting off part of an existing site into its own site. The code was already written and luckily I had kept it in my head to keep it portable, so that took approximately 20-30 minutes to move over, change some settings, test, etc. But the problem was the database, as it referenced folder names that would no longer be applicable since they were based on an auto incrementing number in the database. 228 folders to be exact. My first thought was, “No big deal, some manual renaming, it’ll take an hour or two.” I did 5 of them and said forget this. I exported the folder name and an identifying filename from the old database, then exported the same info from the new database, matched them up in Excel, killed the filename columns, and then exported a csv that contained the new foldername and the old foldername. A little php scripting and in seconds 200+ folders were renamed. Granted I had a few that had to be done manually, but only about 5 or 6. For anyone that cares, here’s the quick and dirty php code to rename based off of a csv list of newname,oldname.

<?php
$handle = fopen(”file.csv”, “r”);
while (($data = fgetcsv($handle, 1000, “,”)) !== FALSE) {
   if (rename(”$data[1]“, “$data[0]“)) {
    print “Rename: <font color=\”green\”>Success</font><br />\n”;
   } else {
    print “Rename: <font color=\”red\”>FAILED!</font><br />\n”;
   }
}
fclose($handle);
?>

1 Comment

Demographic Info From 26,000 Phished MySpace Account

Filed Under MySpace, PHP/MySQL |

So a few days ago LoLo tapped me for some quick PHP-ing to analyze a file of phished MySpace passwords that he had found. For those that don’t know what’s been going on with MySpace and the password phishing, check out this article for an explanation.

So armed with this data, I sat down and imported the file into a MySQL database, cleaning up bits and pieces, and then wrote some scripts to pull out useful data. Not content with just info on the emails and passwords, I started playing around with MySpace’s search page, and wrote a screen scraping script to grab the info of all the users by searching for their emails. It took a few hours to gather all the data, but then I was able to run some useful reports on it. I’ll let you check out Lolo’s full write-up on the thing, including the stats over on his article: Demographic Info From 26,000 Phished MySpace Accounts. If there’s any interest/demand, I’ll clean up the scripts and post the code.

Leave a Comment