tags:

views:

56

answers:

2

For example - on vimeo.com, each video has an id number. To view that video, you simply navigate to vimeo.com/2847535 or whatever.

Let's say I have a similar site where every time a user adds a video, it is assigned a unique id. How do I create a directory with that ID number after I add the video to the database?

Also, once the video is in the database and the directory has been created, now I have to display the right video on the php page that loads when a user visits www.example.com/38540305 ...my second question is, how do I pull the video id from the URL? I know how to do it when the url looks like www.example.com/video.php?38540305 using _GET, but I don't think that will work without the "?"

Links to resources or code much appreciated!

+8  A: 

Instead of creating a directory for each video, you could use mod_rewrite to map any url like www.example.com/[numbers] to video.php. Note that you will need Apache as your web server to do this.

e.g. in a .htaccess file (or apache configuration file under directory or vhost)

RewriteEngine On
#check path is not a file
RewriteCond %{REQUEST_FILENAME} !-f
#check path is not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#redirect any numeric path to video.php
RewriteRule ^([0-9]+)$ video.php?id=$1 [L]

You can now use $_GET[id'] to get the video id.

To answer to your first question directly, you could after inserting the record call mysql_insert_id() (or equivalent) to get the last inserted id. You could then obviously use mkdir() to create your directory. This still wouldn't work unless you created an index.php file within each directory.

Tom Haigh
+2  A: 

You are going to want to use mod_rewrite for this. Something like this:

RewriteEngine On
RewriteRule ^([\-_0-9A-Za-z]+)$  index.php?a=$1 [L]
You can customize RewriteRule as much as you want.

Ths should rewrite the URL after the '/', so instead it goes to index.php?a=path

Chacha102