Downloading a file from a PHP server via a website [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have physical files which I want users to download on my website. The files are located at:
C:/xampp/htdocs/myfile/uploads/*
I need a PHP script which can download files dynamically on click. Let's say I have the following button which when clicked it triggers magic.php script.
Download file
What PHP code do I need in magic.php to download the file name I passed in the query parameters?

Download link
Download
magic.php page
<?php
$file = 'C:/xampp/htdocs/myfile/uploads/'.urldecode($_GET['file']);
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>

Maybe you could do something like this: (Correct me if i am wrong)
<a href="uploads/<?php echo $row['name']; ?>" download="download">

Related

PHP download will randomly gives Failed - Network error [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 days ago.
Improve this question
I am working on a WordPress Website, in which the admin can download the CV of users in PDF or Word Format
When I try to download CV, it randomly gives Network error.
ob_start();
header('Content-Description: File Transfer');
header('Content-Type: ' . $file_mimetype);
header("Content-type: application/force-download");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: Binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . #filesize($file_path));
ob_end_clean();
flush();
#readfile($file_path);
exit;

How to generate temporary file download url for laravel local storage? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
From Laravel official documentation:
For files stored using the s3 or rackspace driver, you may create a
temporary URL to a given file using the temporaryUrl method.
Is there any way to have temporary download url for files rather than using s3/etc and for local storage only. I am using https://github.com/spatie/laravel-medialibrary library for development.
public function downloadFileAction()
{
if (isset($_REQUEST['file_name'])) {
$pathFile = STORAGE_PATH.'/'.$_REQUEST['file_name'];
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$_REQUEST['file_name'].'"');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($pathFile));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Expires: 0');
readfile($pathFile);
}
}
public function uploadFile(array $file)
{
$storagePath = STORAGE_PATH;
#mkdir($storagePath, 0777, true);
$fullPath = $storagePath. uniqid (time()). $file['name'];
$fileTemp = $file['tmp_name'];
move_uploaded_file($fileTemp, $fullPath);
return $fullPath;
}
In your controller, you can take file with this: $_FILES

how to download file with php? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i want to solution download file into php
I am using this way to download file but getting issue download file code
$filename = '[file name].[extension]';
$file="application/[extension]";
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
problem:
Try this Follow the instruction by variable.... Use file path not filename
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($dest_file_path));
header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename="'.$dest_file_name.'"');
readfile($dest_file_path);
Here is basic example to download file -
$file="http://example.com/filename.mp3"; // your filename with path
header("Content-type: application/x-file-to-save");
header("Content-Disposition: attachment; filename=".basename($file));
readfile($file);
exit;

how to create Downloadable links in php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have used file system to store the files, all files are either word or pdf files. how should i create their downloadable links on client side.
Any help would be appreciated.
you can download file using:
<?php
$file = 'filename.extension';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
You can simply write the file path in the href attribute of the <a /> tag...
Download Here
<?php
if (isset($_GET['file'])) {
$file = $_GET['file'];
if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) {
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
}
} else {
header("HTTP/1.0 404 Not Found");
echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>";
}
Try the above snippet and name it as download.php!
Once the above has been done, save the file and if needed upload to the server hosting your web page.
Finally, once uploaded, all future .PDF links that you want to be downloaded instead of opened in the browser will need to point to download.php?file=example.pdf, where example.pdf is the name of the PDF you want for the user to download. Below is an example of what a full link could look like.enter code here
Click here to download PDF
Download
This should do the job.

Allowing downlaod after payment [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i am confused in how to allows download the file.zip after the payment.If i redirect them to a download page where is file is placed in sever they can download the file again easily or they can pass that link to anyone.
Any suggestions please!
Don't use a direct link to the file - use a PHP file that serves the file up as a download, but only if a certain session var is found (created in the confirmation of the payment process)
Just like #SmokeyPHP mentioned, just output the file through PHP instead of linking to it directly.
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
http://php.net/manual/en/function.readfile.php
This way you have a full control over who downloads what. Of course depending on the file size, you may wish to split the file into smaller chunks. You don't want to be buffering 40 MB files in your server memory every 5s. With bigger files, you can use something like this:
<?php
$file = fopen("file.dat", "r");
while (!feof($file)) {
echo fgets($file);
}
fclose($file);
?>
put .zip file outside the webserver;
protect the .zip URL with some rule [e.g. being logged in and having purchased the resource];
associate the .zip URL with an action that reads the actual binary file and forwards it to user [plenty of examples in here].

Categories