Using the below code, it should upload to the path specified in $path but for some reason, it saves the links to all pictures to c:\xampp\tmp with the .tmp extension but will move them to the correct folder anyway. What have I done wrong?
public function store(Request $request){
//
if($file = $request->file('image')){
$name = $file->getClientOriginalName();
$path = 'public/images';
if($file->move($path, $name)){
$post = new Gallery();
$post->image = $request->image;
$post->name = $request->name;
$post->species_id = $request->species_id;
$post->tag = $request->tag;
$post->patreon = $request->tag;
$post->save();
return redirect()->route('admin.gallery.index');
};
};
}
Related
I am coding a blog post, when i click add post the image from any folder is put into public/user/img/
How I delete it when i delete the whole table line? (click delete a post), please see the public function deletePost($id) below, I can delete the details (text) in a row but cannot delete the file in the folder.
Please help
public function deletePost($id){
$posts = Post::where('id',$id);
$posts->delete();
return back()->with('delete_posts_success','Deleted!');
}
public function getAddPost() {
return view('admin.publish');
}
public function postAddPost(Request $request) {
$post = $request->all();
$post = new Post;
$post->title = $request->title;
$post->author = $request->author;
$post->content = $request->content;
$post->intro = $request->intro;
$post->type = $request->type;
if($request->hasFile('image')) {
$file = $request->file('image');
$extension = $file->getClientOriginalExtension();
if($extension != 'jpg' && $extension != 'png' && $extension != 'jpeg') {
return back()->with('Error', 'File extension must be jpg, png, jpeg');
}
$imageName = $file->getClientOriginalName();
$file->move("user/img", $imageName);
$post->image = $imageName;
} else {
$imageName = null;
}
$post->save();
return back()->with('create_posts_success','Published!');
}
you must be saving filename in database somewhere for this uploaded post
now when you delete that post,
-> get the name of the file from your db record,
-> check if the file exists at the location using file_exists()
-> delete the found file using unlink()
-> get file deletion response (true|false)
in your case, it becomes something like below
$filePathName = 'user/img/' . $post->image;
if( file_exists($filePathName) ){
unlink($filePathName);
}
ignore my syntax mistakes please
Done
I want to edit the the blog form in Laravel. All other text information like Title, Body are successfully edited. But Image could not be updated. New image is not uploaded and image path is set as C:\xampp\tmp\php2030.tmp.
My Controller for edit.
public function update(Request $request, $id)
{
$requestData = $request->all();
$post = Post::findOrFail($id);
$post->update($requestData);
if ($request->hasFile('image'))
{
$file = $request->file('image');
$fileNameExt = $request->file('image')->getClientOriginalName();
$fileNameForm = str_replace(' ', '_', $fileNameExt);
$fileName = pathinfo($fileNameForm, PATHINFO_FILENAME);
$fileExt = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $fileName.'_'.time().'.'.$fileExt;
$pathToStore = public_path('media');
Image::make($file)->resize(600, 531)->save($pathToStore . DIRECTORY_SEPARATOR. $fileNameToStore);
$image = '/images/'.$fileNameToStore;
$post->save();
}
session()->flash('message', 'Successfully updated the post');
return redirect('/');
}
What is wrong with it?
When PHP receives a file upload, by default it writes it to a temporary directory like you're getting, and automatically deletes the file after the request has been handled.
What you need to do is move the uploaded file to a safe location.
Laravel 5.5 has a store method for file uploads that might be of interest.
public function update(Request $request, $id)
{
$requestData = $request->all();
$post = Post::findOrFail($id);
if ($request->hasFile('image')) {
$file = $request->file('image');
$fileNameExt = $request->file('image')->getClientOriginalName();
$fileNameForm = str_replace(' ', '_', $fileNameExt);
$fileName = pathinfo($fileNameForm, PATHINFO_FILENAME);
$fileExt = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $fileName.'_'.time().'.'.$fileExt;
$pathToStore = public_path('media');
Image::make($file)->resize(600, 531)->save($pathToStore . DIRECTORY_SEPARATOR. $fileNameToStore);
// UPDATE TEMPORARY IMAGE PATH WITH ACTUAL PATH
$requestData['image'] = "/media/{$fileNameToStore}";
}
$post->update($requestData);
session()->flash('message', 'Successfully updated the post');
return redirect('/');
}
Please, use the code below:
public function update(Request $request, $id)
{
$requestData = $request->all();
$post = Post::findOrFail($id);
$pathToStore = public_path('media');
if ($request->hasFile('image'))
{
$file = $request->file('image');
$rules = array('file' => 'required|mimes:png,gif,jpeg'); // 'required|mimes:png,gif,jpeg,txt,pdf,doc'
$validator = \Illuminate\Support\Facades\Validator::make(array('file'=> $file), $rules);
if($validator->passes())
{
$filename = $file->getClientOriginalName();
$extension = $file -> getClientOriginalExtension();
$picture = sha1($filename . time()) . '.' . $extension;
$upload_success = $file->move($pathToStore, $picture);
if($upload_success)
{
//if success, create thumb
$image = Image::make(sprintf($pathToStore.'/%s', $picture))->resize(600, 531)->save($pathToStore.'/thumb/'.$picture);
}
}
$requestData['image'] = "$pathToStore/{$picture}";
}
$post->update($requestData);
session()->flash('message', 'Successfully updated the post');
return redirect('/');
}
Hi please help me am new to laravel
I want to store multiple images in table... With this code am unable to save.
Help me for this..
Here in my View
{{Form::open(array('url'=>'businessdirectory/business', 'files'=>true))}}
{{Form::label('image','Upload Image')}}
<div class="form-group">{{Form::file('image[]',array('multiple'=>true))}}
</div>
{{Form::close()}}
In my Controller
if(Input::file('image'))
{
$image = Input::file('image');
foreach($image as $img) {
$destination = 'images';
$filename = $img->getClientOriginalName();
$path = 'images/'.$filename;
$uploadSuccess = $img->move($destination,$filename);
}
}
else
{
$path='images/default.JPG';
}
$business = new Business();
$business->image = $path;
It's not advisable to store images on database. Just save the path of the images instead.
If you really need to store image on db. Make sure you set the column to blob as it need more space. Then, get the image content and type, then save it.
<?php
$image = fopen($image_path, 'rb');
// or
$image = file_get_contents($image_path);
$business = new Business;
$business->image = $image;
$business->imageType = "image/gif"; //
$business->save();
// ...
You need to put below code inside the foreach loop. In every loop you need to insert image path inside a database
$business = new Business();
$business->image = $path;
This is a working code in laravel 5.2.
$files = $request->file('file');
if($request->hasfile('file')){
$destinationPath = 'uploads';
foreach($files as $file){
$image = new Image;
$filename = $file->getClientOriginalName();
$image->name = $filename;
$image->save();
$file->move($destinationPath,$filename);
}
I am trying to upload an image using Laravel it is working to the point that the images are being successfully placed into the destination folder but when I try to view them within my IDE I am told they are corrupt and that netbeans cannot open them.
I am wondering if my upload process is causing this?
This is my code:
public function handleCreate(){
$book = new Book;
$book->title = Input::get('title');
$book->desc = Input::get('desc');
//Img uploading...
$destinationPath = '';
$filename = '';
if(Input::hasFile('cover')){
$file = Input::file('cover');
$destinationPath = public_path().'/img/';
$filename = str_random(6) . "_" . $file->getClientOriginalName();
$uploadSuccess = $file->move($destinationPath, $filename);
}
$book->cover = $filename;
$book->save();
return Redirect::route('books')->with('msg', 'test');
}
Any help is much appreciated. I know the image is not corrupt before uploading as I can view on my desktop. The issue is only after uploading it.
I created a Forum which should upload an image.
In my form i`ve
{{ Form::file('image') }}
This is a part of my controller:
public function store()
{
$input = Input::all();
$v = Validator::make($input, Post::$rules);
if ($v->passes()) {
$post = new Post;
$post->title = Input::get('title');
$post->body = Input::get('body');
$post->image = Input::file('image'); // your file upload input field in the form should be named 'file'
$destinationPath = 'uploads/'.str_random(8);
$filename = $post->image->getClientOriginalName();
$extension =$post->image->getClientOriginalExtension(); //if you need extension of the file
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
$post->m_keyw = Input::get('m_keyw');
$post->m_desc = Input::get('m_desc');
$post->slug = Str::slug(Input::get('title'));
$post->user_id = Auth::user()->id;
$post->save();
return Redirect::route('posts.index');
}
return Redirect::back()->withErrors($v);
}
But laravel stores the image as a .tmp file in my database.
The path in my database is then "/uploads/xxxxx.tmp"
Why does laravel stores the image as .tmp and not as .img ?
What do i wrong and why does laravel stores the image as a .tmp file ?
The problem is in this line
$post->image = Input::file('image');
You assign the .temp image file to your model instance and that's what is stored in the database.
You can do it this way.
$post = new Post;
$post->title = Input::get('title');
$post->body = Input::get('body');
$file = Input::file('image');
$filename = $file->getClientOriginalName();
$destinationPath = 'uploads/'.str_random(8);
// This will store only the filename. Update with full path if you like
$post->image = $filename;
$uploadSuccess = $file->move($destinationPath, $filename);
I solved this problem by deleting the 'image' column in my $fillable variable. In Model Post.php
protected $table = "posts";
protected $fillable = [
'title',
'image', //delete if exists
'content'
];
.tmp file is the file which you select from your local computer. So you need to assign correct path to your model to store it with correct URL.