How to access the file name and its detail from directory in the laravel framework? - laravel-4

Storage::disk('local')->files();
Storage::disk('local')->lastModified();
I want both thing simultaneously on single line. Can you please tell me how to do so?

The only way to achieve this is to loop though the files and then get the modified time for each item. If the directory is large, this approach would not be recommended:
foreach ( Storage::disk('local')->files() as $file ) {
dump( $file, Carbon::createFromTimestamp( Storage::disk('local')->lastModified($file) )->format( 'jS M Y, H:i:s' ) );
}
Remember to include the following lines at the top of your class:
use Storage;
use Carbon\Carbon;

Related

How to glob and check multiple files in InstallBuilder?

I'm making installer program using VMware InstallBuilder (previously named Bitrock InstallBuilder), and need to ensure the non-existence of some files in target directory. Here I describe its logic in Perl fake code:
sub validate_target_dir
{
my $target_dir = shift;
# find all candidate check files
foreach my $file ( glob( "$target_dir/*_vol0.dat" ) )
{
my $fname = basename($file);
# fail if has any data file other than myproduct
if ($fname ne "myproduct_vol0.dat") { return 0; }
}
return 1;
}
However I don't know how to implement similar logic in InstallBuilder, as its findFile action don't return multiple matching files (only the first one).
At current, I find a somewhat twisted way to do it: implement the function in a shell script. InstallBuilder allows you to unpack contents programmatically, so I can pack the script into the installer package and unpack&run it at needed time.
I still seek if there's possible way that purely implemented by InstallBuilder itself.

How to minify view HTML in Codeigniter 4 (CI 4)

I know we can minify HTML in CI3 through the hook but in CI 4
I have done it by adding a minify function before return on every method.
minifyHTML(view('admin/template/template',$this->data));
Any other way I can do it without using the minify function everywhere?
I also figure out another solution which is to add a template function in BaseConroller which renders all the views. but I have already used view() in many places in the project and its not feasible for me but can be work for others.
In CodeIgniter4 You can use Events to Minify the output (Instead of using Hooks like what we did in CodeIgniter3) , by adding the following code inti app/Config/Events.php file
//minify html output on codeigniter 4 in production environment
Events::on('post_controller_constructor', function () {
if (ENVIRONMENT !== 'testing') {
while (ob_get_level() > 0)
{
ob_end_flush();
}
ob_start(function ($buffer) {
$search = array(
'/\n/', // replace end of line by a <del>space</del> nothing , if you want space make it down ' ' instead of ''
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s', // shorten multiple whitespace sequences
'/<!--(.|\s)*?-->/' //remove HTML comments
);
$replace = array(
'',
'>',
'<',
'\\1',
''
);
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
});
}
});
see https://gitlab.irbidnet.com/-/snippets/3
You need the extend your core system classes to be able to do that in a system wide scope.
Every time CodeIgniter runs there are several base classes that are initialized automatically as part of the core framework. It is possible, however, to swap any of the core system classes with your own version or even just extend the core versions.
Two of the classes that you can extend are these two:
CodeIgniter\View\View
CodeIgniter\View\Escaper
For example, if you have a new App\Libraries\View class that you would like to use in place of the core system class, you would create your class like this:
The class declaration must extend the parent class.
<?php namespace App\Libraries;
use CodeIgniter\View\View as View;
class View implements View
{
public function __construct()
{
parent::__construct();
}
}
Any functions in your class that are named identically to the methods in the parent class will be used instead of the native ones (this is known as “method overriding”). This allows you to substantially alter the CodeIgniter core.
So in this case you can look at your system view class and just change it to return the output already compressed.
In your case You might even add an extra param so that the view function can return the output either compressed or not.
For more information about extending core classes in CodeIgniter 4 read this:
https://codeigniter.com/user_guide/extending/core_classes.html#extending-core-classes
In CodeIgniter 4, you can minify the HTML output of your views by using the built-in output compression library.
First, you need to enable output compression in your application's configuration file, which is located at app/Config/App.php.
Then, you need to set the compression level. You can set the compression level by changing the value of compress_output option. The possible values are:
0 : Off (Do not compress the output)
1 : On (Compress output, but do
not remove whitespace)
2 : On (Compress output, and remove
whitespace)
Finally, you need to load the Output Library in your controller, before the output is sent to the browser. You can load it using the following line of code:
$this->load->library('output');

Transactions for file operations in Laravel

In Laravel I can do database transactions by passing a closure to the DB::transaction function.
Does Laravel have any support for a similar feature for the File or Storage facade? Where the file operations are run in a transaction, with rollback in case a file operation fails?
I'm imagining something like
$files = ... // Something that returns a collection
File::transaction(function () use ($files) {
$files->each(function() {
File::move(....);
});
});
There is no built in way of doing it so you'd have to make an implementation yourself.
A simple method of achieving it would be
$fileName = ""; // or $fileNames ( array ) if multiple file uploads
$files = "" // to be used if you're going to update or delete files. Again if multiple file modifications then use array
try{
/* Just a note, but your new file could overwrite any existing files,
so before uploading, check if another file exists with same filename
And if it does, load that file and keep it in the $files variable
*/
// Upload File
$fileName = // name of uploaded file
$files = // Any existing file you're going to modify. Load the entire file data, not just the name
// Modify/Delete a file
}( \Exception $e ){
// Now delete the file using fileName or $fileNames if the variable is not empty
// If you modified/deleted any file, then undo those modifications using the data in $files ( if it's not empty )
}
In this method, existing files are loaded to memory, but if there are multiple large files, it might be better to move them to a temporary location instead, and move them back if any exception is thrown. Just don't forget to delete these temporary files if the file transaction is a success

Laravel Multiple Upload File

How to show uploaded files using multiple upload files?
With data like this
Lets consider you name is in this variable = $uploadedFiles;
$uploadedFiles = ["file1","file2","file3"];
$temp = json_decode($uploadedFiles);
foreach($temp as $name) {
echo $name;
}
It will print your file name 1 by 1
It just names you need the files also
but if you want to store only that names then you can do it by taking a loop for that variable array
if it is not an array then convert to array using json_decode(array)

How to get the details of a file on Windows

If you open the properties of a file in Windows, there usually is a Details tab. I want to access the information on this tab, but I don't know how.
Is there a module for it? Does someone has a code sniplet?
I tried to work with Win32::File's GetAttributes, but these are not the attributes I was looking for.
use Win32::OLE;
my $objShell = Win32::OLE->new("Shell.Application") or die;
my $objFolder = $objShell->NameSpace($myDir) or die;
my $objFile = $objFolder->ParseName($fileName) or die;
while ( $i <= 34 )
{
my $propertyName = $objFolder->GetDetailsOf($fileName,$i);
my $propertyValue = $objFolder->GetDetailsOf($objFile,$i);
print "$i -- $propertyName -- $propertyValue\n";
$i++;
}
You can instantiate a COM "Shell.Application" object. It exposes a .NameSpace(folder) method that returns a reference to the name space of the indicated folder, which holds the information you need. The retrieved instance holds a Items collection with references to each of the files in the folder, and a .GetDetailsOf(file,property) to retrieve each of the values seen in the details tab and explorer columns.
Sorry i have no idea of perl, so i can not include any working code.

Resources