dhruba Posted August 26, 2013 Share Posted August 26, 2013 Hi How to delete file before edit the file field in page means How to edit FieldTypeFile using API. Link to comment Share on other sites More sharing options...
Soma Posted August 26, 2013 Share Posted August 26, 2013 $page->files->deleteAll(); $page->files->delete($pagefile); $page->images->removeAll(); // wirearray $page->save(); http://cheatsheet.processwire.com/?filter=filesdelete 1 Link to comment Share on other sites More sharing options...
dhruba Posted August 26, 2013 Author Share Posted August 26, 2013 I tried but I got some error like Pagefile::deleteAll does not exist or is not callable in this context Or how can I enforce File field to have recent one when I try to re-upload the file to that field. My file field name is document and I try to edit page using id from different page. My code Is................... $message = ''; if($input->post->edit){ $upload_path = $config->paths->assets.'uploads'.$pages->get($input->get->id)->parent->path; $wu = new WireUpload('upload'); $wu->setMaxFiles(1); $wu->setOverwrite(true); $wu->setDestinationPath($upload_path); $wu->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png', 'pdf', 'doc', 'docx', 'xls', 'xlsx')); $documents = $wu->execute(); if(!$wu->getErrors()){ $ep = $pages->get($input->get->id); $ep->of(false); $ep->document->deleteAll(); foreach($documents as $document) { $ep->document = $upload_path . $document; } $ep->setOutputFormatting(false); $ep->save(); $ep->setOutputFormatting(true); $message .= "<p class='message success'>Files uploaded!</p>"; } else { foreach($documents as $document) unlink($upload_path . $document); foreach($wu->getErrors() as $error) $message .= "<p class='error'>$error</p>"; } } Link to comment Share on other sites More sharing options...
Soma Posted August 26, 2013 Share Posted August 26, 2013 Since Pagefile is a single file field you would use delete(). You're not dealing with a WireArray then but with the page file itself. Or a $page->file = ""; $page->save(); would also do it maybe. Just wanted to add that if you (or need to) first $page->of(false) (setOutputFormatting), even a single file field will be an WireArray, so deleteAll() should then also work. 3 Link to comment Share on other sites More sharing options...
dhruba Posted August 26, 2013 Author Share Posted August 26, 2013 Ok it Works thanks. I have Corrected the the Previous code for others who want to edit page with file field. 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