Jump to content

How to delete file using api


dhruba
 Share

Recommended Posts

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

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.

  • Like 3
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...