Archive for December, 2007

How to get the filename from a remote URL

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!

How to move a file using PHP

This article will tell you how to move a file on your webserver using the filesystem functions of PHP. It’s something that is very easy when you know how, but not immediately obvious due to the lack of an explicit move() function.

The trick is to use the rename() function. If you just wanted to rename a file, you would use the code:

<?php
rename("oldname.txt", "newname.txt");
?>

To move a file, it is something similar:

<?php
// Move file into folder called 'store'
rename("file.txt", "./store/file.txt");
?>

In the above example, we assume that both the file and the folder are in the same directory as the php script. The period at the front of the new file path means that it starts looking in this directory - if I omitted that, then it would look for a folder called ’store’ in the root directory: /.

Rather than waffle on about the specifics of what can go wrong, I’ll point you at the PHP manual page: rename. If you have any difficulties, feel free to leave a comment.

How To: Extract .tar.gz archives remotely via SSH

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.

  1. Upload the .tar.gz file to the server using an FTP program (or use SSH to download them straight to the server).
  2. Connect to the server via SSH, for example using PuTTY.
  3. Use the following command to extract, where your file is called file.tar.gz:
    tar zxf file.tar.gz
  4. 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.

About

You are currently browsing the vdhri.net weblog archives for the month December, 2007.

Longer entries are truncated. Click the headline of an entry to read it in its entirety.

Categories