Laravel can't found a stored file 404 NOT FOUND - php

posting this again because I didn't find to solution yet.
Laravel can't found the file storage/app/public/upload
when I usehttp://127.0.0.1:8000/storage/upload/The_fileName.x I get 404 not found
I've tried http://127.0.0.1:8000/storage/app/public/upload/The_fileName.x too.
what should I do ?
In DocumentController :
public function store(Request $request)
{
$request->validate([
'doc' => "required",...
]);
$document = new document();
$file = $request->file('doc');
$filename=time().'.'.$file->getClientOriginalExtension() ;
// I've tried these too, one by one and still get the same error .
//$file_path = public_path('public/upload');
//$file->move($file_path, $filename);
//Storage::disk('local')->put($file, $filename);
//request('doc')->store('upload', 'public');
$file->storeAs('public/upload', $filename);
$document->doc = $request->input('doc', $filename);
$document->candidate_id = $candidate_id;
$document->save();
Thank you in advance

According to Laravel document File Storage, you need to create a symbolic link at public/storage which points to the storage/app/public then you can access the file with http://127.0.0.1:8000/upload/The_fileName.x.

Related

Laravel saved file/image in shared hosting got wrong directory

I have a function to upload files. on Localhost it's not having an error. but after I deploy on shared hosting, its have a problem. If localhost, I'm not moving some folder, but on "tutorial" shared hosting, I need to make 2 folders. Laravel and public. this Laravel folder is all file on project Laravel without public
It's my schema on my shared hosting
(/home/sippausr)
etc
Laravel ->
app
bootstrap
config
database
public_html
files ( this file saved here)
resources
routes
storage
tests
vendor
logs
mail
public_ftp
public_html ->
css
files (not on here, i need to save here)
home
images
js
kalibrasi
public
sop
theme
And I have a function to upload a file and saved this file to directory files on public_html like this
public function store6(Request $request) {
$this->validate($request, [
]);
if($request->hasfile('image')) {
$file = $request->file('image');
$name=$file->getClientOriginalName();
$file->move(public_path().'/files/', $name);
$data = $name;
}
$user = new pemeliharaan;
$id = Auth::user()->id;
$user->user_id = $id;
$user->alat_id = $request->alat_id;
$user->pertanyaan =json_encode($request->except
(['_token','name','alat_id','status','catatan','image']));
$user->catatan = $request->catatan;
$user->image=$data;
$user->status = $request->status;
$user->save();
// dd($user);
return redirect('user/show6')->with('success', 'Data Telah Terinput');
}
But, this file not saved at public_html/files,
This file saved in the Laravel folder, on public_html( you can see at schema). Can someone help me?
Use the below code to save your file to your files directory in your public folder.
use Illuminate\Support\Facades\Storage;
class YourClassName implements Contract {
public function store6(Request $request) {
$this->validate($request, [
]);
if($request->hasfile('image')) {
$file = $request->file('image');
$name=$file->getClientOriginalName();
Storage::putFileAs('public/files', $file, $name);
$data = $name;
}
$user = new pemeliharaan;
$id = Auth::user()->id;
$user->user_id = $id;
$user->alat_id = $request->alat_id;
$user->pertanyaan =json_encode($request->except
(['_token','name','alat_id','status','catatan','image']));
$user->catatan = $request->catatan;
$user->image=$data;
$user->status = $request->status;
$user->save();
return redirect('user/show6')->with('success', 'Data Telah Terinput');
}
}

Creating default object from empty value on update Laravel

I realize this question has been asked several times on this forum, but none of the solutions given has resolved my particular issue.
I get the above error when trying to update my DB.
Here is a snippet of my code
I have tried adding findorfail on my request by id
$service=Service::findorfail($request->input('id'));
This returns a 404 page.
ServiceController.php
public function updateService(Request $request){
// return $request->all();
$this->validate($request, ['serviceTitle'=> 'required',
'serviceSubTitle'=> 'required',
'description'=> 'required',
'slug' => 'required|min:3|max:255|unique:services',
'serviceImage'=>'image|nullable|max:1999']);
$service=Service::find($request->input('id'));
$service->title =$request->input('serviceTitle');
$service->sub_title =$request->input('serviceSubTitle');
$service->slug =$request->input('slug');
$service->description =$request->input('description');
if($request->hasFile('serviceImage')) {
//1 : get filename with ext
$fileNameWithExt = $request->file('serviceImage')->getClientOriginalName();
//2 : get just file name
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
//3 : get just extension
$extension = $request->file('serviceImage')->getClientOriginalExtension();
//4: filename to store
$fileNameToStore = $fileName.'_'.time().'.'.$extension;
//upload image
$path =$request->file('serviceImage')->storeAs('public/serviceImage', $fileNameToStore);
$old_image =Service::find($request->input('id'));
if($old_image!='noimage.jpg') {
Storage::delete('public/serviceImage/'.$old_image->image);
}
$service->image =$fileNameToStore;
}
$service->update();
return redirect('/services')->with('status', 'The '.$service->title.' Service has been updated successfully');
}
The error log tells me the error is on this specific line:
$service->title =$request->input('serviceTitle');
Thank you for time and assistance.

file_put_contents to create file out of public directory in laravel

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

Retrieving multiple files and display in Vue component

I am processing files by extension and storing images, files, and pdf's inside of respective folders located in the public storage.
Php artisan link:storage has already been run. Files are being properly sorted and stored in the correct locations.
The portion of documentation that says to create the URL link using "echo asset()" is a bit unclear to me and I believe that's where the issue lies. Where am I supposed to echo the asset? I was unable to do so in the vue component.
However, on the front end (Vue Component using Axios to pass data) it seems that the :src is only getting the file path stored in the Database and not grabbing the actual file from the storage location.
Here is the portion of the component where the image should display:
<div v-for="image in post.images" :key="image.id">
<img width="220" height="250" :src=" './storage' + image.post_image_path " />
</div>
Here is my Controller function that stores the images:
public function store(Request $request)
{
// get the data from vue component
$title = $request->title;
$description = $request->description;
$author_id = Auth::user()->id;
$images = $request->images;
// creating the new post with the data above
$post = Post::create( [
'title' => $title,
'description' => $description,
'author_id' => $author_id
]);
// we are receiving an array in the image, we need to do a foreach to grab the files that the image has
foreach($images as $image) {
$name = $image->getClientOriginalName();
$ext = $image->getclientoriginalextension();
// creating the extension so we can filter the query above
$ext_images = array('jpg', 'png');
$ext_files = array('pdf', 'doc', 'docx','txt');
$ext_videos = array('mp4', 'mov', 'avi');
// we are proccesing the images files
if (in_array($ext, $ext_images)) {
$imagePath = Storage::disk('local')->put('/post_images' , $image);
Image::create( [
'post_id' => $post->id,
'post_image_path' => '/local/' . $imagePath,
]);
}
// we are proccessing standard files and saving if
elseif (in_array($ext, $ext_files)) {
$imagePath = Storage::disk('local')->put( '/post_files' , $image);
Image::create( [
'post_id' => $post->id,
'post_image_path' => '/local/' . $imagePath,
]);
}
// we are processing the media files (video) and saving if
elseif (in_array($ext, $ext_videos)) {
$imagePath = Storage::disk('local')->put('/post_videos' , $image);
Image::create( [
'post_id' => $post->id,
'post_image_path' => '/local/' . $imagePath,
]);
};
}
return $post;
With the current controller set up files are organized into their respective folders and storing in the "storage/app/specified folder name". This is working correctly, the only issue retrieving the records after they have been created.
PARTIALLY RESOLVED:
By changing the storage location to:
$image->move(public_path('post_images'), $name);
instead of
$imagePath = Storage::disk('local')->put('/post_images' , $image);
While I have a viable work around, understanding why the storage wasn't working would be greatly appreciated.
The asset helper method is going to return to full proper path of the image folder.
Since you are not calling the method (Or you can't since you are working in a vue component), you have to set the base of your path yourself and concat the image name.
By default Laravel's public directory is the entry point, so this should work, if the image is placed properly:
<img width="220" height="250" :src=" '/storage' + image.post_image_path" />
Saving the image like:
$image->move(public_path('post_images'), $name);
resolved the issue.

Laravel get path of saved file from upload

I have a laravel upload for file (along with other data that is passed to the database) Everything works. But I just can't figure out how to save the path of the file that is saved.
Here is my controller function:
public function store(Request $request)
{
request()->validate([
'name' => 'required',
'logo' => 'nullable',
'original_filename' => 'nullable',
]);
//This is where the file uploads?
if ($request->hasFile('logo')) {
$request->file('logo')->store('carrier_logo');
$request->merge([
'logo' => '',//TODO: get file location
'original_filename' => $request->file('logo')->getClientOriginalName(),
]);
}
Carrier::create($request->all());
return redirect()->route('carriers.index')->with('toast', 'Carrier created successfully.');
}
The thing I want to achieve:
I want logo to fill with something like carrier_logo/ZbCG0lnDkUiN690KEFpLrNcn2exPTB8mUdFDwAKN.png
The thing that happened every time I tried to fix it was that it placed the temp path in the database. Which ended up being something in the PHP install directory.
Just assign result to variable:
$path = $request->file('logo')->store('carrier_logo');
According to docs
Then you can do with $path variable whatever you want.
just assign the value like this.
$location=base_path('img/'.$filename);
and save it in db.
You could do this:
For FileName
$fileName = $request->file('test')->getClientOriginalName();
OR
$fileName = $request->user()->id.'.'.$request->file('logo')->getClientOriginalExtension();
$imageDirectory = 'logo_images';
$path = $request->file('logo')->storeAs($imageDirectory, $fileName);
dd($path);

Categories