How to send Download Link By hiding directory - php

I wanted to send my user download link when a user buys the product. I send the download link through mail. So when i send the download link i send it through like.. www.xyz.com/images/2343354.jpg
But i don't want let him know the directory structure. Means when a user tries to access the directory aftwards link /images/2343355 he could download others images that i don't want to give to that user.
Is there any way i can avoid by giving the user directory link to download ?
my images are inside the public/images directory as i have made a laravel application.

Provide hash url to the user like www.xyz.com/download?image=XNS2323NIEYEDUJES
You need to store the image name with related hash value in a table.
In the download page get the image request data and retrieve the original image name from db.
And the Make download with PHP download code.
<?php
//get original filename with hash name from fb and store in a variable
if($dboriginalfilename!="")){
$fileName = basename($_GET['file']);
$path = set ur path;
$filePath = $path.$fileName;
if(!empty($fileName) && file_exists($filePath)){
// Define headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$fileName");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
// Read the file
readfile($filePath);
exit;
} else {
echo 'The file does not exist.';
}
}
I think this will work for you. Thank you

Create a JavaScript function like this which will download file and user will not get to know what url is called.
Here is sample solutions which you can try
download
<script>
function downloadfile(filename){
var file_path = 'host/path/'+filename+'.ext';
var a = document.getElementById('yourlinkId'); //or grab it by tagname etc
a.href = file_path;
a.download = file_path.substr(file_path.lastIndexOf('/') + 1);
document.body.appendChild(a);
a.href = '';//remove path after download
}
</script>
In this code a normal link will be displayed without any href and download file.Then after on click of it you will pass only filename and file will be downloaded then after that if user hover over link no path will be displayed.
Here file name in on-click('filename) will be dynamic each time and directory path is static in your script.
Try this once.

Related

Php header() User Agent Change

$file_name = $_GET['title'];
$file_url = $_GET['url'] . $file_name;
header('Content-Type: video/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
exit;
I'm using this code to download files in my site fetching from another websites.
It works if my url looks like:-
https://www.example.com/video_download.php?title=video.mp4&url=http://googlevideo.com/video/download/223689/289048
(example)
So, it starts downloading by fetching the video file from http://www.googlevideo.com/video/play/221589 to my site.
But my problem is that the file can be accessed if the person uses a PC.
Can I change the User Agent by using header()?
Is it possible?
So if I change the user agent into a PC user agent, so it can be downloaded from a mobile!
I'm sorry, but the User Agent has nothing to do with readfile() function. Readfile() will just throw the raw file input into your browser. Useful for e.g. rendering images through PHP to the client without having to expose the real file name.
Indeed, it is possible to render video to the client with readfile(), but using a HTML5 video tag will dramatically improve performance. This will also provide better mobile support.
Hope this helps you,
You can use stream_compy_to_stream
$video = fopen($file_url);
$file = fopen('videos/' . $title . '.mp4', 'w');
stream_copy_to_stream($video, $file); //copy it to the file
fclose($video);
fclose($file);
I wrote a class for downloading youtube video files. you can find it here.

Codeigniter rename downloaded file?

Let's say the user upload a zip file called codeigniter.zip. And my application saved it with name 1.zip that is the file's id in database table. Also it saved file's original name to the table. If the user download this file he download it by name 1.zip. How can i give this file to the user with name codeigniter.zip? Thank you.
You could do something like this:
$actual_filename = '1.zip'; // I assume this will come from some model's method
$desired_filename = 'codeigniter.zip' // same is assumed here
header('Content-Disposition: attachment; filename=' . $desired_filename);
echo file_get_contents('/path/to/' . $actual_filename);
Content-Disposition: attachment should make the browser prompt the user to download the file, and filename= tells the browser what to call the file [by default].

PhP download link with variables

Ok, so I'm trying to create a download link generator on my site.
What I mean by that is this :
the user writes "list X" and the site lists every file in a given directory
every file has a number (from 0 to n)
when the user types : "download 5" for example
it redirects the user to a download script
the script makes it so that the user has a download "box" that pops-up
I first thought that I could create an array with every file name and then use the position in that array for the "download X" command.
So that, when he redirects the user to the download script, the variable with the name of the file that the user wants to download is "POST"ed to the download script and the header is changed in consequence.
So here are my two questions :
1) - How do you change the download script according to the users input ?
2) - The input field is already used for other purposes with a "$_SERVER['PHP_SELF']", so I don't know how to "POST" variables without a form ?
This is my simple download script :
<?php
header('Content-disposition: attachment; filename=huge_document.pdf');
header('Content-type: application/pdf');
readfile('huge_document.pdf');
?>
Thanks in advance !
Don't make them type in anything. Make each download a link they click on. In the link's URL you will have a query string with the download's unique identifier. Something like this:
Down file #6
downloadscript.php would then get the ID from the $_GET superglobal and you can go from there (using an array as mentioned in your example):
<?php
$download_id = (int) $_GET['id']; // 5
$files = array(
file1.pdf,
file2.pdf,
file3.pdf,
file4.pdf,
file5.pdf,
file6.pdf // This is the file they'll get
);
$filename = $files[$download_id];
// get the file name from your array or database
header('Content-disposition: attachment; ' . filename=$filename);
header('Content-type: application/pdf');
readfile($filename);
?>

