Print File Info in Laravel - laravel

I want to print the list of all files along with the information about them like created, updated and size. Currently, I use
$files = File::allFiles('downloads');
But, this just gives me the filename. Is there a better way or is there any property for the same thing that I am missing?

As per the documentation. If you really want complete info for each file.
You can try something like this
$files = Request::allFiles()
$fullInfo = [];
foreach($files as $file)
{
$fullInfo[$file] = UploadedFile::createFromBase($file)
}
Ref: this line 429 to 455
Edit:
Sorry my bad even UploadedFile::createFromBase will return object
I think we don't have option in laravel to dump every properties of file. You can access each property by calling the functions like getClientOriginalName.
Or try native method $_FILES to
$files = $request->file('file');
foreach ($files as $file) {
$file_name = $file->getClientOriginalName();
}

Related

Sort storage files by modified date in Laravel

I'm building a personal blog using Laravel 8, where the articles are displayed from .md files (no database). Everything works perfectly, but I'm struggling to sort those .md files by the modified date or creation date. Files are placed into the storage folder
Current code:
// Getting files from storage:
$files = Storage::disk('articles')->allFiles();
$articles = [];
foreach ($files as $file) {
$storage = Storage::disk('articles')->get($file);
array_push($articles, $storage);
$collection = collect($articles);
}
$array = collect($articles);
$articles = $this->paginate($array);
return view('pages.home', compact('articles'));
I've tried to get file details using the filemtime() function, but I get the following error:
$filePath = Storage::path($file);
$fileDate = filemtime($filePath);
dd($fileDate);
ErrorException
filemtime(): stat failed for C:\my-project\storage\app\article-file.md
stat function returns the same error.
Storage is linked, file exists, it returns the right path...
I'm out of ideas...
Laravel already has method to read last modify file metadata
Storage::lastModified($name);
Link to documantation:
https://laravel.com/docs/8.x/filesystem#file-metadata

Laravel 5.3 import CSV from file without uploading

I am using Maatwebsite/Excel to import CSV files. My code looks like this tutorial: http://itsolutionstuff.com/post/laravel-5-import-export-to-excel-and-csv-using-maatwebsite-exampleexample.html
I marged both functions and my script gets file and returns file/
But despite many attempts I can't skip uploading file. It means, that I have CSV file in special folder, and I would like to get this file to process data, without uploading. It will automatically get file (maybe file_get_contents ?), and pass it to Illuminate\Support\Facades\Input.
This is the perfect way to import your csv in laravel 5.*
public function importExcel() {
if (Input::hasFile('import_file')) {
$path = Input::file('import_file')->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if (!empty($data) && $data->count()) {
$count = 0;
foreach ($data as $key => $d) {
$insert = array();
$insert['name'] = $value->name;
$this->user->create($insert);
}
}
}
}
You can read your excel file using this code
// for sheet 1
$excelData = $excel->selectSheetsByIndex(0)->load($yourFilePath)->get();
print_r($excelData);
I found solution. We can skip if (Input::hasFile('import_file')) { and $path variable set to our CSV file path

Laravel 4 get image from url

OK so when I want to upload an image. I usually do something like:
$file = Input::file('image');
$destinationPath = 'whereEver';
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
if( $uploadSuccess ) {
// save the url
}
This works fine when the user uploads the image. But how do I save an image from an URL???
If I try something like:
$url = 'http://www.whereEver.com/some/image';
$file = file_get_contents($url);
and then:
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
I get the following error:
Call to a member function move() on a non-object
So, how do I upload an image from a URL with laravel 4??
Amy help greatly appreciated.
I don't know if this will help you a lot but you might want to look at the Intervention Library. It's originally intended to be used as an image manipulation library but it provides saving image from url:
$image = Image::make('http://someurl.com/image.jpg')->save('/path/saveAsImageName.jpg');
$url = "http://example.com/123.jpg";
$url_arr = explode ('/', $url);
$ct = count($url_arr);
$name = $url_arr[$ct-1];
$name_div = explode('.', $name);
$ct_dot = count($name_div);
$img_type = $name_div[$ct_dot -1];
$destinationPath = public_path().'/img/'.$name;
file_put_contents($destinationPath, file_get_contents($url));
this will save the image to your /public/img, filename will be the original file name which is 123.jpg for the above case.
the get image name referred from here
Laravel's Input::file method is only used when you upload files by POST request I think. The error you get is because file_get_contents doesn't return you laravel's class. And you don't have to use move() method or it's analog, because the file you get from url isn't uploaded to your tmp folder.
Instead, I think you should use PHP upload an image file through url what is described here.
Like:
// Your file
$file = 'http://....';
// Open the file to get existing content
$data = file_get_contents($file);
// New file
$new = '/var/www/uploads/';
// Write the contents back to a new file
file_put_contents($new, $data);
I can't check it right now but it seems like not a bad solution. Just get data from url and then save it whereever you want

Get file-name in sub-folders along with its information in controller

I have a 'images' folder and inside it are subfolders which have images. What I want is the name all the images along with their path in an array.
I have written this in my controller:
$data['images_names'] = directory_map('../project/images/');
this is giving be image name, but not path. Can anyone tell me how can I achieve that?
Thanks in advance....
what about this ??
$files = dir_scan('/var/www/www/chat/*');
print_r($files);
function dir_scan($folder) {
$files = glob($folder);
foreach ($files as $f) {
if (is_dir($f)) {
$files = array_merge($files, dir_scan($f . '/*')); // scan subfolder
}
}
return $files;
}
from
PHP get path to every file in folder/subfolder into array?

Codeigniter multifile upload

$this->upload->display_errors()
shows the errors for all the files I am trying to upload.
How can I show the error for each file field ?
for example, if the user can upload 5 files and the third file is to big, I want to be able to tell him that this specific file is to big.
The functionality you are after is not built-in to the CI upload library. Here's CI's $this->upload->display_errors() method:
function display_errors($open = '<p>', $close = '</p>')
{
$str = '';
foreach ($this->error_msg as $val)
{
$str .= $open.$val.$close;
}
return $str;
}
It returns all the errors as a single string. You would need to adjust this accordingly, perhaps to return an array with the key for the respective file field.
You could use the $_FILES superglobal before hand, and check each field for errors, and then if there are no errors, call CI's do_upload()

Resources