move_uploaded_file is not fetching path in laravel - php

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/');

Related

Why doesn't my php laravel application store my image?

I've looked at code from other people and try to implement the same thing, but the application could not store the image to the desire folder, would you please have a look at what the reason is? Thank you!
Here is my code for route:
Route::post('upload_pic', 'UploadController#storePhoto');
Here is my code for the php laravel template:
<div class="panel-body">
<form action="{{url('/upload_pic')}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="upload-user-photo">Upload Photos</label>
<input type="file" name="image" class="form-control" placeholder="Upload Student Image, Size:207(W)x408(H)">
</div>
<div class="row">
<div class="col-sm-4">
<input class="btn btn-success" type="submit" value="Upload Student Photo" name="submit">
</div>
</div>
</form>
</div>
Here is my code in the controller:
public function storePhoto(Request $request){
$valid = $request->validate([
'image' => 'required|image|mimes:jpg,png,jpeg,|dimensions:max_width=272,max_height=408,min_width=271,min_height=407'
]);
$data = new Postimage();
$file = $request->file('image');
$filename = $file->getClientOriginalName();
// dd($filename);
$file -> move(public_path('./public/img/student_photos'),$filename);
$data['image'] = $filename;
$data->save();
return redirect('/upload')->with('success', 'Photo Uploaded');
}
Your problem is somewhere here
$filename = $file->getClientOriginalName();
$file -> move(public_path('./public/img/student_photos'),$filename);
First, the file comprises only the file extension. Instead, you should have something like this $filename = 'name.' . $file->getClientOriginalName(); Notice the dot in 'name.'
Secondly, no need to add public to the file path string. So it should be $file->move(public_path('img/student_photos'),$filename);
Finally, make sure the upload folder exists and is writeable

Get the actual file name and extention from image file in laravel

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

File not uploading

I was working on a project and was trying to develop a file uploading system for skins.
When I tried to upload my skin, I was given "Call to a member function storeAs() on null"
public function uploadSkin(Request $request)
{
/* $request->validate([
'skins' => 'required|mimes:png|max:1024',
]); */
$storage_dir = storage_path('app/skins');
$request->file('skins')->storeAs($storage_dir, Auth::user->name . '.png');
return route('settings')->with('success', 'skin uploaded :)');
}
Form code:
<form method="post" enctype="multipart/form-data" action="/settings">
#csrf
<br/>
<div class="form-group">
<input type="file" class="form-control-file" id="skins" name="skins" required>
</div>
<button type="submit" class="btn btn-success">Upload</button>
</form>
To store a file like an image or any kind of files you can use a code like this:
public function uploadSkin(Request $request){
$image = $request->file('skins');
if ($image != null) {
$image->move('uploads/skins/', Auth::user()->name . $image->getClientOriginalExtension());
}
return route('settings')->with('success', 'skin uploaded :)');
}
To uppload a file there are various ways in the laravel but for now you can try this to simply move your file to your directory:
if($files= $request->file('skins')){
$files->move('uploads/skins/', Auth::user->name . '.png');
}

How to download file from storage?

I'm using Laravel's file storage system and I'm trying to trigger a download response to download my files through the browser, but it cant find the correct file instead it downloads my file page view script. I have a storage link set up as well.
Any suggestions would be appreciated.
File.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<form action="{{route('upload')}}" method="POST"
enctype="multipart/form-data" name="formName">
{{csrf_field() }}
<input type="file" name="file">
<input type="submit" class="btn" name="submit">
</form>
<div class="row">
#foreach($files as $file)
<a href="{{route('download',$file)}}" download="{{$file->name}}">
{{$file->name}}</a>
</div>
</div>
#endsection
Download function
public function download($file){
return response()->download(storage_path('/storage/app/files/'.$file));
}
file routes
Route::get('files', 'FileController#index')->name('upload');
Route::post('files', 'FileController#store');
Route::get('files/{file}', 'FileController#download')->name('download');
Remove this download="{{$file->name}}" from the link.
You can add download as html attribute:
<a href="{!! route('download', $file->name) !!}" download>{{ $file->name }}</a>
But you don't need it in this case, use just:
{{$file->name}}
The response()->download() method in your controller will generate a response that forces the browser to download the file at the given path. So make sure your path i correct.
If your file is in your-project-root-path/storage/app/files/, you can use:
return response()->download(storage_path('/app/files/'. $file));
If your file is in your-project-root-path/storage/storage/app/files/, use:
return response()->download(storage_path('/storage/app/files/'. $file));
I think you are passing the file object instead of the filename to your download route.
Try
#extends('layouts.app')
#section('content')
<div class="container">
<form action="{{route('upload')}}" method="POST"
enctype="multipart/form-data" name="formName">
{{csrf_field() }}
<input type="file" name="file">
<input type="submit" class="btn" name="submit">
</form>
<div class="row">
#foreach($files as $file)
<a href="{{route('download',$file->name)}}" download>
{{$file->name}}</a>
</div>
</div>
#endsection
Try replacing {{route('download',$file)}} with {{route('download',$file->name)}}.
Also try replacing the download controller with this code
public function download($file){
return response()->download(storage_path('app/files/'.$file));
}
public function jpg_download($id)
{
if (auth()->user()->download_count < 5) {
auth()->user()->increment('download_count');
$data = DB::table('products')->where('id', $id)->first();
$path = public_path('/storage/item/jpg/' . $data->jpg);
return response()->download($path);
}
dd('Next Day Download');
}

Laravel 5.1 File Upload isValid() on string?

So I am making a function for file uploading in my project.
however, when I try it I get this error : Call to a member function isValid() on string
My code for the upload function :
public function upload(Request $request){
$file = array('profielfoto' => $request->input('profielfoto'));
$rules = array('profielfoto' => 'required',);
$validator = Validator::make($file,$rules);
if($validator->fails()){
return redirect('/profiel')->withInput()->withErrors($validator);
}
else{
if($request->input('profielfoto')->isValid()){ //<- gives error
$destinationPath = 'assets/uploads';
$extension = $request->input('profielfoto')->getClientOriginalExtension();
$fileName = rand(1111,9999).'.'.$extension;
$request->input('profielfoto')->move($destinationPath,$fileName);
Session::flash('alert-success', 'Foto uploaden gelukt');
return redirect('/profiel');
}
else{
Session::flash('alert-danger', 'Foto uploaden mislukt');
return redirect('/profiel');
}
}
}
The form in the blade view on the 4th line from down below is the location for the input!
<form method="POST" action="/profiel/upload" files="true">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="PUT">
<input type="hidden" class="form-control id2" id="id2" name="id" value="{{$user->id}}">
<img src="assets/images/avatar.png" alt="gfxuser" class="img-circle center-block">
<div class="form-group center-block">
<label class="center-block text-center" for="fotoinput">Kies uw foto</label>
<input class="center-block" type="file" name="profielfoto" id="profielfoto">
</div>
<button type="submit" class="btn btn-success"><span class="fa fa-check" aria-hidden="true"></span> Verander foto</button>
</form>
You must ask isValid() to a file, not to the name of the file. That's why you get the error. You can get the file through $request->file() or through Input::file() :
else{
if( $request->file('profielfoto')->isValid()){ //<- gives error
Also your form should include the correct enctype to send files:
<form enctype="multipart/form-data">
I think you should use as this.
$file = $request -> file('Filedata');
if (!$file -> isValid()) {
echo Protocol::ajaxModel('JSEND_ERROR', 'not an valid file.');
return;
}
Add attribute on
enctype="multipart/form-data"

Categories