PhP download link with variables - php

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);
?>

Related

how mySql Data store in unique excel file that should be created dynamically

I have a web page in which user was given certain input methods to fetch data from database. data will be displayed on web page.
I want to store data that user fetched data from my s-q-l database into an excel file.
command i'm using is given below
$excelquery1="SELECT * FROM excel12 INTO OUTFILE 'D:/Downloads/xyz.csv'";
$excelresult1=mysql_query($excelquery1);
it is running ok and data being fetched will be stored in a new file created in Downloads folder with a name xyz.csv
but user have to go manually to that location to check file.
how is it possible that user must be shown downloaded file in the download bar. + I also want to ask that if the xyz.csv is already present in the location when the query execute so in that scenario this query won't get execute saying file already present. so how to tackle this problem ?
how to give unique file name to the file that will be created..
This is the main idea. You need to adequate the code to your need
$file="/path/to/file".date("YmdHis").".csv"; //file location
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);

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/MySQL Hide file links

i am using PHP to connect to a MySQL Database and customers can login to my website and it lists rows form a table based on their login etc.
I need to be able to display a link to a file name in the database but i don't want users to be able to see the link to the file.
for example, they can download file 1234.pdf and if they can view the actual link, they might think of going to the same location but doing file 5678.pdf which is only meant for another user to download.
so basically i want to hide the link in a long string or something but i'm not sure where to start - any ideas?
Thanks
EDIT:
lets say Customer A logs in, they can view rows from table1
TABLE1
customer file_link
A 1234.pdf
A 5678.pdf
B 8765.pdf
B 4321.pdf
so, i dont want customer A to be able to view the links for customer B.
i mean, if customer A hovers over a link and can see the main file path they can type this in their web browser and then change the file name (guess it) to something else and download another customers file(s)
if you're planning on not letting others see the file links then you probably wouldn't want search engines to see them as well. A typical way of forbidding users from trying out such stuff is to have a specific page that flushes the file instead of linking directly to the file. E.g.,
Download
then in download.php you could check user permissions and make the browser download the file.
<?php
$file = 'file1234.pdf';
$file_url = 'http://www.test.com/files/' . $file;
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename="' . $file_url . '"');
readfile($file_url);
die();
?>
I think this is what you'll need.

html/php user download files

On my web server, I have a bat (harmless) file.
And I have code,
Test Bat File
But when the user clicks, it shows the code instead of downloading the file.
You have to right click "save as.." to download the bat file.
Is there way that when a user clicks, it downloads (not having to right click save as)?
Maybe a pop up window that asks user if he/she wants to download the file or not?
you could write a php file, which adds a content-disposition header, sets the mime type to something binary and echo the files content.
Example:
file.php
$batchfile = file_get_contents('batchlocation');
$size = strlen($batchfile);
header('Content-Disposition: attachment; filename="downloaded.bat"');
header('Content-Type: BAT MIME TYPE or something like application/octet-stream');
header('Content-Lenght: '.$size);
echo $batchfile;
You can do so through setting the file in the PHP header() function.
It is explained here:
How to Automatically Start a Download in PHP?

File open/save as dialog and mime types

I am working on a simple document management system for a site - the user can upload around 20 different file types and the docs are renamed then stored in a folder above www, an entry is created in a docs table to capture meta data entered by the user and the item is then retrieved via another php file so the stored location for the files are hidden from the user.
When a user clicks to download a file using a simple a href it calls, for example, "view.php?doc=image.jpg" - when they do this currently the file opens in the browser so a jpg opens a window with pages of "wingdings" like characters etc.
I would like to be able to force a open/save dialogue box so the user decides what to do and my app doesn't try to open in the browser window with the above results.
From a previous posting I found I know I cannot pass the mime type in the "a href" tag so what other options do I have? Could I put header information into the below view.php file, for example?
$_file = $_GET['doc'];
$filename = './dir/'.$_file;
if (file_exists($filename)) {
echo file_get_contents('./dir/'.$_file);
} else {
echo "The file $_file does not exist";
}
;
You could use get_headers() to get the MIME type header of the desired file, and then use header() to output those headers into the file you're showing.
Alternatively, to simply force downloads, this:
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
Should do it.

Categories