Last active
July 5, 2018 15:27
-
-
Save andreasnymark/db8b31fffe8be91af3d38e8c40b04c39 to your computer and use it in GitHub Desktop.
Kirby file upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* File upload | |
* | |
* | |
**/ | |
array( | |
'pattern' => 'ajax/fileupload', | |
'method' => 'POST', | |
'action' => function () { | |
$site = site(); | |
if ( !$site->user() ) return response::json( array( 'login' => '0' ) ); | |
if ( kirby()->request()->ajax() ) { | |
$page = $site->find( $_POST[ 'path' ] ); | |
$root = $page->root(); | |
$name = $_FILES[ 'file' ][ 'name' ]; | |
$upload = new Upload( $root . DS . $name, array( | |
'input' => 'file', | |
'overwrite' => true | |
)); | |
if ( $file = $upload->file() ) { | |
// Structured field | |
$data = array( | |
'filename' => $file->filename(), | |
); | |
// add to structured field | |
$files = $page->files()->yaml(); | |
$files[] = $data; | |
$files = yaml::encode( $files ); | |
try { | |
$page->update( array( 'files' => $files )); | |
} catch ( Exception $e ) { | |
$alert = $e->getMessage(); | |
} | |
$data[ 'filename' ] = $page->image( $file->filename() )->url(); | |
return response::json( $data ); | |
} else { | |
dump( $upload->error() ); | |
} | |
} else { | |
return page( $site->errorPage() ); | |
} | |
} | |
), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment