How to get current directory in elfinder? - php

I am using elfinder and I have a problem. I want to get current directory in elfinder but I can not.
EDITED:
this is my connector. consist of my_function that called after upload, rename or mkdir commands and I want to get uploaded files path in specified place:
<?php
error_reporting(0); // Set E_ALL for debuging
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderConnector.class.php';
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinder.class.php';
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeDriver.class.php';
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeLocalFileSystem.class.php';
function access($attr, $path, $data, $volume) {
return strpos(basename($path), '.') === 0 // if file/folder begins with '.' (dot)
? !($attr == 'read' || $attr == 'write') // set read+write to false, other (locked+hidden) set to true
: null; // else elFinder decide it itself
}
function my_function($cmd, $result, $args, $elfinder)
{
// how to get current path here?
}
$opts = array(
'bind' => array('upload rename mkdir' => 'my_function'),
// 'debug' => true,
'roots' => array(
array(
'driver' => 'LocalFileSystem', // driver for accessing file system (REQUIRED)
'path' => '../files/', // path to files (REQUIRED)
'URL' => dirname($_SERVER['PHP_SELF']) . '/../files/', // URL to files (REQUIRED)
'accessControl' => 'access' // disable and hide dot starting files (OPTIONAL)
),
)
);
// run elFinder
$connector = new elFinderConnector(new elFinder($opts));
$connector->run();

You can get items URL.
function my_function($cmd, $result, $args, $elfinder)
{
// how to get current path here?
foreach ($result['added'] as $file) {
if (!empty($file['url']) && $file['url'] != 1) {
$url = $file['url'];
}
}
}
or Make inherent class ex elFinderVolumeMyLocalFileSystem
class elFinderVolumeMyLocalFileSystem extends elFinderVolumeLocalFileSystem
{
public function decode($hash) {
return parent::decode($hash);
}
}
function my_function($cmd, $result, $args, $elfinder)
{
// how to get current path here?
foreach ($result['added'] as $file) {
if ($volume = $elfinder->getVolume($file['hash'])) {
$dir = $volume->decode($file['phash']);
}
}
}
$opts = array(
'bind' => array('upload rename mkdir' => 'my_function'),
// 'debug' => true,
'roots' => array(
array(
'driver' => 'MyLocalFileSystem', // driver for accessing file system (REQUIRED)
'path' => '../files/', // path to files (REQUIRED)
'URL' => dirname($_SERVER['PHP_SELF']) . '/../files/', // URL to files (REQUIRED)
'accessControl' => 'access' // disable and hide dot starting files (OPTIONAL)
),
)
);

Related

Upload CSV File in Admin Controller - Custom module