allow user to download after submitting form data

I have to allow a user to download after he has submitted his USER data.
I've created three pages,
will display files available for download(catalogue_downloads.html)
will take in USER data (form_collecting.php)
will take user data validate it send it to an email address and redirect to downloads page (mail.php)
The schema is so catalogue_downloads.html->form_collecting.php->mail.php->catalogue_downloads.html
I am able to redirect to catalogue_downloads but how can I make the user download the file he requested via PHP
Thank you
Regards
Vinoth
http://php.net/manual/en/function.readfile.php read the first part (forcing a download), you can read a file and then sending it to the user via the headers. Thus you don't have to give away your file structure (and perhaps even letting the file outside your website's root/public_html).
You can't feed the user multiple files, so unless you compress them server side in a single archive you have to show him the links to the files.
However, you're not limited to a link to the actual file, you can link to a "dowloader page" which feeds the user a single file when called so unauthorized users can't access the files (see PENDO's answer on how to do that).
you can user below code if needed..
$root = $_SERVER['DOCUMENT_ROOT']; $site = '';
$folder = "path where you want to store downloaded files";
$fullPath = $root.$folder.'/'.$_REQUEST['filenm'];
//$_REQUEST['filenm'] = file name which you want to download.
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower(html);
header("Content-type: application/".$ext);
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment;filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
} } fclose ($fd); exit;
Thanks.
Here is the deal:
When redirecting from catalogue_downloads.html to form_collecting.php, give your catalog a unique ID and post it to form_collecting.php. Keep a hidden input field in your form which will keep the previously posted catalog ID. When the form is posted and validated start the process of mail.php. When mail is successful, read the catalog ID and redirect your user to the catalogue_downloads.html.
Hope this will help you out.
Regards

How to force a file to download in PHP

I have list of images and I want a "Download" link along with every image so that user can download the image.
so can someone guide me How to Provide Download link for any file in php?
EDIT
I want a download panel to be displayed on clicking the download link I dont want to navigate to image to be displayed on the browser
If you want to force a download, you can use something like the following:
<?php
// Fetch the file info.
$filePath = '/path/to/file/on/disk.jpg';
if(file_exists($filePath)) {
$fileName = basename($filePath);
$fileSize = filesize($filePath);
// Output headers.
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileName);
// Output file.
readfile ($filePath);
exit();
}
else {
die('The provided file path is not valid.');
}
?>
If you simply link to this script using a normal link the file will be downloaded.
Incidentally, the code snippet above needs to be executed at the start of a page (before any headers or HTML output had occurred.) Also take care if you decide to create a function based around this for downloading arbitrary files - you'll need to ensure that you prevent directory traversal (realpath is handy), only permit downloads from within a defined area, etc. if you're accepting input from a $_GET or $_POST.
In HTML5 download attribute of <a> tag can be used:
echo '<a href="path/to/file" download>Download</a>';
This attribute is only used if the href attribute is set.
There are no restrictions on allowed values, and the browser will
automatically detect the correct file extension and add it to the file
(.img, .pdf, .txt, .html, etc.).
Read more here.
The solution is easier that you think ;) Simple use:
header('Content-Disposition: attachment');
And that's all. Facebook for example does the same.
You can do this in .htaccess and specify for different file extensions. It's sometimes easier to do this than hard-coding into the application.
<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
By the way, you might need to clear browser cache before it works correctly.

Categories