Craeckerffm
11/26/2012 - 10:08 PM

Upload Images to new created Page Form Example

Upload Images to new created Page Form Example

<?php

// front-end form example with multiple images upload 
// add new page created on the fly and adding images

$message = '';

if($input->post->submit){

    // tmp upload folder for additional security
    // possibly restrict access to this folder using htaccess:
    // # RewriteRule ^.tmp_uploads(.*) - [F]
    $upload_path = $config->paths->root . ".tmp_uploads/";

    // new wire upload
    $u = new WireUpload('uploads');
    $u->setMaxFiles(2);
    $u->setMaxFileSize(200*1024);
    $u->setOverwrite(false);
    $u->setDestinationPath($upload_path);
    $u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png'));

    // execute upload and check for errors
    $files = $u->execute();

    if(!$u->getErrors()){
        // create the new page to add the images
        $uploadpage = new Page();
        $uploadpage->template = "basic-page";
        $uploadpage->title = "uploadpage";
        $uploadpage->parent = $pages->get("/templates/");
        $uploadpage->save();

        // add images upload
        foreach($files as $filename) {
            $uploadpage->images = $upload_path . $filename;
        }
        // save page
        $uploadpage->save();

        // remove all tmp files uploaded
        foreach($files as $filename) unlink($upload_path . $filename);
        $message .= "<p class='message'>Files uploaded!</p>";

    } else {
        // remove files
        foreach($files as $filename) unlink($upload_path . $filename);

        // get the errors
        foreach($u->getErrors() as $error) $message .= "<p class='error'>$error</p>";
    }
}


?>
<h2>Upload Images to Page Example Form</h2>
<?=$message?>
<form method="post" action="./" enctype="multipart/form-data">
    <input type="file" name="uploads[]" multiple="multiple" size="40" accept="image/jpg,image/jpeg,image/gif,image/png"/>
    <input type="submit" name="submit" value="Upload files"/>
</form>