I'm trying to create an Admin Controller with a csv file uploader to process it like an array.
I can't find an efficient way to do it, I tried to use $this-> fields_form, but nothing is showing up.
Then I did a tpl file with an input file, called in initContent, but I don't know how to retrieve my file in the controller.
I need to create multiple object of different classes that I made, thanks to the csv file.
Does somebody have some documentation that could help me, I've already search through prestashop dev doc, stack overflow, ect but I've didn't see anything that could help me (maybe I didn't search the good way ?)
Waiting for your help guys !
Update :
Update :
Just found a way to upload my file, but it's upload as .tmp and can't be processed as a csv file, how can I convert a tmp file to a csv ?
Here is my code :
public function __construct()
{
parent::__construct();
// Base
$this->bootstrap = true; // use Bootstrap CSS
$this->fields_options = array(
'general' => array(
'title' => $this->l('Upload DB'),
'fields' => array(
'DB_BULB_DATA' => array(
'title' => $this->l('Upload DB'),
'type' => 'file',
'name' => 'DB_BULB_DATA'
),
),
'submit' => array('title' => $this->trans('Save', array(), 'Admin.Actions')),
),
);
if(isset($_FILES['DB_BULB_DATA'])){
$headers = fgetcsv(fopen($_FILES['DB_BULB_DATA']['tmp_name'], "r+"));
print_r($headers);
}
}
There is no file type name csvfile , you need to use filed type file and then hadel the csv file after upload, check file extension then process the data.
Just find out how to do it, I feel dummy 😅
I just needed to save my tmp file as a csv to be able to use it then.
Here is the full code :
<?php
class Admin<YourModuleName>Upload<YourThings>DatabaseController extends ModuleAdminController
{
public function __construct()
{
parent::__construct();
// Base
$this->bootstrap = true; // use Bootstrap CSS
$this->name = "Admin<YourModuleName>Upload<YourThings>Database";
$this->fields_options = array(
'general' => array(
'title' => $this->l('Upload DB'),
'fields' => array(
'DB_<YourThings>_DATA' => array(
'title' => $this->l('Upload DB'),
'type' => 'file',
'name' => 'DB_<YourThings>_DATA'
),
),
'submit' => array('title' => $this->l('Save')),
),
);
}
public function initContent()
{
parent::initContent();
unset($_FILES);
}
public function postProcess()
{
$fileName = '<YourThings>Db.csv';
if (!file_exists(_PS_MODULE_DIR_ . '<YourModuleName>/upload/' . $fileName)) {
if (isset($_FILES['DB_<YourThings>_DATA'])) {
$tmpPath = $_FILES['DB_<YourThings>_DATA']["tmp_name"];
move_uploaded_file($tmpPath, _PS_MODULE_DIR_ . '<YourModuleName>/upload/' . $fileName);
$uploadCsv = file(_PS_MODULE_DIR_ . '<YourModuleName>/upload/' . $fileName, FILE_SKIP_EMPTY_LINES);
$Db = array_map("str_getcsv", $uploadCsv, array_fill(0, count($uploadCsv), ';'));
$keys = array_shift($Db);
foreach ($Db as $i => $row) {
$Db[$i] = array_combine($keys, $row);
}
print_r($Db);
}
} else {
$uploadCsv = file(_PS_MODULE_DIR_ . '<YourModuleName>/upload/' . $fileName, FILE_SKIP_EMPTY_LINES);
$Db = array_map("str_getcsv", $uploadCsv, array_fill(0, count($uploadCsv), ';'));
$keys = array_shift($Db);
foreach ($Db as $i => $row) {
$Db[$i] = array_combine($keys, $row);
}
print_r($Db);
}
unset($_FILES['DB_<YourThings>_DATA']);
}
}

Laravel - How to move file from local storage to another storage?

My goal is to upload a large file to dropbox and because the waiting time can be too long I want to split the process and upload the file through a queue.
I'm doing the following:
I upload a file (that can be large)
I save it on local storage
I save data about the file in database.
In a queue I want to get the file and move it to a dropbox disk.
The problem is that when I do the last step I get the following error
ErrorException
fopen(files/7u7v6LYq72vmXLqeWPsc6b0khiy9pEbFicVJuK2W.pdf): failed to open stream: No such file or directory
I tried different approaches but I can't find a solution.
My code
Controller method:
public function uploadToDropbox(Request $request){
$data = $request->validate([
'file' => 'required|mimes:jpeg,jpg,png,doc,docx,pdf,txt,mp3,mp4,avi|max:600000',
'first_name' => 'required',
'last_name' => 'required',
]);
/** #var \Symfony\Component\HttpFoundation\File\File $uploadedFile */
$uploadedFile = $data['file'];
$path = Storage::disk('local')->putFileAs( 'file', $uploadedFile, $uploadedFile->getClientOriginalName());
$file = new File();
$file->first_name = $data['first_name'];
$file->last_name = $data['last_name'];
$file->file = $path;
$file->original_name = $uploadedFile->getClientOriginalName();
$file->size = $uploadedFile->getSize();
$file->real_path = $uploadedFile->getRealPath();
$file->save();
$result = ProcessFile::dispatch($file);
if($result){
return Redirect::back()->withErrors(['msg'=>'Successfully file uploaded']);
} else {
return Redirect::back()->withErrors(['msg'=>'File failed to upload']);
}
}
Queue job:
public function handle()
{
if (Storage::disk('local')->exists($this->file->file)) {
$name = strtolower($this->file->first_name) . '_' . strtolower($this->file->last_name);
$rez = Storage::disk('dropbox')->putFileAs(
'challenge-files/' . $name . '/',
$this->file->file,
$this->file->original_name
);
Log::info('message: ' . $rez);
} else {
Log::alert('falseeeee');
}
}
FilesystemAdapter puthFileAs method:
public function putFileAs($path, $file, $name, $options = [])
{
$stream = fopen(is_string($file) ? $file : $file->getRealPath(), 'r');
// Next, we will format the path of the file and store the file using a stream since
// they provide better performance than alternatives. Once we write the file this
// stream will get closed automatically by us so the developer doesn't have to.
$result = $this->put(
$path = trim($path.'/'.$name, '/'), $stream, $options
);
if (is_resource($stream)) {
fclose($stream);
}
return $result ? $path : false;
}
filesystems.php local disk configs
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'permissions' => [
'file' => [
'public' => 0664,
'private' => 0600,
],
'dir' => [
'public' => 0775,
'private' => 0700,
],
],
],
Probably, you haven't modify the permissions mappings in your filesystems configuration file.
Look for the
dir
array and check if the
public
number is
0775
if it is not, change it to that number
Look for
file
change 'public' => 0664

