I'm working on the database restore menu for an application I created, I'm using sqlite for my database, and I want to put my previously backed up database file to the database directory
backupAndRestore.blade.php
<form method="POST" action="/admin/backupAndRestore/restore" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="file-field input-field">
<div class="btn">
<span><i class="material-icons">file_upload</i></span>
<input type="file" name="database" accept=".sqlite">
</div>
<div class="file-path-wrapper">
<input type="text" class="file-path" placeholder="Example: file_name.sqlite" readonly>
</div>
</div>
<button type="submit" class="btn">restore</button>
</form>
web.php
Route::post('/admin/backupAndRestore/restore', 'DatabaseController#restore');
DatabaseController.php
public function restore(Request $request)
{
$database = $request->database;
$file_name = 'database2.sqlite';
$path = database_path($file_name);
file_put_contents($path, $database);
return $path;
}
my code works, the file is stored in the database directory, the problem is when I backup my database file size is 22kb, but when I restore it and I check the database directory, the file is 1kb size and when I open using sublime file contents as shown below
Images
which I expect as shown below
Images
can someone tell where my fault is?
You are using file_put_contents which inserts the contents in the given file and does not move the file.
If you want to store a file in database path..
public function restore(Request $request)
{
$this->validate($request, [
'database' => 'required|file|mimes:sqlite'
]);
$databaseFile = $request->database;
$fileName = 'database2.sqlite';
$databaseFile->move(database_path(), $fileName);
// return success message...
}
For more information uploading files: https://laravel.com/docs/5.4/requests#files.
Related
Here is my controller
public function createAdmin()
{
$photo=$_FILES['bannerPic']['name'];
$tempname=$_FILES['bannerPic']['tmp_name'];
// echo $tempname." ".$photo;
move_uploaded_file($tempname, 'picture/banner/'.$photo);
$data=[
'sliderPic' => $photo,
];
dd($data);
// \App\Models\Banner::create($data);
// return view('Banner');
}
here is my route
Route::post('/BannerEdit', [App\Http\Controllers\BannerController::class,
'createAdmin']);
here is my blade form
<form action="{{ url('') }}/BannerEdit" method="post" class="col-12"
enctype="multipart/form-data">
#csrf
<div class="mb-3 col-12">
<label class="col-4 text-label form-label">Banner Photo*</label>
<input type="file" name="bannerPic" class="form-control input-rounded col-4 mb-3"
required>
</div>
<div class="mb-3 text-end">
<input type="submit" class="btn btn-primary" value="Upload">
</div>
</form>
When i submit the data it Gives me an Error as
move_uploaded_file(picture/banner/favicon.jpg): Failed to open stream: No such file or directory
But this Directory Exists
And i checked with full pathof localhost then it does not supports http path
You need to use getcwd() function like this:
$dirpath = realpath(dirname(getcwd()));
So your controller code will be:
public function createAdmin()
{
$photo=$_FILES['bannerPic']['name'];
$tempname=$_FILES['bannerPic']['tmp_name'];
// echo $tempname." ".$photo;
$dirpath = realpath(dirname(getcwd()));
move_uploaded_file($tempname, $dirpath.'/'.$photo);
$data=[
'sliderPic' => $photo,
];
dd($data);
// \App\Models\Banner::create($data);
// return view('Banner');
}
let me know if it works.. if not then we'll modify $dirpath variable
PS: this solution will work on server. are you working on server or in localhost?
EDIT:
Other solution is to use below function to get proper directory structure:
$dirpath = public_path('picture/banner/');
When I am uploading an image to the form and returning it from the controller, the image name and extention are changing.I am a beggar, so if I make a mistake while asking a question, I would like to apologize.
This is my form:
<form action="{{ route('admin.slider.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="col-md-12">
<label class="control-label">Image</label>
<input type="file" name="image">
</div>
</div
<button type="submit" class="btn btn-success">Save</button>
</form>
This is my controller:
public function store(Request $request)
{
$this->validate($request, [
'image' => 'required|mimes:jpeg,bmp,png,jpg',
]);
$image = $request->file('image');
return $image;
}
My image file name is :demo.jpg
Controller return result is like that:
C:\xampp\tmp\php5E86.tmp
This is the same result when I give another picture, only the last four characters are changing.
C:\xampp\tmp\phpF239.tmp
It is very helpful to know why I am getting .tmp file return.
use getClientOriginalName to get orginal file name
$request->image->getClientOriginalName()
To get file extension
$request->image->extension();
or
$name = $request->file('image')->getClientOriginalName();
$extension = $request->file('image')->extension();
Ref:https://laravel.com/docs/8.x/filesystem#other-uploaded-file-information
I've tried uploaded an image, but the only thing that is saved is picture inside the local folder. I've tried many ways such as using the save method, but everything is not working for me.
It's appearing blank in the database table. When I dd($request0>all()), the name of the file did appeared, but after I dd($funds). Somehow the image attribute is gone.
Controller
public function store(Request $request)
{
// Validate funds form data
$validated = $request->validate([
'title' => 'required|string|unique:funds|min:5|max:100',
'content' => 'required|string|min:5|max:2000',
'image' => 'required|image|max:2048',
]);
$filename = $request->image->getClientOriginalName();
$validated['image'] = $request->image->storeAs('images', $filename, 'public');
// Create slug from title
$validated['slug'] = Str::slug($validated['title'], '-');
// Create and save funds with validated data
$funds = Fund::create($validated);
// Redirect the user to the created funds with a success notification
return redirect(route('funds.index', [$funds->slug]))->with('success', 'Post created!');
}
Blade
<form method="post" action="{{ route('funds.store') }}" enctype="multipart/form-data">
#csrf
#include('partials.errors')
<div class="field">
<label class="label">Image</label>
<div class="control">
<div class="col-md-6 mx-auto">
<img src="#" alt="image" id="img"
style="max-width:150px; display: block; margin-left: auto; margin-right: auto; padding-bottom: 10px; width: 60%;">
<input type="file" id="upload" name="image">
</div>
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-link is-
outlined">Publish</button>
</div>
</div>
</form>
Unless the table's column that's supposed to store the image is of the binary data type, it'll never show up in the database.
You didn't show your table's schema, so I can only guess here. If the image is being uploaded and stored to your storage directory, I recommend you save the file path to the database after it's done uploading (it's a string). Alternatively, you could just base64_encode the image and store it on that column, so that it can be displayed with an <img> element later.
Have a look at this part of the official documentation.
$path = Storage::disk('public')->put($fileName, $path);
Then save the $path value in database for the imagePath field.
You can specify drive. Public is the one accessible to all users once you run php artisan storage:link command in your terminal. Local disk is everything in private "/storage/app/private/.....".
Also for the slugs have a look at at this package. It will generate slugs automatically if you install and configure it properly in your specific models.
I want to upload file in my app.
This is the blade file .
<form action="/fileUploader " files="true" method="post" role="form" name="file" id="chan" >
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="panel panel-default">
<label>Please Select a File to Upload</label>
<input type="image">
<button type="submit" name ="Upload_File">Upload File</button>
</div>
</form>
This is my controller file
public function viewFile()
{
return View::make('/fileUploader');
}
public function showfileupload(Request $request)
{
$file = $request -> file('image');
// show the file name
echo 'File Name : '.$file->getClientOriginalName();
echo '<br>';
// show file extensions
echo 'File Extensions : '.$file->getClientOriginalExtension();
echo '<br>';
// show file path
echo 'File Path : '.$file->getRealPath();
echo '<br>';
// show file size
echo 'File Size : '.$file->getSize();
echo '<br>';
// show file mime type
echo 'File Mime Type : '.$file->getMimeType();
echo '<br>';
// move uploaded File
$destinationPath = 'uploads';
$file->move($destinationPath,$file->getClientOriginalName());
}
This is the web.php file
Route::get('/fileUploader', 'channelController#viewFile');
Route::post('/fileUploader', 'channelController#showfileupload');
I'm getting an error called FatalThrowableError in channelController.php line 48:
Call to a member function getClientOriginalName() on null.
How can I solve this problem
Most likely, you are trying to call a method - getClientOriginalName() - on a object that doesn't exist, so it's null. That jives with the error message you are seeing.
I'm not sure why, but we can start working backwards. Let's use an if statement with the hasFile() method to verify that a file is actually present on the request before attempting to move() it.
if ($request->hasFile('image')) {
$file->move($destinationPath,$file->getClientOriginalName());
}
If you implement the above, does the error still exist?
Here are the Laravel 5.3 Docs on file uploads. It may give you some more ideas.
If you're finding that users are posting files and hasFile() is still returning boolean FALSE, then you may want to go digging into the php.ini file and take a look at the Post_max_size or upload_max_size values to make sure that we aren't blocking large uploads.
VIEW
{!! Form::open(['route'=>'fileUploader', 'id'=>'chan', 'files' => true] )!!}
<div class="panel panel-default">
<label>Please Select a File to Upload</label>
<input type="file" name="image">
<button type="submit">Upload File</button>
</div>
{!! Form::close()!!}
ROUTES
Route::get('/fileUploader', 'channelController#viewFile');
Route::post('fileUploader', array(
'as' => 'fileUploader',
'uses' => 'channelController#showfileupload',
));
CONTROLLER
public function showfileupload(Request $request){
$file = $request -> file('image');
dd($file); // This work well for me and return information about the image
}
Do copy and past! i hope it work, let me know any error and result!
i am need hint link to download upload file, currently i am following this answer
here is my code
Routes file :
Route::get('getDownload/{remind_letter}',
['as'=>'downloadData',
'uses'=>'DockController#getDownload']);
Controller File :
public function getDownload($remind_letter)
{
$download = Dock::where('remind_letter','=',$remind_letter)->firstOrFail();
$file =Storage::disk('local')->get($download->remind_letter);
return (new response($file))->header('Content-Type', $entry->m1);
}
result file
<div class="row">
<div class="form-group">
<div class="col-xs-5">
<label class="col-sm-7">Remind Letter :</label>
<input type="text" name="remind_letter" value="{{$getData->remind_letter}}">
</div>
<div><a href="{{ route('downloadData',$getData->remind_letter) }}">
<button class="btn btn-success btn-sm">Download</button></a></div>
</div>
</div>
and the result link dock.start/star/getDownload/home/vagrant/Code/dock/storage/app/25/JTK/2015/Renewal License Kaspersky/Moonarch Security/tmp/phpQaGzoD.pdf
but the link is always show up error 404 and i can't download file
FYI when upload file i am using storage_path() function
it fix, i am wrong logic, when upload file it should using getFilename() function, $entry->filename = $filename->getFilename().$extension;