public function indexAction() { $messages = array(); $form = new BootstrapForm('bootstrap'); if ($this->request->isPost()) { $post = $this->request->getPost(); $form->setData($post); $messages[] = _a('Form submitted successfully.'); //$rename = 'demo_for_upload_%random%'; $rename = function ($name) { //$extension = pathinfo($name, PATHINFO_EXTENSION); $name = 'test_for_upload_' . $name; return $name; }; $uploader = new Upload(array('rename' => $rename)); $uploader->setExtension('jpg,png,gif,txt,zip,rar'); //->setRename('tmp.%random%'); //->setImageSize(array('maxWidth' => 600, 'maxHeight' => 500)); if ($uploader->isValid()) { $uploader->receive(); $file = $uploader->getUploaded('upload_demo'); $messages[] = sprintf(_a('File uploaded and saved as %s'), $file); } } $this->view()->assign(array('form' => $form, 'messages' => $messages)); }
/** * Upload files * * @param array $options * @param bool $doUpload * * @return Upload * @see Pi\File\Upload */ public function upload(array $options = array(), $doUpload = false) { $uploader = new Upload($options); if ($doUpload && $uploader->isValid()) { $uploader->receive(); } return $uploader; }
/** * Process upload image by AJAX */ public function uploadAction() { Pi::service('log')->mute(); $module = $this->getModule(); $config = Pi::config('', $module); $return = array('status' => false); $fakeId = $this->params('fake_id', 0); // Checking whether ID is empty if (empty($fakeId)) { $return['message'] = _a('Invalid fake ID!'); echo json_encode($return); exit; } $rawInfo = $this->request->getFiles('upload'); // Rename $ext = strtolower(pathinfo($rawInfo['name'], PATHINFO_EXTENSION)); $rename = $fakeId . '.' . $ext; // Get path to store $destination = Media::getTargetDir('author', $module, true, false); $uploader = new UploadHandler(array('destination' => Pi::path($destination), 'rename' => $rename)); $uploader->setExtension($config['image_extension'])->setSize($config['max_media_size']); // Checking whether uploaded file is valid if (!$uploader->isValid()) { $return['message'] = implode(', ', $uploader->getMessages()); echo json_encode($return); exit; } $uploader->receive(); $fileName = $destination . '/' . $rename; // Resolve allowed image extension $imageSize = array(); $imageSizeRaw = getimagesize(Pi::path($fileName)); $imageSize['w'] = $imageSizeRaw[0]; $imageSize['h'] = $imageSizeRaw[1]; $uploadInfo = array('tmp_name' => $fileName, 'w' => $imageSize['w'], 'h' => $imageSize['h']); // Save info to session $session = Media::getUploadSession($module, 'author'); $session->{$fakeId} = $uploadInfo; // Prepare return data $return['data'] = array_merge(array('originalName' => $rawInfo['name'], 'size' => $rawInfo['size'], 'preview_url' => Pi::url($fileName), 'basename' => basename($fileName), 'type' => $ext, 'id' => $fakeId, 'filename' => $fileName), $imageSize); $return['status'] = true; echo json_encode($return); exit; }
/** * Process upload image by AJAX */ public function uploadAction() { $module = $this->getModule(); $config = Pi::user()->config(''); $result = array('status' => false); $fakeId = $this->params('fake_id', 0); // Checking whether ID is empty if (empty($fakeId)) { $result['message'] = __('Invalid token!'); return $result; } $rawInfo = $this->request->getFiles('upload'); // Rename $ext = strtolower(pathinfo($rawInfo['name'], PATHINFO_EXTENSION)); $rename = $fakeId . '.' . $ext; // Get path to store $location = $this->config('path_tmp') ?: sprintf('upload/%s/tmp', $this->getModule()); //$destination = Pi::path($location); $uploadConfig = Pi::service('avatar')->getOption('upload'); $extension = implode(',', $uploadConfig['extension']); $maxFile = $config['max_size'] ? $config['max_size'] * 1024 : 0; $maxSize = array(); if ($config['max_avatar_width']) { $maxSize['width'] = (int) $config['max_avatar_width']; } if ($config['max_avatar_height']) { $maxSize['height'] = (int) $config['max_avatar_height']; } $upload = new UploadHandler(); $upload->setDestination($location)->setRename($rename)->setExtension($extension); if ($maxFile) { $upload->setSize($maxFile); } if ($maxSize) { $upload->setImageSize($maxSize); } // Get raw file name if (empty($rawInfo)) { $content = $this->request->getContent(); preg_match('/filename="(.+)"/', $content, $matches); $rawName = $matches[1]; } else { $rawName = null; } // Checking whether uploaded file is valid if (!$upload->isValid($rawName)) { $result['message'] = implode(', ', $upload->getMessages()); return $result; } $upload->receive(); //$fileName = $upload->getDestination() . '/' . $rename; $fileName = $location . '/' . $upload->getUploaded(); // Resolve allowed image extension $imageSize = array(); $imageSizeRaw = getimagesize(Pi::path($fileName)); $imageSize['w'] = $imageSizeRaw[0]; $imageSize['h'] = $imageSizeRaw[1]; $uploadInfo = array('tmp_name' => $fileName, 'w' => $imageSize['w'], 'h' => $imageSize['h']); // Save info to session $session = $this->getUploadSession($module, 'avatar'); $session->{$fakeId} = $uploadInfo; // Prepare return data $result['data'] = array_merge(array('originalName' => $rawInfo['name'], 'size' => $rawInfo['size'], 'preview_url' => Pi::url($fileName), 'basename' => basename($fileName), 'type' => $ext, 'id' => $fakeId, 'filename' => $fileName), $imageSize); $result['status'] = true; //return $result; for ie10+ bug Pi::service('log')->mute(); echo json_encode($result); exit; }
/** * Processing media uploaded. */ public function uploadAction() { Pi::service('log')->mute(); $module = $this->getModule(); $config = Pi::config('', $module); $return = array('status' => false); $id = $this->params('id', 0); $fakeId = $this->params('fake_id', 0) ?: uniqid(); $type = $this->params('type', 'attachment'); $formName = $this->params('form_name', 'upload'); // Checking whether ID is empty if (empty($id) && empty($fakeId)) { $return['message'] = _a('Invalid ID!'); return json_encode($return); exit; } $width = $this->params('width', 0); $height = $this->params('height', 0); $rawInfo = $this->request->getFiles($formName); $ext = strtolower(pathinfo($rawInfo['name'], PATHINFO_EXTENSION)); $rename = $id ?: $fakeId . '.' . $ext; $allowedExtension = $type == 'image' ? $config['image_extension'] : $config['media_extension']; $mediaSize = $type == 'image' ? $config['max_image_size'] : $config['max_media_size']; $destination = Media::getTargetDir('media', $module, true, true); $uploader = new UploadHandler(array('destination' => Pi::path($destination), 'rename' => $rename)); $uploader->setExtension($allowedExtension)->setSize($mediaSize); // Get raw file name $rawName = null; if (empty($rawInfo)) { $content = $this->request->getContent(); preg_match('/filename="(.+)"/', $content, $matches); $rawName = $matches[1]; } // Checking whether uploaded file is valid if (!$uploader->isValid($rawName)) { $return['message'] = implode(', ', $uploader->getMessages()); echo json_encode($return); exit; } $uploader->receive(); $fileName = $destination . '/' . $rename; $rawName = $rawName ?: substr($rawInfo['name'], 0, strrpos($rawInfo['name'], '.')); // Resolve allowed image extension $imageExt = explode(',', $config['image_extension']); foreach ($imageExt as &$value) { $value = strtolower(trim($value)); } // Scale image if file is image file $uploadInfo['tmp_name'] = $fileName; $uploadInfo['raw_name'] = $rawName; $imageSize = array(); if (in_array($ext, $imageExt)) { $scaleImageSize = $this->scaleImageSize(Pi::path($fileName), $config); $uploadInfo['w'] = $width ?: $scaleImageSize['w']; $uploadInfo['h'] = $height ?: $scaleImageSize['h']; Media::saveImage($uploadInfo); $imageSizeRaw = getimagesize(Pi::path($fileName)); $imageSize['w'] = $imageSizeRaw[0]; $imageSize['h'] = $imageSizeRaw[1]; } // Save uploaded media if ($id) { $row = $this->getModel('media')->find($id); if ($row->url && $row->url != $fileName) { unlink(Pi::path($row->url)); } $row->url = $fileName; $row->type = $ext; $row->size = filesize(Pi::path($fileName)); $row->meta = json_encode($imageSize); $row->save(); } else { // Or save info to session $session = Media::getUploadSession($module, 'media'); $session->{$fakeId} = $uploadInfo; } // Prepare return data $return['data'] = array_merge(array('originalName' => $rawInfo['name'], 'size' => $rawInfo['size'], 'preview_url' => Pi::url($fileName), 'basename' => basename($fileName), 'type' => $ext, 'id' => $id ?: $fakeId, 'filename' => $fileName), $imageSize); $return['status'] = true; echo json_encode($return); exit; }
public function uploadAction() { //Pi::service('log')->mute(); $return = array('status' => 1, 'message' => '', 'image' => ''); $rename = $this->tmpPrefix . '%random%'; /**#@+ * Just for demo for anonymous callback */ /* $rename = function ($name) { $pos = strrpos($name, '.'); if (false !== $pos) { $extension = substr($name, $pos); $name = substr($name, 0, $pos); } else { $extension = ''; } $newName = $name . '.random-' .uniqid() . '.' . $extension; return $newName; }; */ /**#@-*/ $uploader = new Upload(array('rename' => $rename)); $uploader->setExtension('jpg,png,gif'); //->setRename('tmp.%random%'); //->setImageSize(array('maxWidth' => 600, 'maxHeight' => 500)); if ($uploader->isValid()) { $uploader->receive(); $file = $uploader->getUploaded('image'); $return['image'] = $file; } else { $messages = $uploader->getMessages(); $return = array('status' => 0, 'message' => implode('; ', $messages)); } return $return; }
/** * Upload a doc and save meta * * @TODO not completed * * @param array $params * @param string $method * * @return int doc id */ public function upload(array $params, $method = 'POST') { @ignore_user_abort(true); @set_time_limit(0); $options = Pi::service('media')->getOption('local', 'options'); $rootUri = $options['root_uri']; $rootPath = $options['root_path']; $path = $options['locator']['path']; if ($path instanceof Closure) { $relativePath = $path(); } else { $relativePath = $path; } $destination = $rootPath . '/' . $relativePath; Pi::service('file')->mkdir($destination); $rename = $options['locator']['file']; $success = false; switch (strtoupper($method)) { // For remote post case 'POST': $uploader = new Upload(array('destination' => $destination, 'rename' => $rename($params['filename']))); $maxSize = Pi::config('max_size', $this->module); if ($maxSize) { $uploader->setSize($maxSize * 1024); } $result = $uploader->isValid(); if ($result) { $uploader->receive(); $filename = $uploader->getUploaded(); if (is_array($filename)) { $filename = current($filename); } // Fetch file attributes $fileinfoList = $uploader->getFileInfo(); $fileinfo = current($fileinfoList); if (!isset($params['mimetype'])) { $params['mimetype'] = $fileinfo['type']; } if (!isset($params['size'])) { $params['size'] = $fileinfo['size']; } $success = true; } break; // For remote put // For remote put case 'PUT': $putdata = fopen('php://input', 'r'); $filename = $rename($params['filename']); $target = $destination . '/' . $filename; $fp = fopen($target, 'w'); while ($data = fread($putdata, 1024)) { fwrite($fp, $data); } fclose($fp); fclose($putdata); $success = true; break; // For local // For local case 'MOVE': $filename = $rename($params['filename']); $target = $destination . '/' . $filename; Pi::service('file')->copy($params['file'], $target); unset($params['file']); $success = true; break; default: break; } if ($success) { $params['url'] = $rootUri . '/' . $relativePath . '/' . $filename; $params['path'] = $rootPath . '/' . $relativePath . '/' . $filename; $result = $this->add($params); } else { $result = 0; } return $result; }