I'm trying to use this: Response::download() to make a download of a file, I'm using Laravel 4, here my code:
public function download_file($file)
{
$file = 'myfile.pdf';
//-- public/docs/myfile.pdf
$path = public_path(). '/docs/' . $file;
$headers = array(
'Content-Type' => 'application/pdf',
);
return Response::download($path, $file, $headers);
}
The error I got:
The file "C:\Users\myuser\Workbench\Web\sites\mysite\public/docs/myfile.pdf" does not exist
What did I went wrong?
Thanks!
Related
Hello i have some problem with doing a force download.
Here is my code :
public function download(Request $request, $id) {
$document = Document::find($id);
$s3 = Storage::disk('s3');
$name = substr($document->link, strrpos($document->link, '/') + 1);
$file = $s3->get($name);
$headers = [
'Content-Type' => 'application/pdf',
'Content-Description' => 'File Transfer',
'Content-Disposition' => "attachment; filename={$name}",
'filename'=> $name
];
return response()->download($file);
}
i don't get it how to access the good file
response :
is_file() expects parameter 1 to be a valid path, string given
Any ideas ?
Your headers are not actually attached to the response. If you refer to: https://laravel.com/docs/5.5/responses#attaching-headers-to-responses you will see that the proper way to send headers in Laravel is:
return response($file)
->header('Content-Type', 'application/pdf')
->header('Content-Description', 'File Transfer')
->header('Content-Disposition', "attachment; filename={$name}")
->header('Filename', $name)
The $file is alse sent as a paramenter in reponse() instead of as download().
I have a Silex project where I want to generate a PDF of a view of this project.
I installed locally wkhtmltopdf on OSX, and i'm using Snappy.
For example, you're on localhost:8001/test/702254 and the page appears. If you access localhost:8001/test/702254/print then the page will be the PDF of the previous (I want the browser show the PDF to the client, then he can download it).
I have a variable in my routes and if it has the 'print' value I want to display the PDF.
$body = null;
$headers = null;
if ($request->get('print') == 'print') {
$uri = str_replace('/print', '', $_SERVER['REQUEST_URI']);
$this->snappy->setOption('cookie', array('MY-COOKIE' => $_COOKIE['MY-COOKIE']));
//$this->snappy->setTimeout(360);
$body = $this->snappy->getOutput('http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . "{$_SERVER['HTTP_HOST']}{$uri}");
$headers = array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="Export.pdf"'
);
}
else {
$body = $app['twig']->render('views/index.html.twig', $twigParams);
$headers = array('Cache-Control' => 'max-age=300, public');
}
return new Response($body, 200, $headers);
My problem is when I call the URL with '/print', I have the following error :
ProcessTimedOutException in Process.php line 1211:
The process "/usr/local/bin/wkhtmltopdf --lowquality --cookie 'MY-COOKIE' '4vkvcjnt2svf0' 'http://localhost:8001/test/702254' '/var/folders/2h/qwnt4dk97dsg1mhv_63ycbrh0000gp/T/knp_snappy574713575a5e51.78801388.pdf'" exceeded the timeout of 60 seconds.
I hope it is not an 'OSX issue' as I read in some topics...
So i'm using Laravel for a while. I wrote this:
public function downloadUserFile(){
$result = $_POST['filename'];
$entry = File::where('filename', '=', $result)->firstOrFail();
$file = Storage::disk()->get(storage_path($entry->filePath));
$headers = array('Content-Disposition' => 'attachment; filename="'.basename($entry->filePath).'"', 'Content-Type' => $entry->mimetype );
return (new Response($file, 200))->header($headers);
}
With the hope that I can download a selected file. Well I get two exceptions in FilesystemAdapter.php line 58 and in Filesystem.php line 381: File not found at path: C:\xampp\htdocs\bluedrive\drive\storage\uploads\1\SHTURCITE - Ti I Az.mp3 ---- This file is in the directory and i can't understand why laravel can't find it.
How can I download file from s3 in php. The following code not working for me...
Here is my code
upload file should work correct
try {
$s3->putObject([
'Bucket' => $config['s3']['bucket'],//'eliuserfiles',
'Key' => "uploads/{$name}",
'Body' => fopen($tmp_name, 'r+'),
//'SourceFile' => $pathToFile,
'ACL' => 'public-read',
]);
download gives me error
$objInfo = $s3->get_object_headers($config['s3']['bucket'], "uploads/{$name}");
$obj = $s3->get_object($config['s3']['bucket'], "uploads/{$name}");
header('Content-type: ' . $objInfo->header['_info']['content_type']);
echo $obj->body;
error
PHP Catchable fatal error: Argument 2 passed to Aws\\AwsClient::getCommand() must be of the type array, string given, called in /php_workspace/s3_php/vendor/aws/aws-sdk-php/src/AwsClient.php on line 167 and defined in /php_workspace/s3_php/vendor/aws/aws-sdk-php/src/AwsClient.php on line 211, referer: http://localhost/upload.php
Simple way to do this:
include Amazon S3 PHP Class in you're project.
instantiate the class:
1. OO method (e,g; $s3->getObject(...)):
$s3 = new S3($awsAccessKey, $awsSecretKey);
2. Statically (e,g; S3::getObject(...)):
S3::setAuth($awsAccessKey, $awsSecretKey);
Then get Objects:
// Return an entire object buffer:
$object = S3::getObject($bucket, $uri));
var_dump($object);
Usually, the most efficient way to do this is to save the object to a file or resource
<?php
// To save it to a file (unbuffered write stream):
if (($object = S3::getObject($bucket, $uri, "/tmp/savefile.txt")) !== false) {
print_r($object);
}
// To write it to a resource (unbuffered write stream):
$fp = fopen("/tmp/savefile.txt", "wb");
if (($object = S3::getObject($bucket, $uri, $fp)) !== false) {
print_r($object);
}
?>
S3 Class -With Examples
S3 Class -Documentation
You can try this :
$bucket= "bucket-name";
$filetodownload = "name-of-the-file";
$resultbool = $s3->doesObjectExist ($bucket, $filetodownload );
if ($resultbool) {
$result = $client->getObject ( [
'Bucket' => $bucket,
'Key' => $filetodownload
] );
}
else
{
echo "file not found";die;
}
header ( "Content-Type: {$result['ContentType']}" );
header ( "Content-Disposition: attachment; filename=" . $filetodownload );
header ('Pragma: public');
echo $result ['Body'];
die ();
public function getDownload($name){
$file = storage_path().'/cv/'.$name; //error here
$headers = array(
'Content-Description' => 'File Transfer',
'Content-Type' => 'application/pdf',
);
return Response::download($file, $headers);
}
I just want to concatenate all in one variable. All parameters are string.
Below is one var_dump () of the two variavaies storage_path() and $name.
If you were to take a look at the download method of the Request factory you will find that you are missing a parameter.
public function download($file, $name = null, array $headers = array(), $disposition = 'attachment');