Laravel s3 check if directory exists. - laravel

How would I check with Laravel storage if a directory exists?
In the documentation there is only documented how to check if a file exists.
Storage::disk('local')->exists('file.jpg');
https://laravel.com/docs/5.3/filesystem#retrieving-files

None of the answers here seems to answer specifically to check if a particular folder exists in S3. This is what I use and it works:
Storage::disk('s3')->exists('FOLDER_NAME')
The above will return a boolean. The above check works for both folders and filenames.

In Laravel 5.7 use:
The has() method is used to determine if a given file exists on the disk:
$exists = Storage::disk('s3')->has('file.jpg');

Try to get directories list in a parent directory and check if the directory you're looking for is in an array:
in_array('directoryYoureLookingFor', Storage::disk('s3')->directories('parentDirectory'));
I've just checked and this solution works in Laravel 5.3.

You can using isDirectory method:
if (File::isDirectory($filename))
{
echo "Yes. It's a directory.";
}

I have also faced same issue many time for check file is exists or not on S3 with Laravel. I am working on Laravel 7.* and exists() and has() functions not providing proper response when file is uploaded on sub directory of S3 bucket. I have write a function which is working perfectly for me.
public static function fileExistsS3($directoryOnS3, $image) {
$directoryOnS3 = Config::get('custom.AWS_PUBLIC_ENDPOINT').$directoryOnS3;
$exists = #getimagesize($directoryOnS3.$image);
if(isset($exists) && is_array($exists)) {
return true;
} else {
return false;
}
}
It will return true if file available on S3 else it will return false.

You can do the following thing i.e u can get the filename and compare it with the file name urtrying to upload if it exists give a maessage else u can insert
$file = Storage::disk('local')->get($filename);
if($file){
echo "file exist";
} else {
Storage::disk('local')->put($filename,FILE::get($file));
}

Related

Laravel 5.4 - File Storage returns false when checking for file existence

I am trying to see if a file exist in the storage folder. It returns false in the server even if the file exist. In my local environment, it is working perfect.
I am trying to store avatar for users. When I check if avatar exists, it returns false.
Store avatar:
$avatar = $request->file('avatar');
$user = Auth::user();
if($avatar && $file = $avatar->isValid()) {
$file = $request->file('avatar');
$path = $user->avatar_path;
$image = Image::make($file)->fit(100,100)->encode('jpg');
Storage::put($path, $image);
$url = Storage::url($path);
}
Checking for avatar existence
$user = Auth::user();
if ($user->has_avatar) {
Storage::delete($user->avatar_path);
}
has avatar method in user.php model
public function getHasAvatarAttribute()
{
return Storage::disk('public')->has('avatars/'.md5($this->id . $this->email).'.jpg');
}
Avatar path function
public function getAvatarPathAttribute()
{
return 'public/avatars/'. md5($this->id . $this->email) . '.jpg';
}
When I test on my machine, all is good. On server, it returns false even if avatar exists.
I can not comment Yet so I will leave this answer here. In production it is not working? As in a live server? Maybe linux server? And you are running the local development on Windows? See where I am going with this? Your path maybe wrong. I mean the naming. Even a to A or vice versa may effect the path. The same goes for your file naming. And path naming. avatar/something and /avatar/something are completely different.
Please let me know if that was the case.

getClientOriginalName() on null

I have a little problem with one request file in my Store controller !
I get the orginal file name extension like this :
$licencie_amateur->cert_medical_surclassement = $request->file('cert_medical_surclassement')->getClientOriginalName();
Sometimes the user don't need to put a file "cert_medical_surclassement" i would like to do a condition like : If the file was added ok but if there is no files in the field i pass to the other variable .
Someone know how i could do this ? thanks a lot in advance
Use the hasFile() method
if ($request->hasFile('cert_medical_surclassement')) {
$licencie_amateur->cert_medical_surclassement = $request->file('cert_medical_surclassement')->getClientOriginalName();
} else {
// else code
}
More info: Documentation

Checking folder already existed and if not create a new folder by id in laravel

i want to store my image to a specific folder and the folder will named by page id. For example if Page id=1, the folder location should be public/pages/page_id/image_here.
If the folder are not existed yet the system will generate and named with their page id.
$id=1;
$directoryPath=public_path('pages/' . $id);
if(File::isDirectory($directoryPath)){
//Perform storing
} else {
Storage::makeDirectory($directoryPath);
//Perform storing
}
But i having error that "mkdir(): Invalid argument".
Can i know why it happen?
And after my research some people say the folder name should based with id+token so intruder cannot search image based on id, is there possible to achieve?
I had the same problem, but when I use File instead of Storage it works!
$id=1;
$directoryPath=public_path('pages/'.$id);
//check if the directory exists
if(!File::isDirectory($directoryPath)){
//make the directory because it doesn't exists
File::makeDirectory($directoryPath);
}
//Perform store of the file
Hope this works!
When you use Storage facade, it uses local disk as default which is configured to work with storage_path().
So, if you want to create directory in public directory, use File::makeDirectory which is just simple wrapper for mkdir() with additional options or use mkdir() directly:
File::makeDirectory($directoryPath);
mkdir($directoryPath);
For basic Laravel file system the syntax is :
At very top under namespace write:
use File;
Then in your method use:
$id=1;
$directoryPath=public_path('pages/' . $id);
if(File::isDirectory($directoryPath)){
//Perform storing
} else {
File::makeDirectory($directoryPath, 0777, true, true);
//Perform storing
}

