本文整理汇总了PHP中Attachment::create方法的典型用法代码示例。如果您正苦于以下问题:PHP Attachment::create方法的具体用法?PHP Attachment::create怎么用?PHP Attachment::create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Attachment
的用法示例。
在下文中一共展示了Attachment::create方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
public function run()
{
$faker = Faker::create();
foreach (range(1, 10) as $index) {
Attachment::create([]);
}
}
示例2: OnFileUpload
/**
* This function should be called when an attachment is uploaded. It will
* save the attachment to the appropriate place on the disk, and create a
* database entry for the file.
*
* @param array $p_fileVar
* <pre>
* The variable from the $_FILES array. The array specifies the following:
* $a["name"] = original name of the file.
* $a["type"] = the MIME type of the file
* $a["tmp_name"] = the temporary storage location on disk of the file
* $a["size"] = size of the file, in bytes (not required)
* $a["error"] = 0 (zero) if there was no error
* </pre>
*
* @param array $p_attributes
* Optional attributes which are stored in the database.
* Indexes can be the following: 'content_disposition', 'fk_language_id', 'http_charset', 'fk_user_id'
*
* @param int $p_id
* If the attachment already exists and we just want to update it, specify the
* current ID here.
*
* @param bool $p_uploaded
* If the attachment was uploaded with other mechanism (ex: plUploader)
* this is set so that the single upload file from article functionality is still secured.
*
* @return mixed
* The Attachment object that was created or updated.
* Return a PEAR_Error on failure.
*/
public static function OnFileUpload($p_fileVar, $p_attributes, $p_id = null, $p_uploaded = false)
{
if (!is_array($p_fileVar)) {
return null;
}
// Verify its a valid file.
$filesize = filesize($p_fileVar['tmp_name']);
if ($filesize === false) {
return new PEAR_Error("Attachment::OnFileUpload(): invalid parameters received.");
}
// Are we updating or creating?
if (!is_null($p_id)) {
// Updating the attachment
$attachment = new Attachment($p_id);
$attachment->update($p_attributes);
// Remove the old file because
// the new file may have a different file extension.
if (file_exists($attachment->getStorageLocation())) {
unlink($attachment->getStorageLocation());
}
} else {
// Creating the attachment
$attachment = new Attachment();
$attachment->create($p_attributes);
$attachment->setProperty('time_created', 'NULL', true, true);
}
$attachment->setProperty('file_name', $p_fileVar['name'], false);
$attachment->setProperty('mime_type', $p_fileVar['type'], false);
$attachment->setProperty('size_in_bytes', $p_fileVar['size'], false);
$extension = "";
$fileParts = explode('.', $p_fileVar['name']);
if (count($fileParts) > 1) {
$extension = array_pop($fileParts);
$attachment->setProperty('extension', $extension, false);
}
$target = $attachment->getStorageLocation();
$attachment->makeDirectories();
ob_start();
var_dump(is_uploaded_file($p_fileVar['tmp_name']));
$dump = ob_get_clean();
/**
* for security reason
* for file uploaded normal not with other mechanism (ex: plUploader)
* we still need the move_uploaded_file functionality
*/
if (!$p_uploaded && !move_uploaded_file($p_fileVar['tmp_name'], $target)) {
$attachment->delete();
return new PEAR_Error(camp_get_error_message(CAMP_ERROR_CREATE_FILE, $target), CAMP_ERROR_CREATE_FILE);
}
// if the file was uploaded with other mechanism (ex: plUploader) use rename(move) functionality
if ($p_uploaded && !rename($p_fileVar['tmp_name'], $target)) {
$attachment->delete();
return new PEAR_Error(camp_get_error_message(CAMP_ERROR_CREATE_FILE, $target), CAMP_ERROR_CREATE_FILE);
}
chmod($target, 0644);
$attachment->commit();
return $attachment;
}
示例3: postMedia
/**
* Main media upload API
*/
protected function postMedia($mediaType = 'user', $item_id = 0)
{
// Get corresponding item
switch ($mediaType) {
case 'message':
$item = Alert::find($item_id);
$allowedTypes = array('alert_picture');
break;
case 'user':
$item = User::find($item_id);
$allowedTypes = array('profile_picture', 'cover_picture');
break;
case 'brand':
$item = Brand::find($item_id);
$allowedTypes = array('logo_picture', 'cover_picture');
break;
default:
throw new Exception('Invalid media type:' . $mediaType);
break;
}
if (!$item) {
App::abort(404);
}
// Gathering and validate upload information
$uploadFiles = Input::file();
$uploadType = key($uploadFiles);
if (!Input::hasFile($uploadType)) {
array_shift($uploadFiles);
$uploadType = key($uploadFiles);
}
if (!in_array($uploadType, $allowedTypes)) {
return Redirect::back()->with('warning', 'Invalid upload name : ' . $uploadType);
}
// Early mime validation
$validType = false;
if ($mime = Input::file($uploadType)->getMimetype()) {
$validType = strpos($mime, 'image') === 0;
}
if (!$validType) {
return Redirect::back()->with('warning', 'Invalid mime type : ' . $mime);
}
// Prepare uploader
$fs = new Filesystem();
$storage = new UploadFileSystem($this->media_directory);
$file = new UploadFile($uploadType, $storage);
// Set to item's media slug
$mediaName = $uploadType . '_' . $mediaType . '_' . $item->id;
$file->setName($mediaName);
// Validate file upload
// MimeType List => http://www.webmaster-toolkit.com/mime-types.shtml
$file->addValidations(array(new UploadMimetype($mime), new UploadSize('5M')));
// Access data about the file that has been uploaded
$data = array('path' => $this->media_directory . '/' . $file->getNameWithExtension(), 'name' => $file->getNameWithExtension(), 'extension' => $file->getExtension(), 'mime' => $file->getMimetype(), 'size' => $file->getSize(), 'md5' => $file->getMd5(), 'dimensions' => $file->getDimensions());
// Try to upload file
try {
// If it already there, remove
if ($fs->exists($data['path'])) {
$fs->delete($data['path']);
}
$file->upload();
chmod($data['path'], 0777);
//why not 0644 or 0664
$attachment = Attachment::create(array('mime' => $data['mime'], 'path' => $data['path'], 'url' => URL::to('/media/' . $mediaType . '/' . $uploadType . '/' . $item->id)));
foreach ($item->attachments as $previousAttachment) {
if ($previousAttachment->pivot->type == $uploadType) {
$item->attachments()->detach($previousAttachment->id);
}
}
$item->attachments()->save($attachment, array('type' => $uploadType));
return Redirect::back();
} catch (Exception $e) {
// Fail!
$error = $file->getErrors();
return Redirect::back()->with('warning', current($error));
}
}