Published 6 months, 1 week ago
in Tips.
This is another quite simple article. The idea is to find the filename of a remote URL, so if you have the address “http://www.example.com/files/movie.avi” stored in a variable, you want to strip out everything and just be left with “movie.avi”. This is quite a trivial problem, but it is useful to know about this function for doing it.
The way we’ll find out the filename is using PHP’s basename() function, which takes the path to a local file, or the address of a remote file, and returns the filename associated with it. Note that this doesn’t check that the file exists - it just examines the name.
So, for example:
<?
echo basename('http://example.com/files/movie.avi'). '<br />'; // returns "movie.avi" (remote file)
echo basename('/home/user/password.txt'). '<br />'; // returns "password.txt" (local file)
echo basename('http://example.com/') . '<br />'; // returns "example.com"
echo basename('http://example.com/files/page.php?admin=true'); // returns "page.php?admin=true"
?>
The last example above shows that the function doesn’t strip out things such as the query string, so if you want to do that, you’ll have to do it yourself. One possible way is to use the explode() function on the string returned by basename():
<?
$page_parts = explode("?",basename('http://example.com/files/page.php?admin=true'));
echo $page_parts[0];
// returns "page.php"
?>
For more information, please check out the PHP manual entries for basename and explode, or leave a comment!
Published 6 months, 2 weeks ago
in Tips.
Earlier this week, I downloaded some script I wanted to try out as a .tar.gz file, extracted it to my computer then set my FTP client to upload everything to the web server. Half an hour later, I came back and it was still going. Given that Dreamhost gives me SSH access to the server, this is what I should’ve done in the first place, and when I realised it, it took about a minute to get all the extracted files up there.
- Upload the .tar.gz file to the server using an FTP program (or use SSH to download them straight to the server).
- Connect to the server via SSH, for example using PuTTY.
- Use the following command to extract, where your file is called file.tar.gz:
tar zxf file.tar.gz
- That’s it! You can then delete the archive file and move around the extracted files using either the shell commands (e.g. rm file.tar.gz to delete the archive)
This is by far the easiest way to go about uploading a script to your web server, if you have this facility.
The command contained three parameters, zxf, which were passed to the tar program, these have the following function:
- x - extract
- z - it’s gzipped (so omit the z if you merely have a .tar file)
- f - supplying filename on command line.
Published 1 year, 4 months ago
in Tips.
Gmail have just launched a new feature: Gmail Fetcher, which lets you download email from five other (pop3 accounts) to your Gmail inbox. You can set it up on the Accounts tab of the settings page.
Related Link:
How do I set up Mail Fetcher?