Skip to content

Instantly share code, notes, and snippets.

@somatonic
Created November 26, 2012 22:08
Show Gist options
  • Save somatonic/4150974 to your computer and use it in GitHub Desktop.
Save somatonic/4150974 to your computer and use it in GitHub Desktop.
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment