As a freelance IT guy, I occasionally get cause to work on technologies I don’t usually mess around with. I last did any PHP work over 10 years ago so obviously I am rusty but a trusty google search provided me all the information I needed. Here are some of the useful functions / methods / tips i’ve recently used on projects.
Upload file using FTP
// FTP access parameters $host = 'ftp.example.org'; $usr = 'example_user'; $pwd = 'example_password'; // file to move: $local_file = './example.txt'; $ftp_path = '/data/example.txt'; // connect to FTP server (port 21) $conn_id = ftp_connect($host, 21) or die ("Cannot connect to host"); // send access parameters ftp_login($conn_id, $usr, $pwd) or die("Cannot login"); // turn on passive mode transfers (some servers need this) // ftp_pasv ($conn_id, true); // perform file upload $upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII); // check upload status: print (!$upload) ? 'Cannot upload' : 'Upload complete'; print "n";
Create a unique filename
$ourUniqueFilename = uniqid() . '.txt'; // generate a unique filename with the txt extension
Exception handling
try { // do some stuff to throw an error } catch(Exception $e)//catch exception { echo 'Caught exception: ', $e->getMessage(), "n"; }
{
// do some stuff to throw an error
}
catch(Exception $e)//catch exception
{
echo ‘Caught exception: ‘, $e->getMessage(), “n”;
}