Jump to content

How To Hook File Upload Destination?


Mustafa-Online
 Share

Recommended Posts

  • 4 weeks later...

You can Hook into InputfieldFile::processInputFile. There are other places in that Class you could also hook into, but I think processInputFile works best.

Throw the code below in ready.php

Please note:

  1. Starter code: not much validation going on other than checking if the field the file was uploaded to is 'course_file'
  2. You will have to implement other logic yourself. For instance, the code copies the file immediately it is uploaded by ProcessWire Ajax. It doesn't check if the page is actually saved. If a page is not saved and the page is reloaded, as you know, files in file fields are deleted from disk. This code does not delete the corresponding file in your custom directory
  3. You might want the Hook to only run if you are in admin. You can add that logic
  4. I've purposefully left in verbose and debugging code in there (Tracy stuff) to help you ( maybe and others) understand what's going on. I suggest you test using Tracy Debugger for a better grasp of the file upload process. Delete the debugging stuff when you've got this working as you want :-).
wire()->addHookAfter("InputfieldFile::processInputFile", function(HookEvent $event)  {
    // @note: here, events are $input, $pagefile, $n @see: the method
    // get event we are hooking into
    
    // get arguments by index {a bit faster, but less-readable}
    /* $input = $event->arguments[0];
    $pagefile = $event->arguments[1];
    $n = $event->arguments[2]; */
    // get arguments by name
    #$input = $event->argumentsByName('input');
    $pagefile = $event->argumentsByName('pagefile');
    #$n = $event->argumentsByName('n');
    
    // $pagefile->field: The Field object that this file is part of.
    // limit to a specific field {course_file}
    if($pagefile->field->name != 'course_files') return;

    # intercept file
    
    // Tracy Debugger calls to see what's going on. Also logs Ajax inputs!
    #bd($input, 'input');
    #bd($n, 'input');
    // @see: http://processwire.com/api/ref/pagefile/
    // pagefile object
    bd($pagefile, 'pagefile');
    // name of the field uploading to {your 'course_file'}
    bd($pagefile->field->name, 'field pagefile is part of');
    // file-sanitized name of the file we've added, e.g. 'checklist_install.pdf'
    bd($pagefile->basename, 'name of added file');
    // full disk path where the file has been uploaded in this page's files folder...
    //... in /site/assets/files/1234 where 1234 is this page's ID
    // ... e.g. "F:/www/mysite/site/assets/files/1234/checklist_install.pdf"
    bd($pagefile->filename, 'full disk path name of added file');
    // $pagefile->page: The Page object that this file is part of
    bd($pagefile->page->id, 'id of the page file added to');

    // full disk path to your custom uploads directory
    $customDirectory = $this->wire('config')->paths->assets . 'custom_directory/';
    bd($customDirectory,'custom directory for files');
    
    # copy file
    // use ProcessWire's $files API
    // @see: http://processwire.com/api/ref/files/
    $files = $this->wire('files');
    // copy the file(s)
    $files->copy($pagefile->filename,$customDirectory . $pagefile->basename);
});

 

Edited by kongondo
  • Like 7
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...