简体   繁体   English

如何为数据库中的每个视频创建单独的目录并从URL中提取其ID?

[英]How do I create a separate directory for each video in a database and pull its ID from the URL?

For example - on vimeo.com, each video has an id number. 例如 - 在vimeo.com上,每个视频都有一个ID号。 To view that video, you simply navigate to vimeo.com/2847535 or whatever. 要查看该视频,您只需导航到vimeo.com/2847535或其他任何内容。

Let's say I have a similar site where every time a user adds a video, it is assigned a unique id. 假设我有一个类似的网站,每次用户添加视频时,都会为其分配一个唯一的ID。 How do I create a directory with that ID number after I add the video to the database? 将视频添加到数据库后,如何创建具有该ID号的目录?

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? 此外,一旦视频在数据库中并且目录已经创建,现在我必须在用户访问www.example.com/38540305时加载的php页面上显示正确的视频...我的第二个问题是,如何从网址中提取视频ID? 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 "?" 我知道当网址使用_GET看起来像www.example.com/video.php?38540305时该怎么做,但我不认为没有“?”

Links to resources or code much appreciated! 链接到资源或代码非常感谢!

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. 您可以使用mod_rewrite将任何网址(如www.example.com/ [numbers])映射到video.php,而不是为每个视频创建目录。 Note that you will need Apache as your web server to do this. 请注意,您需要Apache作为您的Web服务器来执行此操作。

eg in a .htaccess file (or apache configuration file under directory or vhost) 例如在.htaccess文件中(或目录或vhost下的apache配置文件)

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. 您现在可以使用$_GET[id']来获取视频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. 要直接回答第一个问题,可以在插入记录调用mysql_insert_id() (或等效mysql_insert_id() )后获取最后插入的id。 You could then obviously use mkdir() to create your directory. 然后你可以使用mkdir()来创建你的目录。 This still wouldn't work unless you created an index.php file within each directory. 除非您在每个目录中创建了一个index.php文件,否则这仍然无效。

You are going to want to use mod_rewrite for this. 您将要使用mod_rewrite。 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 应该在'/'之后重写URL,所以它转到index.php?a = path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM