function store_file($challenge_id, $file) { if ($file['error']) { message_error('Could not upload file: ' . file_upload_error_description($file['error'])); } if ($file['size'] > max_file_upload_size()) { message_error('File too large.'); } $file_id = db_insert('files', array('added' => time(), 'added_by' => $_SESSION['id'], 'title' => $file['name'], 'size' => $file['size'], 'challenge' => $challenge_id)); if (file_exists(CONFIG_PATH_FILE_UPLOAD . $file_id)) { message_error('File already existed! This should never happen!'); } // do we put the file on AWS S3? if (CONFIG_AWS_S3_KEY_ID && CONFIG_AWS_S3_SECRET && CONFIG_AWS_S3_BUCKET) { try { // Instantiate the S3 client with your AWS credentials $client = S3Client::factory(array('key' => CONFIG_AWS_S3_KEY_ID, 'secret' => CONFIG_AWS_S3_SECRET)); $file_key = '/challenges/' . $file_id; // Upload an object by streaming the contents of a file $result = $client->putObject(array('Bucket' => CONFIG_AWS_S3_BUCKET, 'Key' => $file_key, 'SourceFile' => $file['tmp_name'])); // We can poll the object until it is accessible $client->waitUntil('ObjectExists', array('Bucket' => CONFIG_AWS_S3_BUCKET, 'Key' => $file_key)); } catch (Exception $e) { message_error('Caught exception uploading file to S3: ' . $e->getMessage()); } } else { move_uploaded_file($file['tmp_name'], CONFIG_PATH_FILE_UPLOAD . $file_id); if (!file_exists(CONFIG_PATH_FILE_UPLOAD . $file_id)) { delete_file($file_id); message_error('File upload failed!'); } } }
form_end(); echo ' </td> </tr> '; } echo ' </tbody> </table> '; form_start(CONFIG_SITE_ADMIN_RELPATH . 'actions/edit_challenge', '', 'multipart/form-data'); form_file('file'); form_hidden('action', 'upload_file'); form_hidden('id', $_GET['id']); form_button_submit('Upload file'); echo 'Max file size: ', bytes_to_pretty_size(max_file_upload_size()); form_end(); section_subhead('Hints'); echo ' <table id="hints" class="table table-striped table-hover"> <thead> <tr> <th>Added</th> <th>Hint</th> <th>Manage</th> </tr> </thead> <tbody> '; $hints = db_select_all('hints', array('id', 'added', 'body'), array('challenge' => $_GET['id'])); foreach ($hints as $hint) {