set dynamic path elfinder

i'm using elfinder and i want to set path with php var (from my db connection)
here the array from my connector
include_once('../../include/header.php');
$CustomerUniqueCode=$InfoG->customer_unique_secure_code;
error_reporting(0); // Set E_ALL for debuging
require './autoload.php';
// ===============================================
function access($attr, $path, $data, $volume) {
return strpos(basename($path), '.') === 0 // if file/folder begins with '.' (dot)
? !($attr == 'read' || $attr == 'write') // set read+write to false, other (locked+hidden) set to true
: null; // else elFinder decide it itself
}
$opts = array(
//'debug' => true,
'roots' => array(
array(
'driver' => 'LocalFileSystem', // driver for accessing file system (REQUIRED)
'path' => '../../folder/customer/'.$CustomerUniqueCode.'/files/', // path to files (REQUIRED)
'URL' => dirname($_SERVER['PHP_SELF']) . '../../folder/customer/'.$CustomerUniqueCode.'/files/', // URL to files (REQUIRED)
'tmbPath' => '../files/thumbnails/',
'tmbURL' => dirname($_SERVER['PHP_SELF']) . '/../files/thumbnails',
'uploadDeny' => array('all'), // All Mimetypes not allowed to upload
'uploadAllow' => array('image', 'text/plain'),// Mimetype `image` and `text/plain` allowed to upload
'uploadOrder' => array('deny', 'allow'), // allowed Mimetype `image` and `text/plain` only
'accessControl' => 'access' // disable and hide dot starting files (OPTIONAL)
)
)
);
// run elFinder
$connector = new elFinderConnector(new elFinder($opts));
$connector->run();
The error message is :
Invalid backend configuration.
Readable volumes not available.
display_file.php including elfinder.php
Here the server organisation
Someone can help me ?

What is the best way to edit a settings file containing a global named config in php

