Xonox Posted September 12, 2018 Share Posted September 12, 2018 Hi, I'm trying to figure what's happening here. Objective The site administrator uploads, via FTP, a group of book images. Then, by running a tool in the admin area all the images will be assigned to each book through the SKU in the file name. This is a standalone tool with PW bootstrapped. Working code: // Get SKU $file_sku = substr($file, 0, 9); // Check for book $book = $pages->get('sku=' . $file_sku); if(!$book->id) { // Book doesn't exist } else { // Upload image into book $book->book_images->add($upload_directory . '/' . $file); // Delete file unlink($upload_directory . '/' . $file); } // Save book if($book->id) $book->save(); Problem This code adds to existing files. So when the administrator wants to replace files, instead of replacing it just keeps adding them to existing books. Alternative Using WireUpload. I've never used it before, but I think this might be right: // Get SKU $file_sku = substr($file, 0, 9); echo 'SKU: ' . $file_sku . '<br />'; // Check for book $book = $pages->get('sku=' . $file_sku); if(!$book->id) { // Book doesn't exist } else { // Upload image into book $u = new WireUpload('book_images'); $u->setMaxFiles(1); $u->setOverwrite(false); $u->setDestinationPath($book->files->path()); $u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png')); foreach($u->execute() as $filename) $book->files->add($filename); // Delete file unlink($upload_directory . '/' . $file); } // Save book if($book->id) $book->save(); Error! With this code I get the following error: Fatal error: Uncaught Error: Class 'WireUpload' not found in E:\WebServer\sadacosta.com\site\modules\Tools\process-book-images.php:35 Stack trace: #0 E:\WebServer\sadacosta.com\tools-process-book-images.php(4): include() #1 {main} thrown in E:\WebServer\sadacosta.com\site\modules\Tools\process-book-images.php on line 35 What's missing? Is there any other way to do this? Thanks, Link to comment Share on other sites More sharing options...
adrian Posted September 12, 2018 Share Posted September 12, 2018 Probably a namespace issue - try new \ProcessWire\WireUpload 2 Link to comment Share on other sites More sharing options...
Xonox Posted September 12, 2018 Author Share Posted September 12, 2018 2 hours ago, adrian said: Probably a namespace issue - try new \ProcessWire\WireUpload That was it! Thank you @adrian! 1 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