I'm generating PDF documents on the fly using php-pdftk and saving it to the public directory. Works great locally, but on a production Digital Ocean server, the file won't save to the public directory. In my code below at the ->saveAs..., the 'docs/reg-forms/' are directories within public.
...
$pdf = new Pdf('/docs/ax_reg_form.pdf');
$result = $pdf->fillForm([
'email' => $this->usercar->user->email,
'date' => $this->usercar->created_at,
...
])
->needAppearances()
->saveAs('docs/reg-forms/'.$this->usercar->user->id.'_'.strtolower($this->usercar->user->first_name).'_'.strtolower($this->usercar->user->last_name).'_'.'car_class.pdf');
session()->flash('url', '/docs/reg-forms/'.$this->usercar->user->id.'_'.strtolower($this->usercar->user->first_name).'_'.strtolower($this->usercar->user->last_name).'_'.'car_class.pdf');
return redirect()->route('usercar.show', $this->usercar->id);
try to wrap with public_path helper
public_path('docs/reg-forms/');
and check if the path exists
if (!file_exists(public_path('docs/reg-forms/'))) {
mkdir(public_path('docs/reg-forms/'), 0777, true);
}
Related
I am making like a dropbox clone for a school project, i made an upload fuction that uploads to the local disk so it cant be accessed by everyone.
Upload Function
public function updatedUpload($upload){
$object = $this->currentTeam->objects()->make(['parent_id' => $this->object->id]);
$object->objectable()->associate(
$this->currentTeam->files()->create([
'name' => $upload->getClientOriginalName(),
'size' => $upload->getSize(),
'path' => $upload->storePublicly('files', ['disk' => 'local'])
])
);
$object->save();
$this->object = $this->object->fresh();
}
But I want to put it in an in the home.blade.
How do I make this and only the person who uploaded it can access it?
You can create a specific link for the user that can see this resource.
For example in your routes/web.php
Route::get('/private_resource',[\App\Http\Controllers\PrivateResource::class, 'getLocalResource'])
->name('get_private_resource')->middleware('auth');
Define PrivateResource controller
class PrivateResource
{
public function getLocalResource($filename){
// Get your resource, for example
$file = File::where('name', $filename)->first();
// Check whether user can access this resource
// For example
if(\Auth::id() != $file->user_id) abort(403);
return \Storage::disk('local')->download($filename);
}
}
Extra: If you want extra privacy, you can use signed route using below function.
\URL::signedRoute('get_private_resource');
And add signed middleware to your route
Route::get('/private_resource',[\App\Http\Controllers\PrivateResource::class, 'getLocalResource'])
->name('get_private_resource')->middleware('auth','signed');
i am trying to create a file in laravels App/Http/Controller directory
so i had used this code
$path = 'App/Http/Controller/';
$fileName = 'demo.php';
$contents = "<?php echo 'hello'; ?>";
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
$path = $path.$fileName;
file_put_contents($path, $contents);
but this is creating a file with directory in Public folder..
how to get rid of this.?
Setting up a Disk
You can setup a disk at config/filesystems.php and then benefit from Storage facade methods.
config/filesystems.php
'tmp' => [
'driver' => 'local',
'root' => public_path('..'),
],
Notice: public_path() Starts from public folder, you can go one step ahead using public_path('..')
Using Storage Facade
You can now use Storage methods such as allDirectories and allFiles for retrieving directories and files, or put for saving new files as thoroughly explained in laravel doc.
Storage::disk('tmp')->allDirectories();
Storage::disk('tmp')->allFiles('routes');
// Storing new photo
Storage::disk('tmp')->put('newfile.jpg',$content);
I can upload my images while i'm working on local but when I moved my site to host it doesn't upload images while get names of them in database,
my filesystems.php
'default' => env('FILESYSTEM_DRIVER', 'local'),
disks' => [
'local' => [
'driver' => 'local',
'root' => public_path('images/'),
],
....
sample of my upload codes:
if ($request->hasFile('imageOne')) {
$imageOne = $request->file('imageOne');
$filename = 'productone' . '-' . time() . '.' . $imageOne->getClientOriginalExtension();
$location = public_path('images/');
$request->file('imageOne')->move($location, $filename);
$product->imageOne = $filename;
}
Issue:
In host i get uploaded image name in database, but no image in my
public/images folder.
any idea?
UPDATE
I found interesting folder in my host, as I moved all my public folder files to server public_html now it created another folder in my root called public and inside that has images folder including all images i uploaded so far!
Questions:
Why laravel created new public/images folder in my root instead of
using my public_html?
How to fix it?
SOLVED
I added this code to my public_html/index.php file and it's working now.
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
Hope it helps.
I change the public path inside my save image controller to look like this:
if (request()->imagefile->move(public_path('../../public_html/destination'), $imageName))
echo "Yap";
else echo "Nop";
I am able to load all the files in a directory using blueimp file uploading plugin.
I just want to select only few of the files using filenames in that.
I know that get method of uploadhandler returns all the files in given directory but is there any way to select only few files?
This is what I have done to get all files..
public function getFile() {
extract($this -> reqData);
require_once ('path/to/UploadHandler.php');
$options = array(
'script_url' => "path to uploadhandler",
'upload_dir' => "xxxxxxx",
'upload_url' => "xxxxxxx",
'type' => 'project_files'
);
$upload_handler = new UploadHandler($options);
exit;
}
Please don't give negative remarks just because my editing is poor.
Thanks everybody...
I'm using Laravel 4 framework, I have a function that creates a csv file called data_78888.csv the number 78888 changes everytime the function is run to generate a csv file. That function returns a string like that : "Download/78888"
The folder where my csv files are created is called "outputs" and is located in my project folder where the app folder is located to, (it is not in the public folder).
What I would like to do is to create a route that points to my Process controller like that :
Route::get('Download/{token}', array('uses' => 'ProcessController#downloadCSV'));
In my controller I would like to send that csv file to the browser to download it , I'm doing like that :
<?php
class ProcessController extends BaseController {
public function downloadCSV($token){
$fileToDownload = "data_".$token.".csv";
$filePath = "outputs/";
return Response::download($filePath, $fileToDownload, array(
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment;filename="'.$fileToDownload
));
}
}
The issue is that this is not working and I get an html file called 78888.htm and an error on the server.
How can I make this working please?
The path to the file has to include the name and file extension of the file.
So try this;
$fileToDownload = "data_".$token.".csv";
$filePath = base_path() . "outputs/" . $fileToDownload;
return Response::download($filePath, $fileToDownload, array(
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment;filename="'.$fileToDownload
));
Also make sure the file exists, before downloading.