Jump to content
Mustafa-Online

How To Hook File Upload Destination?

Recommended Posts

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 5

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By rushy
      Hi. I've been using Processwire for a few years now and installed it on a few different shared hosting servers without issues, but I'm now running into an issue on a Fasthosts shared server that I've not seen before. The installation goes ok and I have a working default site profile but I am unable to complete any uploads of images. It just hangs at the progress spinner during the upload. I thought it might be a priviledge issue so have temporarily set the dir / file privs. as 777 / 666 just to test that but it made no difference. I have no errors in the wire log. In server error log I have:
      set_time_limit() has been disabled for security reasons in ....htdocs/wire/core/ImageSizerEngine.php on line 1035 and i note in the assets/file dir. where the image is being stored the date on the file being created has a year of 1970 and 0 length. If I leave the page this file disappears and nothing has been stored. 
      Does anyone know how I can enable set_time_limit if this is likely to be the problem? 
      Many thanks! Paul
    • By Fanni
      Hi all, 
      I am having some issues uploading a PDF to our website (a problem we’ve had before).
      I have tried reducing the size of the PDF but the problem seems to be with generating the thumbnail image because the upload works under the Fallback mode but the thumbnail is terrible (see attached). 
      Does anybody have any advice about this? Please note that I'm not a developer. 
      Thanks!
       


    • By Robin S
      If you've ever needed to insert links to a large number of files within CKEditor you may have found that the standard PW link modal is a somewhat slow way to do it.
      This module provides a quicker way to insert links to files on the page being edited. You can insert a link to an individual file, or insert an unordered list of links to all files on the page with a single click.
      CKEditor Link Files
      Adds a menu to CKEditor to allow the quick insertion of links to files on the page being edited.

      Features
      Hover a menu item to see the "Description" of the corresponding file (if present). Click a menu item to insert a link to the corresponding file at the current cursor position. The filename is used as the link text. If you Alt-click a menu item the file description is used as the link text (with fallback to filename if no description entered). If text is currently selected in the editor then the selected text is used as the link text. Click "* Insert links to all files *" to insert an unordered list of links to all files on the page. Also works with the Alt-click option. Menu is built via AJAX so newly uploaded files are included in the menu without the page needing to be saved. However, descriptions are not available for newly uploaded files until the page is saved. There is an option in the module config to include files from Repeater fields in the edited page. Nested Repeater fields (files inside a Repeater inside another Repeater) are not supported. Installation
      Install the CKEditor Link Files module.
      For any CKEditor field where you want the "Insert link to file" dropdown menu to appear in the CKEditor toolbar, visit the field settings and add "LinkFilesMenu" to the "CKEditor Toolbar" settings field.
       
      http://modules.processwire.com/modules/cke-link-files/
      https://github.com/Toutouwai/CkeLinkFiles
    • By Hardoman
      Hello community,
      we have a website running version 3.0.118. The owner would like to have a watermark merged to the images, that are being uploaded in the CKEditor as a requirement.
      Image upload besides the CKEditor within galleries and single images works as a charm already. We also use croppable image 3 there. (PIM2)
      To realize this requirement, I thought of using a hook in the admin area. So, I read a lot in our forums and tested this by adding a hook into the ready.php file.
      $this->addHookAfter('InputfieldFile::fileAdded',function(HookEvent$event){ wire('log')->save('test','Image upload works'); ... The log entry is being created correctly. But when I try to use the pim/watermark-function like in a template, he cannot find the watermark-image anymore. Furthermore, when I try to get the page-id, it does not seem to be accessible, because the application does not seem to know how to reference it, or I dont know the right way to do so…
      So my questions are:
       
      Is this the right attempt at all or will there be another, better workaround? It seems, I cannot access the page object (of the content page) within this scope or file but I would need it to save the processed image inside the right files/id folder Would it be better to place the hook into the admin-template? (or admin.php)
        Thanks for any hints in advance. 🙂
    • By martind
      hi,
      is it possible to change field parent_id from InputfieldPageListSelect to InputfieldPageAutocomplet by addHookAfter('ProcessPageEdit::buildFormContent')?
      thanks,
      martin
       
×
×
  • Create New...