11/26/2012 - 10:08 PM

Upload Images to new created Page Form Example

Upload Images to new created Page Form Example


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

$message = '';


    // 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->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png'));

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

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

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

        // 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>
<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"/>