本文整理汇总了PHP中Uploader::setup方法的典型用法代码示例。如果您正苦于以下问题:PHP Uploader::setup方法的具体用法?PHP Uploader::setup怎么用?PHP Uploader::setup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Uploader
的用法示例。
在下文中一共展示了Uploader::setup方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: beforeSave
/**
* Before saving the data, try uploading the image, if successful save to database.
*
* @access public
* @param Model $model
* @return mixed
*/
public function beforeSave(Model $model)
{
if (empty($model->data[$model->alias])) {
return true;
}
foreach ($model->data[$model->alias] as $field => $file) {
if (empty($this->_attachments[$model->alias][$field])) {
continue;
}
$attachment = $this->_attachments[$model->alias][$field];
$attachment = $this->callback($model, 'beforeUpload', $attachment);
$data = array();
// Not a form upload, so lets treat it as an import
if (is_string($file) && !empty($file)) {
$attachment['importFrom'] = $file;
}
// Should we continue if a file threw errors during upload?
if (empty($file['tmp_name']) || isset($file['error']) && $file['error'] == UPLOAD_ERR_NO_FILE || is_string($file) && empty($attachment['importFrom'])) {
if ($attachment['stopSave'] && !$attachment['allowEmpty']) {
return false;
} else {
if ($attachment['allowEmpty']) {
if (empty($attachment['defaultPath'])) {
unset($model->data[$model->alias][$attachment['dbColumn']]);
} else {
$model->data[$model->alias][$attachment['dbColumn']] = $attachment['defaultPath'];
}
continue;
}
}
}
// Save model method for formatting function
if (!empty($attachment['name']) && method_exists($model, $attachment['name'])) {
$attachment['name'] = array($model, $attachment['name']);
}
// Setup instances
$this->uploader->setup($attachment);
$this->s3 = $this->s3($attachment['s3']);
// Upload or import the file and attach to model data
$uploadResponse = $this->upload($file, $attachment, array('overwrite' => $attachment['overwrite'], 'name' => $attachment['name'], 'append' => $attachment['append'], 'prepend' => $attachment['prepend']));
$uploaderOptions = array('uploadDir' => $this->uploader->uploadDir, 'baseDir' => $this->uploader->baseDir, 'tempDir' => $this->uploader->tempDir);
if (empty($uploadResponse)) {
$model->invalidate($field, __d('uploader', 'There was an error uploading this file, please try again.'));
return false;
}
$basePath = $this->transfer($uploadResponse['path']);
$data[$attachment['dbColumn']] = $attachment['saveAsFilename'] && $this->s3 === null ? basename($basePath) : $basePath;
$toDelete = array();
$lastPath = $basePath;
// Apply image transformations
if ($attachment['transforms']) {
foreach ($attachment['transforms'] as $options) {
$options['field'] = $field;
$options = $this->callback($model, 'beforeTransform', $options);
$method = $options['method'];
if (!method_exists($this->uploader, $method)) {
trigger_error(sprintf('Uploader.Attachment::beforeSave(): "%s" is not a defined transformation method.', $method), E_USER_WARNING);
return false;
}
// Apply custom options for transform
$this->uploader->setup($options);
// Transform image
$transformResponse = $this->uploader->{$method}($options);
// Rollback uploaded files if one fails
if (empty($transformResponse)) {
foreach ($data as $path) {
$this->delete($path);
}
$model->invalidate($field, __d('uploader', 'An error occured during image %s transformation.', $method));
return false;
}
// Transform successful
$transformPath = $this->transfer($transformResponse);
$data[$options['dbColumn']] = $attachment['saveAsFilename'] && $this->s3 === null ? basename($transformPath) : $transformPath;
// Delete original if same column name and transform name are not the same file
if ($options['dbColumn'] == $attachment['dbColumn'] && $lastPath != $transformPath) {
$toDelete[] = $lastPath;
}
$lastPath = $transformPath;
// Reset to default settings
$this->uploader->setup($uploaderOptions);
}
}
// Delete old files if replacing them
if ($toDelete) {
foreach ($toDelete as $deleteFile) {
$this->delete($deleteFile);
}
}
// Apply meta columns
if ($attachment['metaColumns']) {
foreach ($attachment['metaColumns'] as $field => $column) {
if (isset($uploadResponse[$field]) && !empty($column)) {
//.........这里部分代码省略.........