I have a form on a web site to submit/upload recorded webcam video files. And then an email, with a file link (to the uploaded file) is sent. It all works successfully. When I access my email to see the arrived message, (via pc) and select the link (to the video file), it automatically plays on my pc, I’m looking for a way to have it so when the link is selected the file downloads, not automatically plays. Any help will be appreciated. Here’s the submit.php file:
<?php
if($_POST){
$to = 'form@....com';
$subject = 'NewForm';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = $name;
$message .= "Hello ".$_POST[name]." - Here is the link to your video: ".$_POST[videolink].". Created by ".$_POST['email']." ";
header('Location: https://....com');
exit;
}
?>
Thanks for your replies and I read the link provided.
I tried, without success, because I don’t know what to add into the filename, because it’s name is randomly generated upon upload, via js:
However it’s generated, you must be storing the filename somewhere in the database so that you can retrieve it. If you’re storing the complete URL for some reason, then you’ll have to strip that out as the parameter should not contain a complete path, only a filename.
The filename you specify here is just a basis for what the file will be called when the user saves it - don’t forget the user could change that. For testing purposes, you could stick any valid video filename string in there. It’s nothing to do with what the file is called on your server, which you must already know as your next bit of code after the header will be to output the file contents.
Instead of having the link in the e-mail be a direct link to the actual filename, you want it to point to a seperate php page that only handles setting the header appropriately and then reading and passing along the return the data from the file. Have a look at Example #1 from this page:
Let’s say you create a page like this call it download.php
This example is almost exactly what you need, except that the filename is hardcoded, right?
So the question is, how do you tell download.php which file you want to download? It would have to be through a query parameter that you pass in the link in the email.
So a generated email message would contain something like:
Hello John - Here is the link to your video: https://....../download.php?somevar=someval
And in download.php you would $_GET['somevar'] and use it to decide which file to serve.
But what exactly should you pass to the page?
The quick and dirty solution would be to just pass the actual filename. So the url in th e-mail would look something like https://....../download.php?file=my%20urlencoded%20filename.mpg and in download.php you could just do something like: $file = urldecode($_GET['file'])
A much safer, but slightly more complicated solution would be to do what @droopsnoot suggested and have a database table where you store each of the paths for each uploaded file against a unique id. Your links would look something like https://....../download.php?id=1
and in dowload.php you would retrieve the actual filename from the table where the id column is $_GET['id']
That code is very dangerous, as it doesn’t impose any limits on the path. I could go to download.php?file=/etc/passwd and download the information on all users in your system.
Using an id is already a lot better.
But you don’t need to use php at all. If you put all videos in the same directory, you can set headers for all files in that directory using the webserver, which is a lot better at serving files than php is.
Thanks for all the replies. All files are currently uploaded to the ‘upload’ folder on the server. How do I “set headers for all files in that directory using the webserver”?