ProcessWire - Dateien verarbeiten: Unterschied zwischen den Versionen
(Die Seite wurde neu angelegt: „Dateien sind immer ein recht komplexes Thema. Hier sind einige Basics dazu welche Funktionen und Fieldtypes es gibt, wo Dateien gespeichert werden etc. == Lin…“) |
|||
| Zeile 3: | Zeile 3: | ||
== Links == | == Links == | ||
https://processwire.com/talk/topic/19876-how-to-hook-file-upload-destination/ | https://processwire.com/talk/topic/19876-how-to-hook-file-upload-destination/ | ||
| + | |||
| + | === Funktionen für Files und Bilder === | ||
| + | https://cheatsheet.processwire.com/#files | ||
| + | |||
=== File Uploads in Modulen === | === File Uploads in Modulen === | ||
https://processwire.com/talk/topic/21616-file-upload-inside-custom-module/ | https://processwire.com/talk/topic/21616-file-upload-inside-custom-module/ | ||
https://github.com/ryancramerdesign/ImportPagesCSV/blob/master/ImportPagesCSV.module | https://github.com/ryancramerdesign/ImportPagesCSV/blob/master/ImportPagesCSV.module | ||
| + | |||
| + | == Basiswissen zu Dateien ProcessWire == | ||
| + | Jede Seite in ProcessWire hat einen eigenen Ordner unterhalb von | ||
| + | site/assets/files/ | ||
| + | Wenn Dateien oder Bilder hochgeladen werden (egal ob API oder über ein Feld) muss die Seite gespeichert werden, sonst werden die Dateien gelöscht. | ||
== Snippets == | == Snippets == | ||
| + | === Funktionen um Dateien zu löschen === | ||
| + | <pre> | ||
| + | $page->files->deleteAll(); | ||
| + | $page->files->delete($pagefile); | ||
| + | |||
| + | $page->images->removeAll(); // wirearray | ||
| + | |||
| + | $page->save(); | ||
| + | </pre> | ||
=== Custom Directory für Dateien === | === Custom Directory für Dateien === | ||
https://processwire.com/talk/topic/19876-how-to-hook-file-upload-destination/ | https://processwire.com/talk/topic/19876-how-to-hook-file-upload-destination/ | ||
Version vom 7. Januar 2020, 13:39 Uhr
Dateien sind immer ein recht komplexes Thema. Hier sind einige Basics dazu welche Funktionen und Fieldtypes es gibt, wo Dateien gespeichert werden etc.
Links
https://processwire.com/talk/topic/19876-how-to-hook-file-upload-destination/
Funktionen für Files und Bilder
https://cheatsheet.processwire.com/#files
File Uploads in Modulen
https://processwire.com/talk/topic/21616-file-upload-inside-custom-module/ https://github.com/ryancramerdesign/ImportPagesCSV/blob/master/ImportPagesCSV.module
Basiswissen zu Dateien ProcessWire
Jede Seite in ProcessWire hat einen eigenen Ordner unterhalb von
site/assets/files/
Wenn Dateien oder Bilder hochgeladen werden (egal ob API oder über ein Feld) muss die Seite gespeichert werden, sonst werden die Dateien gelöscht.
Snippets
Funktionen um Dateien zu löschen
$page->files->deleteAll(); $page->files->delete($pagefile); $page->images->removeAll(); // wirearray $page->save();
Custom Directory für Dateien
https://processwire.com/talk/topic/19876-how-to-hook-file-upload-destination/
Mit dem InputfieldFile werden Dateien nach $page->save() automatisch mit einer Seite gespeichert. Wenn man mit den Seiten etwas spezielles vorhat, z.B. in einem Custom Ordner speichern kommt man mit einem Hook weiter.
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:
- Starter code: not much validation going on other than checking if the field the file was uploaded to is 'course_file'
- 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
- You might want the Hook to only run if you are in admin. You can add that logic
- 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);
});