Category
How to bulk transfer many files over FTP using PHP
Have you ever wanted to transfer/upload many files using FTP, and sat there for hours watching the process and hoping that your FTP connection doesn’t time out?? Well, I have. This especially happens when I am uploading images, or a CMS. Well, I learned a little trick from Bo over at the Web Development Forums on how to use zipped files and PHP to make the file transfer process less painful.
This is how you do it:
Step 1: Zip everything you want copied.
Step 2: Upload the Zip file.
Step 3: Create a PHP file in the same directory called “unzip.php†and put this code in it:
Step 4: Run the PHP file in a browser (you should see the unzip output).
Step 5: Remove the zip file and the ‘unzip.php’ file from your server.
Step 6: Rejoice that you just saved yourself a hour of waiting!
Isn’t that nice?
For more tricks like this and great advice, make some time to visit the Web Development Forums for more.
PHP and MySQL for Dynamic Websites
The other day I was browsing Amazon.com trying to find a good PHP/MySQL book for my easily distracted mind to focus on. I have been making starts and stops in my PHP journey for some time now mostly because I was busy with school, but also because I couldn’t seem to find a good resource to help me get started and moving fast. The book that I had contemplated using before when I started working on improving my PHP skills fell short because it covered outdated versions of PHP and MySQL so I was constantly running into all kinds of errors.
Well, I think I finally solved my problem! I think I finally found the book that I’ve been looking for:
Being the geek I am, I never buy a book or a gadget without reading as many reviews as I can find, and PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition) (Visual QuickPro Guide) has excellent reviews at Amazon.com, and I definitely highly recommend it.
By the way, the second edition covers PHP 5 and MySQL 4.1, so you’re pretty up to date, and Appendix A guides you through the installation process. I’m excited to start working through this book!!
Venturing into flat-file Content Management Systems
Life has been busy with school and all taking up an inordinate amount of my time. However, I have also been playing around with some small coding projects. I have some big ideas but they’re going to need a lot of time to build. I realized that to get these projects off the ground I’ll need to really buckle down and work on my PHP coding skills, so these projects will have to wait till next year.
Apart from that, I’ve been working on some small website projects using PHP, and one thing I’ve been playing with is the concept of flat-file content management systems. These are content management systems that don’t use a database to store information, but instead, the information is stored in text files. If you have worked with any of the database-based content management systems before, such as Joomla, Mambo, or MODX, you know that they are pretty heavy duty, and sometimes resource hungry because of all the utility and functionality that the try to provide.
Flat file CMS’s are ideal for smaller websites that don’t require loads of information to be pulled from many different places at the same time. Since I am just starting to explore these kinds of systems, all I want to do in this post is introduce the concept to you and explain why I think it’s such an attractive option. Here are advantages, I am finding, of using a flat-file CMS:
- I don’t need a database - This is useful because one of the hosting accounts I use offers only a limited number of mySQL databases, but unlimited add-on domains. So I can reserve the mySQL databases for my bigger websites and use flat-file CMSs for my smaller websites. With today’s ever-expanding hosting solutions, this might not be an issue for most people, but it’s definitely an advantage to keep in mind.
- Installation is easy - Flat-file CMSs are a breeze to install and setup, since all the configuration information is in easily accessible files.
- Updates are easy -Unlike most major CMSs where updates involve going back and figuring out what to do with all your plugins, and what files had you edited where, updating flat-file CMSs is a breeze since changes are usually to specific files and are easy to implement. And because you’re not working with databases, updates are an easy DIY task.
- Editing and tweaking is easy - Most of the flat-file CMSs that I have had a chance to glance at are written in PHP, so they are easy to figure out and tweak. As I mentioned in a previous post, playing with CMSs is a great way to master PHP.
There are a number of flat-file CMSs out there and I will be talking about them in future posts. The one that I am currently dabbling with is NanoCMS, a nice little flat-file CMS that is proving to be quite useful and easy to play with, so you may want to check it out if you need to whip up a quick website without much ado.
Converting csv to sql using php
In previous posts we’ve discussed getting your csv files into sql format using different methods. I walked you through the process from csv to sql using HeidiSQL and PHPMyAdmin. I then did a follow up on how to import a large csv file into a MySQL database using the MySQL command prompt. In all these instances, what we were essentially doing was creating queries, just in different ways.
Well, today I want to present you with another method of getting your csv file into sql, using PHP code. For this piece of code, the full credit goes to legend. You need to make sure the database is already created before you dump the date.
/********************************************************************************************/
/* Code at http://legend.ws/blog/tips-tricks/csv-php-mysql-import/
/* Edit the entries below to reflect the appropriate values
/********************************************************************************************/
$databasehost = “localhost”;
$databasename = “test”;
$databasetable = “sample”;
$databaseusername =”test”;
$databasepassword = “”;
$fieldseparator = “,”;
$lineseparator = “\n”;
$csvfile = “bbqrest.csv”;
/********************************************************************************************/
/* Would you like to add an ampty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don’t set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************************************************************************/
$addauto = 0;
/********************************************************************************************/
/* Would you like to save the mysql queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
/********************************************************************************************/
$save = 1;
$outputfile = “output.sql”;
/********************************************************************************************/if(!file_exists($csvfile)) {
echo “File not found. Make sure you specified the correct path.\n”;
exit;
}$file = fopen($csvfile,”r”);
if(!$file) {
echo “Error opening data file.\n”;
exit;
}$size = filesize($csvfile);
if(!$size) {
echo “File is empty.\n”;
exit;
}$csvcontent = fread($file,$size);
fclose($file);
$con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
@mysql_select_db($databasename) or die(mysql_error());$lines = 0;
$queries = “”;
$linearray = array();foreach(split($lineseparator,$csvcontent) as $line) {
$lines++;
$line = trim($line,” \t”);
$line = str_replace(”\r”,”",$line);
/**********************************************************************************************/
This line escapes the special character. remove it if entries are already escaped in the csv file
***********************************************************************************************/
$line = str_replace(”‘”,”\’”,$line);
/**********************************************************************************************/$linearray = explode($fieldseparator,$line);
$linemysql = implode(”‘,’”,$linearray);
if($addauto)
$query = “insert into $databasetable values(”,’$linemysql’);”;
else
$query = “insert into $databasetable values(’$linemysql’);”;$queries .= $query . “\n”;
@mysql_query($query);
}@mysql_close($con);
if($save) {
if(!is_writable($outputfile)) {
echo “File is not writable, check permissions.\n”;
}else {
$file2 = fopen($outputfile,”w”);if(!$file2) {
echo “Error writing to the output file.\n”;
}
else {
fwrite($file2,$queries);
fclose($file2);
}
}}
echo “Found a total of $lines records in this csv file.\n”;
?>
So that is one nice easy way to do it, and the code is easy to follow and understand, so that you can mod and adapt it to your needs. Many thanks to legend for this code.
Learning PHP and MySQL - Good Books to Have
If you are serious about learning PHP and MySQL, you need good resources. There are many online tutorials and websites, and I have listed some really useful resources in a previous post. If you are like me and prefer to not read off a screen, and instead like to use a book, there are many excellent books out there that range from novice to expert level. I have collected what I believe to be some of the best books to have and added them my Programmers Store, so that they are in one easy place for you to find. So go ahead and check it out.
You can also find some on your library bookshelf for free, which is always better… free is good. The one that I am using is Beginning PHP5, Apache, and MySQL Web Development (Programmer to Programmer), and I love it. It starts you from scratch, although it does help to have some HTML/CSS knowledge. The Appendix has instructions on installing Apache, PHP, and MySQL, which came in really handy for me.
CMS’s - Another reason to master PHP
If you are interested in web development, then you’ve probably at some point discovered, dealt with, or cursed at a Content Management System (CMS). This is actually one of the main avenues through which I discovered, and decided to learn PHP. Installing a CMS and modding it to get it to look like you want, let alone work like you want it to, can be painful. It can also be fun… it’s all a matter of perspective. Using a CMS can save you a lot of time and effort in putting a website up. Depending on the application that you choose, using a CMS can also add a lot of functionality to your website with minimal coding effort and time on your part.
Many CMS frameworks are built on PHP, and having PHP skills goes a long way in helping you install, secure, and modify files so that you get the look, feel, and functionality that you are seeking.
There are many PHP-based Content Management Systems out there, and picking out one that works is often a matter of trial and error. I don’t want to dwell on the different CMSs in this post since they are too many to count, but a good place to look at and compare them is http://www.opensourcecms.com. You can test-drive a lot of them there, look at the admin panel, and determine if the CMS is what you’re looking for.
My recommendation is that you get yourself a free, or relatively cheap hosting account that offers PHP and mySQL. A good feature to have would be the ability to create subdomains. You can then use this account to test and play with CMS applications to your heart’s content, without any risk to your primary website. This is the approach that I use when testing and picking content management systems.
Hosting
For cheap hosting, I recommend Hasty Host because they have a great capacity plan that will allow you lots of space and bandwidth to set up different systems and test them out. If you want to use a free account, then I would recommend going with Byet Host. They offer you cpanel hosting, with 3 MySQL databases, and 5 additional subdomains.
Bear in mind that you’re going to need a MySQL database for each system you install, so the more the better. I prefer to shell out the $2.95 a month for Hasty Host because I get an unlimited number of MySQL databases, and unlimited subdomains. With the free hosting plans you will have to contend with ads on your page. However, with a plan like the one I have at Hasty Host, you are in full control, and if one of your CMS experiments takes off and you like what you’ve created, you don’t have to worry about stripping everything down, and starting again on an ad-free hosting account. You’ll already have you site set up and ready to go!
Domain Names
The other thing you may or may not need is a domain name. If you go with a free hosting plan, you won’t need a domain name unless you decide to use one, and you free hosting account allows you to. In most cases your account is a subdomain of their account, for example: myexperiments.myfreehost.com, or myfreehost.com/myexperiments. If you do go with a cheap hosting account such as Hasty Host, then you will need to purchase a domain name. I recommend always buying your domain names from established registrars, such as GoDaddy (www.GoDaddy.com).
Once you have your hosting account set up, you can start playing with the different content management systems to see what they have to offer.
Some Tips
- If you want to test the applications side by side, create subdomains for each, so that you can compare features and functionality without the hassle of installing and uninstalling.
- Name each subdomain with a meaningful name in case your experiment takes off and you decide to keep the website.
- See if your hosting offers a script installer such as Fantastico, since this makes installing different CMSs easier.
- You can use your account to test blogs, bulletin board scripts, and other scripts too!
- Most CMS apps have user forums, which are always a good place to go for tips and help when you get stuck.
Have fun! And happy coding.





