KarlvonKarton Posted 5 hours ago Share Posted 5 hours ago I've tried to make a custom file upload module that uploads the files in a folder outside the webroot, but without succes. I tried different approaches, but not getting anywhere... Any ideas how to tackle this? approach 1: <?php namespace ProcessWire; /** * FieldtypeCustomImage * Custom fieldtype that stores images outside the web root. */ class FieldtypeOriginalFile extends FieldtypeFile { public static function getModuleInfo() { return [ 'title' => 'Original Image Field', 'version' => 1, 'summary' => 'An original image field that stores images outside the web root.', 'requires' => ['FieldtypeFile', 'InputfieldOriginalFile'], 'icon' => 'image', ]; } /** * Initialize the module and hook into Pages::saved to move files after the page is saved. */ public function init() { parent::init(); $this->addHookAfter('FieldtypeFile::customizeFilePath', $this, 'customizeFilePath'); $this->addHookAfter('FieldtypeFile::customizeFileUrl', $this, 'customizeFileUrl'); } /** * Customize the file path to store files in a folder named after the page ID. */ public function customizeFilePath(HookEvent $event) { $page = $event->argumentsByName('page'); $field = $event->argumentsByName('field'); $filename = $event->argumentsByName('file'); if ($field->name !== 'originalphoto') return; // Define the base custom storage path $baseCustomPath = '/home/portila/originalphotos/'; // Create a folder named after the page ID $customPath = $baseCustomPath . $page->id . '/'; // Ensure the directory exists if (!is_dir($customPath)) { mkdir($customPath, 0755, true); } // Return the full path to the file $event->return = $customPath . basename($filename); } /** * Customize the URL to serve files from a custom handler. */ public function customizeFileUrl(HookEvent $event) { $page = $event->argumentsByName('page'); $field = $event->argumentsByName('field'); $filename = $event->argumentsByName('file'); if ($field->name !== 'originalphoto') return; // Define the custom URL handler for files $customUrlBase = '/photo-file-handler.php?file='; $filePath = $page->id . '/' . urlencode(basename($filename)); $event->return = $customUrlBase . $filePath; } } Approach 2: <?php namespace ProcessWire; /** * FieldtypeCustomImage * Custom fieldtype that stores images outside the web root. */ class FieldtypeOriginalFile extends FieldtypeFile { public static function getModuleInfo() { return [ 'title' => 'Original Image Field', 'version' => 1, 'summary' => 'An original image field that stores images outside the web root.', 'requires' => ['FieldtypeFile', 'InputfieldOriginalFile'], 'icon' => 'image', ]; } /** * Initialize the module and hook into Pages::saved to move files after the page is saved. */ public function init() { parent::init(); $this->addHookAfter('InputfieldFile::processInputFile', $this, 'moveFilesToCustomPath'); //$this->addHookBefore('Pages::saved', $this, 'moveFilesToCustomPath'); // werkt niet, doet niets // werkt niet // $this->set('destinationPath', '/home/portila/originalphotos/'); // Set the custom path for uploaded files // hook into delete page to remove the file from the custom path $this->addHookBefore('Pages::delete', $this, 'removeCustomFile'); // Hook into Pagefile::remove to ensure files are deleted from the custom path $this->addHookBefore('Pagefile::remove', $this, 'removeCustomFile'); } public function moveFilesToCustomPath(HookEvent $event) { $pagefile = $event->argumentsByName('pagefile'); if($pagefile->field->name != 'originalphoto') return; //if($pagefile->filename == '') return; // dit is NOOIT leeg! // dus checken als file bestaat if(!file_exists($pagefile->filename)) return; $customPath = '/home/portila/originalphotos/'; $newPath = $customPath . basename($pagefile->filename); // Move the file from the assets location to the custom path if (rename($pagefile->filename, $newPath)) { // Update the file's path in ProcessWire (not in the database) //$pagefile->filename = $newPath; // Log a message the new path $this->message("The file from the custom path: $newPath"); // log message with current page id $this->message("The page id is: " . $pagefile->page->id); } else { // Log an error if the file couldn't be moved $this->error("Could not move the file to the custom path: $pagefile->filename"); } } public function removeCustomFile(HookEvent $event) { $pagefile = $event->argumentsByName('pagefile'); if($pagefile->field->name != 'originalphoto') return; $customPath = '/home/portila/originalphotos/'; // error if $pagefile->filename is empty if($pagefile->filename == '') { $this->error("The file is empty"); return; } $newPath = $customPath . basename($pagefile->filename); // Check if the file exists if (!file_exists($newPath)) { $this->error("The file does not exist at the custom path: $newPath"); return; } // Remove the file from the custom path if (@unlink($newPath)) { // Log a message if the file was removed $this->message("The file was removed from the custom path: $newPath"); } else { // Log an error if the file couldn't be removed $this->error("Could not remove the file from the custom path: $newPath"); } } } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now