Upload a picture to database with laravel - database

i ve created a database in localhost and table in it called gallery. I want to upload and store pictures there but i m stuck.. Can you please give me any guides or tutorial how to do it? Thanks.

upload file just refer to laravel document
for example photo is the name fo file in form
<form action="xxx" method="post" enctype="multipart/form-data">
<input type="file" name="photo" />
<input type="submit" value="update" />
</form>
store the file then return file's path in filesystem
$path = $request->photo->storeAs('images', 'filename.jpg');
$path = $request->photo->storeAs('images', 'filename.jpg', 's3');
then store the $path to your database

First you need the form on your view (don't forget the csrf token):
<form action="/image-upload" method="POST" enctype="multipart/form-data">
#csrf
<input type="file" name="image">
<button type="submit">Upload</button>
</form>
And on your routes file add the route for POST method:
Route::post('image-upload', 'ImageUploadController#imageUploadPost');
Then on your Controller create the function that will validate and
move your image to the 'public/images' folder.
public function imageUploadPost()
{
request()->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$imageName = time().'.'.request()->image->getClientOriginalExtension();
request()->image->move(public_path('images'), $imageName);
}

Related

upload files in angular with data

I am new in angular and I have an issue with uploading files from different inputs. All the examples I have seen was about uploading files only from one input.
This is what I need for example :
<form>
<div>
<lable>type file A</lable>
<input type='text'>
<input type='file'>
</div>
<div>
<lable>type file B</lable>
<input type='text'>
<input type='file'>
</div>
<button type='submit'>submit</button>
</form>
In my API I want to get the file type A the file type B and the files A and B.
I do not need the API code but I do need to see how to do it in angular.

Upload files with Angular with a post containing other form data

I have followed countless examples (from here and other sites) that explain how you upload files from Angular to a web server. I am happy with the solution of using angular-file-upload and processing the data on the server (Node) with Multer.
What I haven't been able to find is a way to upload files from the form with a post that contains all the other controller data.
controller:
$scope.files = [];
$scope.name = "";
$scope.post = //$http post to server from service
view:
<input type="text" ng-model="name">
<input type="file">
<button ng-click="post()">Send post without page refresh</button>
Is there a way I can send the [name] and the [files] in the same post? If I send with multi part data will that be ok for [name] and [files]? Do I need to send two separate posts?
At the moment, my working example submits with a form action of 'post' and an enctype of "multipart/form-data". But I don't want the page to refresh and I want to send [name] and [files] from the scope... do I need to attach the files from the form to the scope or get the scope to pull the files from the DOM?
You can push formData to file before upload.
$scope.uploader.onBeforeUploadItem = function(fileItem) {
fileItem.formData.push({name: $scope.name});
};
Please look through the demo for complete solution :)
Plunkr
<form>
<input type="text" placeholder="Enter Name here" ng-model="newCountry.Name" />
<label>Choose 1 or more files:</label>
<input type="file" ng-files="AddnewCountryFlag($files)" multiple />
<h3>Try Uploading Image file. It will preview your image.</h3>
<div ng-repeat="item in imagesrc">
<img src="{{item.Src}}" />
<label>{{item.Size}}kB</label>
</div>
<input type="submit" value="Send Data" ng-click="AddCountry()" />

Pass a picture from Angualrjs front-end to Laravel back-end

I'm building an app in which I need to upload an image with angularjs, pass it with other data to a save() method written in laravel, what is the simplest way to do it?
in my html I have a title field and an image uploader:
<form>
<input type="text" required ng-model="selectedAD.title">
<input type="file" name="file" ngf-max-size="2MB" required ng-model="selectedAD.image"/>
<a class="btn-portal" type="submit" ng-click="saveAD()">
</form>
and inside my js file, I pass these data into the save route which goes to the store() function in laravel and moves the image into a folder, like this:
$scope.selectedAD = ad;
$scope.saveAD = function(){
$scope.managementErrors = [];
adsResource.save($scope.selectedAD,function(success){
$('#ads-modal').modal('hide');
showSuccessAlert($scope,$timeout, getSuccessMessages(success),$('#successMessages'));
$scope.getAdsTimeout();
},function(error){
$scope.managementErrors = getErrorMessagesUpdate(error);
});
this way the image is not passed to laravel processing code, it instead passes null to the store function, any helpful guides ?

Blobstore Together with Other Input in GAE

I understand that uploading blob in Google App Engine is something like this:
<%
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
%>
<form action="<%= blobstoreService.createUploadUrl("/upload") %>" method="post" enctype="multipart/form-data">
<input type="file" name="myFile">
<input type="submit" value="Submit">
</form>
Is it possible to save other input field? Let say we have other input in a form.
<input type="text" name="str" />
The answer is yes, it is possible. I'm successfully doing what you're asking about - uploading files and text on the same request in GAE. You just handle the request and its parameters in your request handler.

Name of a file in a POST request in Google AppEngine

Given the html form ...
<form id="mainform" action="/upload" enctype="multipart/form-data" method="post">
<div>
<input type="file" name="img"/>
</div>
<div>
<input type="submit" value="Send">
</div>
</form>
... and the handler ...
class Picture(db.Model):
image = db.BlobProperty()
class Submission(webapp.RequestHandler):
def post(self):
picture = Picture()
image = self.request.get("img")
picture.image = db.Blob(image)
picture.put()
self.redirect('/')
... is there any way within the handler to get the filename the user entered for the upload? In PHP, I can refer to $_FILES['img']['name'], but I do not see what syntax, if any, would work with request.get. In another question, another author uses a javascript routine in his html page to extract the filename the user chooses every time an OnChange event occurs, and then pass it separately in a hidden field. Is that necessary? PHP seems to get the filename for free.
I uncovered a solution in the documentation for the cgi module:
picture.local_filename = self.request.POST[u'img'].filename
The request's POST object is a collection of FieldStorage, and FieldStorage objects have a filename attribute.

Resources