Laravel move uploaded file - image

It is working good for full path like this
$file=$request->file('file');
$file->move('C:\xampp\htdocs\modo\images',$file->getClientOriginalName());
But i cant understand why it doesnt for root folder path :
$file->move('\modo\images',$file->getClientOriginalName());

You need to use base_path() method. This method returns the fully qualified path to the project root:
So in your case try the below code:
$file = $request->file('file');
$file->move(base_path('\modo\images'), $file->getClientOriginalName());
and if you want to return the public directory then use:
$path = public_path();
For more info read Laravel helper functions

You need to do it this way:
$file->move(base_path('\modo\images'),$file->getClientOriginalName());

Related

Laravel putFileAs with External URL not working

Trying to save an external URL image, to my s3.
But when I use code below, it returns error.
$contents = file_get_contents($url);
$filename = Str::slug($findproduct->title)."-".uniqid().".".$extension;
Storage::disk('s3')->putFileAs('product', $contents, $filename, ['visibility' => 'public']);
Here's error:
fopen() expects parameter 1 to be a valid path, string given
fopen() is the first function called by putFileAs API Docs.
It attempts to open the File supplied as parameter 2 to putFileAs(), in your case this is $contents. (FWIW, newer versions of Laravel also accept a string as a file path here)
It expects this to be either a File or UploadedFile object.
file_get_contents() returns a string instead.
Try using this instead:
$contents = new File($url);

How to get file as a resource with Storage?

I am trying to lock a file with the flock function but I am not able to do it. I work with Laravel 8 and Storage Class.
The code is as follows:
$disk = Storage::disk('communication');
$file_name = 'received.json';
$file_exists = $disk->exists($file_name);
if($file_exists){
flock($disk->get($file_name), LOCK_EX);
...
}
The problem I'm having is that when I invoke the get() function on the file path, it returns the contents of the file (a string), which causes the following error:
flock() expects parameter 1 to be resource, string given
I need to know how to get file as a resource and not the content of the file.
Could someone help me and tell me how to do it?
Thank you very much in advance.
You can use Storage::readStream() method
if($file_exists){
$stream=Storage::disk('communication')->readStream($file_name);
flock($stream, LOCK_EX);
}
As per php doc
flock(resource $stream, int $operation, int &$would_block = null): bool
First param needed stream.flock() allows you to perform a simple
reader/writer model which can be used on virtually every platform
(including most Unix derivatives and even Windows).
Ref:https://www.php.net/manual/en/function.flock.php

Codeigniter set_output function not working with image

I am trying to get the image as output, but it seems like set_output() function is not working properly with content type jpeg
My Code is given below...
$image = file_get_contents('assets/images/ThinkstockPhotos-145054512_small.jpg');
$this->output->set_content_type('jpeg')->set_output($image);
When I replace the image with a plain text file, in that case, it shows me the correct output
$file = file_get_contents('assets/images/test.txt');
$this->output->set_content_type('text')->set_output($file);
I have change content type from set_content_type('jpeg') to set_content_type('jpg') and set_content_type('gif') but stil it does not work and not show me on output.
What output I am getting now is shown in screenshot given below.
I was able to replicate the issue on my localhost setup only when I provided file_get_contents either a (1) bad path or (2) a non-existent image. I think you need to provide a full path to the image as with any directory/file related operation.
Try using FCPATH that is where you index.php lies and I assume your assets/ folder as well.
public function index() {
$file = FCPATH . 'assets/images/ThinkstockPhotos-145054512_small.jpg';
if (!is_file($file)) {
exit('File not found!');
}
$image = file_get_contents($file);
$this->output->set_content_type('jpeg')->set_output($image);
}
Note: if you get File not found! then assure that the file exists in /htdocs/assets/... where /htdocs/index.php is your main CI file

How to clear compiled view for specific blade template

PHP artisan view:clear command clears whole compiled views in an application.
How to clear compiled output for specific view.
Simple answer: Write your own command.
How do i start?
First of all, You must know that compiled views have different names than the original blade views.
What names do they have?
Laravel calls sha1() in the full file path. So for example. The compiled file name of layouts/app.blade.php (comes with default installation).
in versions less than 5.2 md5() is used instead of sha1(),
5.2, 5.3 => sha1()
5.1, 5.0, 4.2, 4.1, 4.0 => md5()
Assuming your version is >= 5.2
sha1('C:\xampp\htdocs\myapp\resources\views/layouts/app.blade.php');
So file name will be 9407584f16494299da8c41f4ed65dcb99af82ae2.php
How do i do that then?
Create new command that takes filename as an argument.
Add views path for filename in fire() function. As i showed you before C:\xampp\htdocs\myapp\resources\views (view full path) + /layouts/app.blade.php (filename)
$path = 'C:\xampp\htdocs\myapp\resources\views' . '/layouts/app.blade.php';
$path = sha1($path) . '.php'; To get the compiled filename.
Check if filename exists in compiled views dir
Delete file if exists
The command you'll have something like,
Note: If you have different view paths (Changed defaults), You must
make changes on my code below.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use RuntimeException;
class RemoveCompiled extends Command
{
protected $signature = 'view:clearOne {file}';
protected $description = 'Remove one compiled view!';
public function handle()
{
$path = sha1($this->laravel['config']['view.paths'][0] . '/' . $this->argument('file'));
$f = $this->laravel['config']['view.compiled'] . '\\'. $path . '.php';
if(!file_exists($f))
return; //do whatever you want
if(unlink($f))
echo "File deleted!";
}
}
Calling: php artisan view:clearOne layouts/app.blade.php

Registry Pattern location of variable declared

As Magento uses
Mage::register('somevar',$object);
Then use
Mage::registery('somevar') to get the data anywhere in the system.
Now,
Is there any quick way to find out the location of the declaration of the 'somevar'?
Can we locate that file and line quickly?
Or we have to go through all the class and find it all manually.
grep,awk, or your IDE should suffice for finding strings. grep example:
grep -srn0 "Mage::register('current" ./
Output:
./app/code/core/Mage/Catalog/Helper/Product.php:324: Mage::register('current_category', $category);
./app/code/core/Mage/Catalog/Helper/Product.php:328: Mage::register('current_product', $product);
./app/code/core/Mage/Catalog/Model/Convert/Parser/Product.php:384: Mage::register('current_imported_inventory', $inventoryFields);
./app/code/core/Mage/Catalog/Model/Layer/Filter/Category.php:94: Mage::register('current_category_filter', $this->getCategory(), true);
./app/code/core/Mage/Catalog/controllers/CategoryController.php:57: Mage::register('current_category', $category);

Resources