Can't write image data to path in laravel

I am having the same error as this guy is :
Another thread
basically the error i have is uploading the image to the specific path, my code is as follows :
public function postCreate() {
$validator = Validator::make(Input::all() , Product::$rules);
if ($validator->passes()) {
$product = new Product;
$product->category_id = Input::get('category_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->price = Input::get('price');
$image = Input::file('image');
$filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products'.$filename);
$product->image = 'public/img/products'.$filename;
$product->save();
return Redirect::to('admin/products/index')
->with('message' , 'Product created');
}
return Redirect::to('admin/products/index')->with('message' , 'something went wrong')
->withErrors($validator)->withInput();
}
I was just trying to follow a tutorial on laravel e-commerce web application.
I guess the problem is that i don't have write permisson in my directory , how do i add write permission in my directory. I.E. the public folder, I googled a few places , but i don't understand what is it that i have to edit ?
I.E the htaccesss file or can i make write changes on the cmd ? also how do i check what weather a directory is write protected .
PS. i am using windows . i am attaching a screenshot of the error .
Thank you.
You might want to change the dateformat since windows doesn't allow colons in filenames:
$filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
And you also might want to add a trailing slash to your path so it doesn't concatenate the filename to the folder path:
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products'.$filename);
Generally this error occurs when you do not yet have the directory that will store the image inside the public directory. Sometimes it can be a permission issue.
Does you img directory exists in your public directory?
To fix this, follow the steps:
Use this snippet:
$relPath = 'img/'; //your path inside public directory
if (!file_exists(public_path($relPath))) { //Verify if the directory exists
mkdir(public_path($relPath), 666, true); //create it if do not exists
}
Or manually create the img directory in public
2.Then you can save your image:
Image::make($image)->resize(468, 249)->save(public_path('img/products'.$filename)); //save you image
$product->image = 'img/products'.$filename; //note
$product->save();
**NOTE: We do not need to specify the public directory in the path because we are using a relative path. The img directory will be created inside public directory.
Along with this, you need to make sure the folder path exists and which has right permissions set.
$relPath = 'img/product/';
if (!file_exists(public_path($relPath))) {
mkdir(public_path($relPath), 777, true);
}
Where $relPath is the path relative to public directory.
This requirement is however windows specific. In linux, folder directory will be created if it does not exist.
I also recommend all of you to check if $path exists. Like Jose Seie use native PHP check, I recommend you to thought about build-in helpers.
This can be achieved with File Facade helper:
File::exists($imagePath) or File::makeDirectory($imagePath, 777, true);
Advice you to use Laravel built-in functions, classes & helpers to improve the performance of your application!
well , i made the correction that john suggested and then made the following corrections :
I replaced the below code :
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products'.$filename);
with :
$path = public_path('img/products/'.$filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
problem solved , i don't know why public_path works , but never mind .

codeigniter output cache doesn't work?

I'm using $this->output->cache(n) to cache webpage, but i cannot figure out how does it work.. I didn't find any cache files under system/cache folder...and also after I edit the page and show it again, the content changes, so it seems that the page is not really cached. Can anyone give a help? (i'm using phil's template lib)
my code:
function show(){
$this->output->cache(5);
$this->load->model('page_model');
$var = $this->uri->segment(3, 0); //get About page
$row = $this->page_model->getPage($var);
$this->template->title('about')
->set_layout('default')
->set_partial('styles', 'css')
->set('data', $row->body)
->build('about');
}
THANKS!
Two things, as outlined in the documentation:
Warning: Because of the way CodeIgniter stores content for output, caching will only work if you are generating display for your controller with a view.
Perhaps not using the "native" views is an issue?
Additionally:
Note: Before the cache files can be written you must set the file permissions on your application/cache folder such that it is writable.
Are you sure your application/cache directory has the correct permissions?
Debug this file and check it's actually writing the cache:
system/core/Output.php
// Do we need to write a cache file? Only if the controller does not have its
// own _output() method and we are not dealing with a cache file, which we
// can determine by the existence of the $CI object above
if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output'))
{
$this->_write_cache($output);
}
This works well for me:
function _output($content) {
// Load the base template with output content available as $content
$data['content'] = &$content;
$this->output->cache(600);
$output = $this->load->view('base', $data, true);
$this->output->_write_cache($output);
echo $output;
}
Are you sure your application/cache directory has the correct permissions?
you you to directories application/cache in cpanel , permissions become 777 is OK

Resources