2

I have a video. Suppose it's name is sample.mp4

But I want it to be served as another dynamic and non repeated name.

For example:

data-videomp4="assets/video/sample.mp4"

should be like this

data-videomp4="assets/video/123456789.mp4"

OR

data-videomp4="assets/video/any_RANDOM_NAME.mp4"

File should remain only one. I can copy files at different name at run-time but that will not be wise to do.

So I need something like a dynamic ROUTE which will be linked to single file always.

File size is always less than 10 Megabytes.

  • are you using a routing component? if not, use one! and if you don't want to, mess around with .htaccess virtual host directives and configure a redirect to a php file that will serve your file up. Using a router, you could set a regex like `#assets\/video\/\w+.mp4#` – delboy1978uk May 08 '18 at 08:46
  • I am using Codeigniter. How do I implement dynamic routing for single file ? Please give an example. – Hardik Trivedi May 08 '18 at 08:48
  • Please check the codeigniter docs for dynamic routing https://www.codeigniter.com/user_guide/general/routing.html – delboy1978uk May 08 '18 at 08:50
  • Have you tried to create a [symlink](https://stackoverflow.com/questions/1951742/how-to-symlink-a-file-in-linux) dynamically ? – Agnius Vasiliauskas May 08 '18 at 08:51
  • How do you determine which file to serve? Is there just a single file in `assets/video/`? Then an .htaccess file in there with `RewriteRule ^ sample.mp4` will do the trick… – deceze May 08 '18 at 09:03
  • Yes there is only one file. Can you provide me exact line which I can put somewhere ? To deliver same file with different name? – Hardik Trivedi May 08 '18 at 09:06
  • In this case I want the file to be served by different name. Not redirecting to that file fellas. I know the suggestions you gave me. I appriciate the help. But nothing in here would avail the option that original filename must not ever disclosed to anywhere. Also file should deliver by random name. – Hardik Trivedi May 08 '18 at 09:10
  • *"original filename must not ever disclosed to anywhere"* – What is the point of that? If the file is accessible via *any* name, then what significance does its *"original name"* carry? – deceze May 08 '18 at 09:12
  • ^ i also don't get why this is necessary. if you are just trying to get around a cache just append `?` or something. The user will still be able to download the file even if the name is different as long as they can access it and you aren't chunking the ouput. – Alex May 08 '18 at 19:25

2 Answers2

0

I'm not sure of the rewrite rules Codeigniter applies by default and how they may or may not conflict with this, but fundamentally all you want is to make your web server serve one specific file regardless of what URL is being accessed. To do this in Apache, you place a file called .htaccess into the assets/video/ folder with this content:

RewriteEngine on
RewriteRule ^ sample.mp4

^ simply matches any request and substitutes it with sample.mp4. See https://stackoverflow.com/a/20563773/476.

deceze
  • 510,633
  • 85
  • 743
  • 889
-1

Try to generate symlinks (shortcuts) on-the-fly, should be fast operation :

<?php

$real_file = 'sample.mp4';

// generating new symbolic link (shortcut)
$rnd_name = md5(random_bytes(100)) . '.mp4';
$cmd = "ln -s {$real_file} {$rnd_name} 2>&1";
$rez = shell_exec($cmd);

// check for errors - for example, no write permission for apache user by default
if ($rez !==null) {
  var_dump($rez);
  exit;
}

echo "<a href='{$rnd_name}' 
         target='blank'>{$rnd_name}
      </a><br><br>
     <video id='myVideo' src='{$rnd_name}' controls='true'>
     </video>";

// don't forget to unlink (delete) created symlinks after some time

?>
Agnius Vasiliauskas
  • 10,935
  • 5
  • 50
  • 70