I've got the file 'init.php'. This one is being used to start the session, set some settings and that sort of stuff. I call to this file using this line:
require_once 'core/init.php';
This works perfectly in my opinion. Now, I've written the following script so that it becomes very easy to call a setting in the init.php file.
class Config {
public static function get($path = null) {
if ($path){
$config = $GLOBALS['config'];
$path = explode('/', $path);
foreach($path as $bit) {
if(isset($config[$bit])) {
$config = $config[$bit];
}
}
return $config;
}
return false;
}
}
So now I can use this line of code in my other pages to use the setting:
Config::get('settings/main_color')
This is very easy of course. But now I'd like to edit a setting without having to change the file myself. It should all be done by scripts in the browser. The rest of my init.php settings global looks like this:
$GLOBALS['config'] = array(
'mysql' => array(
'host' => 'localhost:3307',
'username' => 'root',
'password' => 'usbw',
'db' => 'webshop'
),
'remember' => array(
'cookie_name' => 'hash',
'cookie_expiry' => 604800
),
'sessions' => array(
'session_name' => 'user',
'token_name' => 'token'
),
'settings' => array(
'main_color' => '#069CDE',
'front_page_cat' => 'Best Verkocht,Populaire Producten',
'title_block_first' => 'GRATIS verzending van €50,-',
'title_block_second' => 'Vandaag besteld morgen in huis!',
),
'statics' => array(
'header' => 'enabled',
'title_block' => 'enabled',
'menu' => 'enabled',
'slideshow' => 'enabled',
'left_box' => 'enabled',
'email_block' => 'enabled',
'footer' => 'enabled',
'keurmerken' => 'enabled',
'copyright' => 'enabled'
)
);
What I hope for is a solution like this:
Config::update('settings/main_color','blue')
What is the best way to achieve this? Use str_replace and replace the word in the file? I hope there's a better way of doing this, and I'd be very pleased if you could help me out.
EDIT: (my complete init.php file)
<?php
session_start();
define('DS',DIRECTORY_SEPARATOR);
$GLOBALS['config'] = json_decode(__DIR__.DS.'prefs.json', true);
spl_autoload_register(function($class) {
require_once 'classes/' . $class . '.php';
});
require_once 'functions/sanitize.php';
require_once 'functions/statics.php';
require_once 'functions/pagination.php';
if(Cookie::exists(Config::get('remember/cookie_name')) && !Session::exists(Config::get('sessions/session_name'))) {
$hash = Cookie::get(Config::get('remember/cookie_name'));
$hashCheck = DB::getInstance()->get('users_session', array('hash', '=', $hash));
if($hashCheck->count()) {
$user = new User($hashCheck->first()->user_id);
$user->login();
}
}
$_link = DB::getConnected();
$url_parts = explode('/',$_SERVER['REQUEST_URI']);
$current = $url_parts[count($url_parts)-2];
if($current == 'page'){
$_SESSION['location'] = 1;
}
else{
$_SESSION['location'] = 0;
}
This is somewhat opinion-based, but I use a json or xml file that the config file parses. The update would call the file, parse it then resave it.
/core/init.php
$GLOBALS['config'] = json_decode(__DIR__.DS.'prefs.json', true);
/core/prefs.json
{"mysql":{"host":"localhost:3307","username":"root","password":"usbw","db":"webshop"},"remember":{"cookie_name":"hash","cookie_expiry":604800},"sessions":{"session_name":"user","token_name":"token"},"settings":{"main_color":"#069CDE","front_page_cat":"Best Verkocht,Populaire Producten","title_block_first":"GRATIS verzending van €50,-","title_block_second":"Vandaag besteld morgen in huis!"},"statics":{"header":"enabled","title_block":"enabled","menu":"enabled","slideshow":"enabled","left_box":"enabled","email_block":"enabled","footer":"enabled","keurmerken":"enabled","copyright":"enabled"}}
Pseudo-code for your update:
class Config
{
public static function update($path,$value)
{
# Fetch the file and convert it to json
$file = json_decode(file_get_contents(CONFIG_PATH),true);
# Do some code that will update the value....
# Save file back
file_put_contents(CONFIG_PATH,json_encode($data));
# Call your prefs to GLOBALS function here so the settings
# are updated in your global array
}
}

Creating jstree from ftp directory structure

I need to use jstree to display the directory structure of a ftp account's homedir.
I somehow got stuck at retrieving the full directory structure (and create a json object, xml file, html code, whatever). It is most likely something easy that is just slipping my mind, but anyway, here's what i tried so far:
function draw_tree($path)
{
global $con;
$list = ftp_nlist($con,$path);
$dirs = array(); $files = array();
foreach($list as $file)
{
if(ftp_is_dir($file))
{
$dir[] = array(
'attr' => array('data-path' => $path . '/' . $file,
'data' => $file,
'state' => 'closed',
'children' => // ??? some recursive function should
// probably go here
);
}
else {
$files[] = array(
'attr' => array('data-path' => $file)
);
}
}
return array_merge($dirs,$files);
}

Categories