Symfony - failed to open stream only in Command - php

I'm using Symfony 4.
In my code, I have a function I use in a Controller and in a Command.
public function offlinequiz_check_scanned_page($scannedpage, $colle) {
....
$this->load_stored_image($scannedpage->getFilename(), $scannedpage, $colle);
....
}
public function load_stored_image($file, $scannedpage, $colle) {
....
$pathparts = pathinfo($file);
$filename = $pathparts['filename'] . '.' . $pathparts['extension'];
$imageinfo = getimagesize($file);
....
}
When used in the controller, the function works just fine.
In the Command, I have this error :
Failed to open stream: No such file or directory.
$scannedpage is the same object in both cases.
getFilename() returns an image path : ./uploads/copies/xxx/image_name.jpg.
The upload directory is in /public.
I tried to use Asset Component to get the right path but theses images are uploaded by users and are not managed by Webpack Encore so when I try this :
$package = new Package(new EmptyVersionStrategy());
$this->load_stored_image($package->getUrl($scannedpage->getFilename()), $scannedpage, $colle);
It returns the same as : $scannedpage->getFilename().

Related

Laravel 5.5 get file after uploading with Storage

In my Laravel 5.5 project I am having a problem in showing uploaded files. I uploaded the files using Storage. The part of store action of the controller is indicated below.
if ($request->hasFile('content_uz'))
{
$path = $request->file('content_uz')->store('/content/lesson'.$topic->lesson->id.'/topic'.$topic->id);
$data->content_uz = $path;
}
if ($request->hasFile('content_ru'))
{
$path = $request->file('content_ru')->store('/content/lesson'.$topic->lesson->id.'/topic'.$topic->id);
$data->content_ru = $path;
}
Uploading happened successfully. The path to uploaded 'content_uz' file is stored with "storage/app/content/lesson2/topic3" path and content_uz column is stored in my db as below:
content\lesson2\topic3\WSjrlG9a1ermGDOvRJTjn9iEIhfFvhVzjaOs6l79.mp4
How can I display the files in my Blade template? I searched the web, but with no result.
You can use method like this,
public function showFile() {
header("Content-type: video/mp4");
return Storage::get($filePath);
}
I hope this will help.
You may access the files of storage directory by two ways.
If your files are publicly accessible then you may follow laravel public disk.
If your files are protected or private then you may declare a route to access the files.
Route::get('content/{lesson}/{topic}/{file}', function($lesson, $topic, $file)
{
//Check access logic
$filePath = '/content/' . $lesson . '/' . $topic . '/' . $file;
return Storage::get($filePath);
});

laravel - saving and accessing an uploaded file

I am studying some laravel code that I downloaded and I am getting some problem.
This supposed to be the functions to save,delete and download the files but the problem is.
The files are being saved in a folder named with a number on "storage\app\public\project-files\" (i.e. storage\app\public\project-files\11), both destroy and download methods are referencing different paths, I tried to change but didn't worked, download show FileNotFoundException and destroy just remove from the database but not from the folder
So is this code wrong? How It supposed to be?
I've read about using artisan:link but seems odd to me run this command every time I want upload a file to make a link
PS. I cheched the routes, so the methods are being called
Thanks
public function store(Request $request)
{
if ($request->hasFile('file')) {
$file = new ProjectFile();
$file->user_id = $this->user->id;
$file->project_id = $request->project_id;
$request->file->store('public/project-files/'.$request->project_id);
$file->filename = $request->file->getClientOriginalName();
$file->hashname = $request->file->hashName();
$file->size = $request->file->getSize();
$file->save();
$this->project = Project::find($request->project_id);
return view('project-files');
}
public function destroy($id)
{
$file = ProjectFile::find($id);
File::delete('storage/project-files/'.$file->project_id.'/'.$file->hashname);
ProjectFile::destroy($id);
$this->project = Project::find($file->project_id);
return view('project-files');
}
public function download($id) {
$file = ProjectFile::find($id);
return response()->download('storage/project-files/'.$file->project_id.'/'.$file->hashname);
}
You are storing files in storage so i assume you have uploaded image in the following path
project\storage\app\public\project-files
if this is the path then you can delete using
Storage::delete('public/project-files/1.JPG');
for Downlaoding file
$path= storage_path('app/public/project-files/3.JPG');
return response()->download($path);

How to output file above public folder Laravel 5.1

I have debian machine and my site is in /var/www/laravel-site/ folder.
In laravel-site folder i have FILES and PUBLIC folders. How can i output file to FILES folder to protect the file from web reading?
I guess base_path() will help
Simple usage example:
<?php
$file = base_path('FILES').'/people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>
function base_path($path = '') is a Laravel helper function. It gets the path to the base of the install:
function base_path($path = '')
{
return app()->basePath().($path ? '/'.$path : $path);
}
So, to get your /var/www/laravel-site folder just use
echo base_path();
To get FILES folder (or whatever path you need inside the Laravel's base) use echo base_path('FILES');
it will output /var/www/laravel-site/FILES

Codeigniter deleting files in a directory not working

I am trying to delete pdf files from a folder. I have written the delete_files function as follows
public function delete_files($company_id)
{
$this->load->model('search_model');
$company = $this->search_model->get_company($company_id);
$username = $company[0]['username'];
$path=$this->config->base_url('/uploads/'.$username . '/' . 'uploaded');
$this->load->helper("file"); // load the helper
delete_files($path, true); // delete all files/folders
}
when i did echo $path; it shows the right path where i want the files deleted but when i run the entire function nothing happens and i just get a white screen.
You need to use path to file and not resource locator.
$path = FCPATH . "uploads/$username/uploaded";
try this code
Use
$path = FCPATH . "uploads/$username/uploaded";
unset($path);

How to mock readonly directory using PHPUnit and VFSstream

I have a simple open (file) method which should throw an exception if it fails to open or create a file in the given path:
const ERR_MSG_OPEN_FILE = 'Failed to open or create file at %s';
public function open($filePath)
{
ini_set('auto_detect_line_endings', TRUE);
if (false !== ($this->handler = #fopen($filePath, 'c+'))) {
return true;
} else {
throw new \Exception(
sprintf(self::ERR_MSG_OPEN_FILE, $filePath)
);
}
}
There is unit-test around it using PHPUnit and VFSStream:
$structure = [
'sample.csv' => file_get_contents(__DIR__ . '/../sampleFiles/small.csv')
];
$this->root = vfsStream::setup('exampleDir', null, $structure);
$existingFilePath = vfsStream::url('exampleDir') . DIRECTORY_SEPARATOR . 'sample.csv';
$this->file = new File();
$this->file->open($existingFilePath);
The above setup creates a virtual directory structor containing a mock file (read/cloned from an existing CSV file) and this satisfy the open method's requirement to open a file. And also if I pass a different path (none existing file) it will be created in the same virtual structor.
Now in order to cover the exception I want to add a ready-only directory to the existing structor, lets say a folder belong to another user and I don't have write permission on it, like this when I try to open a non existing file in that folder, attempt to creating one should fail and the open method should throw the exception.
The only problem is,.... I don't know how to do it :D
Any Advice, hint and guidance will be appreciated :)
You could simply point your mock directory's url to an incorrect path.
$existingFilePath = vfsStream::url('badExampleDir') . DIRECTORY_SEPARATOR . 'incorrect.csv';
considering the badExampleDir never been setup your method will fail to create the file in it.

Categories