本文整理汇总了PHP中UploadedFile类的典型用法代码示例。如果您正苦于以下问题:PHP UploadedFile类的具体用法?PHP UploadedFile怎么用?PHP UploadedFile使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UploadedFile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getImageOriginal
public function getImageOriginal()
{
if (file_exists($this->pathForIcons . "/apple-icon-original.png")) {
$this->imageOriginal = UploadedFile::getInstance($model, 'imageOriginal');
}
return $this->imageOriginal;
}
示例2: actionUpload
public function actionUpload()
{
$model = new UploadForm();
if (Yii::$app->request->isPost) {
$tanggal = date("YmdHis");
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->file && $model->validate()) {
$model->file->saveAs('uploads/data_prospek/' . $model->file->baseName . $tanggal . '.' . $model->file->extension);
//$this->actionExupload();
}
}
return $this->render('upload', ['model' => $model]);
}
示例3: actionCreate
public function actionCreate()
{
$model = new MeForm();
if ($model->load(Yii::$app->request->post())) {
$model->file = UploadedFile::getInstance($model, 'file');
$photoName = 'uploads/' . $model->title . '.' . $model->file->extension;
$model->file->saveAs($photoName);
$model->photo = $photoName;
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', ['model' => $model]);
}
}
示例4: run
/**
* @inheritdoc
*/
public function run()
{
$model = $this->model;
if ($model->load($_POST)) {
$file = UploadedFile::getInstance($model, 'file');
$model->fileName = $file->getBaseName() . '.' . $file->getExtension();
if ($model->validate()) {
$uploaded = $file->saveAs(Yii::getAlias('@app/web/imgs') . "/{$model->path}");
} else {
print_r($model->getErrors());
}
}
Yii::$app->response->format = Response::FORMAT_JSON;
return ['files' => [['url' => "/imgs/{$model->path}", 'name' => 'name', 'type' => 'type', 'size' => 18932, 'deleteUrl' => 'url', 'deleteType' => 'DELETE']]];
}
示例5: saveUploadedFile
public function saveUploadedFile($model, $attribute, $fileName = null, $obj = false, $returnObj = false)
{
$result = UploadedFile::getInstance($model, $attribute);
if ($fileName == null) {
$fileName = time() . '.' . $result->getExtension();
}
if (strpos($fileName, "/") !== false) {
$fileSubPath = substr($fileName, 0, strrpos($fileName, "/"));
if (!is_dir($this->basePath . '/' . $fileSubPath)) {
mkdir($this->basePath . '/' . $fileSubPath, $this->filePermission, true);
}
}
if ($result->saveAs($this->basePath . "/" . $fileName)) {
chmod($this->basePath . "/" . $fileName, $this->filePermission);
}
return $returnObj ? $returnObj : $fileName;
}
示例6: restoreAction
function restoreAction()
{
$backupFile = new UploadedFile("backupFile");
if (!$backupFile->wasUploaded()) {
return;
}
$gzipMode = $this->request->fileType == "gzip";
$fileName = $backupFile->getTempName();
$fp = $gzipMode ? gzopen($fileName, "r") : fopen($fileName, "r");
$inString = false;
$query = "";
while (!feof($fp)) {
$line = $gzipMode ? gzgets($fp) : fgets($fp);
if (!$inString) {
$isCommentLine = false;
foreach (array("#", "--") as $commentTag) {
if (strpos($line, $commentTag) === 0) {
$isCommentLine = true;
}
}
if ($isCommentLine || trim($line) == "") {
continue;
}
}
$deslashedLine = str_replace('\\', '', $line);
if ((substr_count($deslashedLine, "'") - substr_count($deslashedLine, "\\'")) % 2) {
$inString = !$inString;
}
$query .= $line;
if (substr_compare(rtrim($line), ";", -1) == 0 && !$inString) {
$this->database->sqlQuery($query);
$query = "";
}
}
}
示例7: moveUploadedFile
public function moveUploadedFile(UploadedFile $file, $uploadBasePath)
{
$originalName = $file->getOriginalName();
// use filemtime() to have a more determenistic way to determine the subpath, otherwise its hard to test.
$relativePath = date('Y-m', filemtime($this->file->getPath()));
$targetFileName = $relativePath . DIRECTORY_SEPARATOR . $originalName;
$targetFilePath = $uploadBasePath . DIRECTORY_SEPARATOR . $targetFileName;
$ext = $this->file->getExtension();
$i = 1;
while (file_exists($targetFilePath) && md5_file($file->getPath()) != md5_file($targetFilePath)) {
if ($ext) {
$prev = $i == 1 ? "" : $i;
$targetFilePath = $targetFilePath . str_replace($prev . $ext, $i++ . $ext, $targetFilePath);
} else {
$targetFilePath = $targetFilePath . $i++;
}
}
$targetDir = $uploadBasePath . DIRECTORY_SEPARATOR . $relativePath;
if (!is_dir($targetDir)) {
$ret = mkdir($targetDir, umask(), true);
if (!$ret) {
throw new \RuntimeException("Could not create target directory to move temporary file into.");
}
}
$file->move($targetDir, basename($targetFilePath));
return str_replace($uploadBasePath . DIRECTORY_SEPARATOR, "", $targetFilePath);
}
示例8: s3Upload
/**
* Upload file to Amazon s3
*
* @param UploadedFile $file
* @param String $subdirectory
* @param String $type
* @return Array $uploadedFile
*/
private function s3Upload($file, $subdirectory, $type)
{
$client_original_name = $file->getClientOriginalName();
$fileName = time() . '_' . $client_original_name;
$destinationPath = 'uploads/' . $subdirectory;
$path = $destinationPath . '/' . $fileName;
$image = Image::make($file->getRealPath());
switch ($type) {
case 'profile_photo':
$image->fit(128, 128, function ($constraint) {
$constraint->upsize();
});
break;
case 'profile_cover':
$image->resize(1440, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
break;
}
$stream = $image->stream();
$s3 = Storage::disk('s3');
$s3->put($path, $stream->__toString(), 'public');
$client = $s3->getDriver()->getAdapter()->getClient();
$public_url = $client->getObjectUrl(env('S3_BUCKET'), $path);
$original_name = pathinfo($client_original_name, PATHINFO_FILENAME);
$uploadedFile = ['original_name' => $original_name, 'file_name' => $fileName, 'public_url' => $public_url, 'type' => $type];
return $uploadedFile;
}
示例9: testMoveTo
/**
* @depends testConstructor
* @param UploadedFile $uploadedFile
* @return UploadedFile
*/
public function testMoveTo(UploadedFile $uploadedFile)
{
$tempName = uniqid('file-');
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $tempName;
$uploadedFile->moveTo($path);
$this->assertFileExists($path);
unlink($path);
return $uploadedFile;
}
示例10: saveFile
protected function saveFile(UploadedFile $file)
{
$upload_dir = public_path('uploads/');
$file_name = 'file-' . date('dmY-His') . '.' . $file->getClientOriginalExtension();
try {
if ($file->move($upload_dir, $file_name)) {
return $file_name;
} else {
return false;
}
} catch (\Exception $ex) {
return abort(500, $ex->getMessage());
}
}
示例11: upload_file
/**
* Handles the uploading and db entry for a file
*
* @param UploadedFile $file
* @return array
*/
function upload_file($file)
{
global $db;
// Handle file errors
if ($file->error) {
throw new UploadException($file->error);
}
// Check if a file with the same hash and size (a file which is the same) does already exist in
// the database; if it does, delete the file just uploaded and return the proper link and data.
$q = $db->prepare('SELECT filename, COUNT(*) AS count FROM files WHERE hash = (:hash) ' . 'AND size = (:size)');
$q->bindValue(':hash', $file->get_sha1(), PDO::PARAM_STR);
$q->bindValue(':size', $file->size, PDO::PARAM_INT);
$q->execute();
$result = $q->fetch();
if ($result['count'] > 0) {
unlink($file->tempfile);
return array('hash' => $file->get_sha1(), 'name' => $file->name, 'url' => POMF_URL . $result['filename'], 'size' => $file->size);
}
// Generate a name for the file
$newname = generate_name($file);
// Attempt to move it to the static directory
if (move_uploaded_file($file->tempfile, POMF_FILES_ROOT . $newname)) {
// Need to change permissions for the new file to make it world readable
if (chmod(POMF_FILES_ROOT . $newname, 0644)) {
// Add it to the database
if (empty($_SESSION['id'])) {
// Query if user is NOT logged in
$q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, ' . 'expire, delid) VALUES (:hash, :orig, :name, :size, :date, ' . ':exp, :del)');
} else {
// Query if user is logged in (insert user id together with other data)
$q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, ' . 'expire, delid, user) VALUES (:hash, :orig, :name, :size, ' . ':date, :expires, :delid, :user)');
$q->bindValue(':user', $_SESSION['id'], PDO::PARAM_INT);
}
// Common parameters binding
$q->bindValue(':hash', $file->get_sha1(), PDO::PARAM_STR);
$q->bindValue(':orig', strip_tags($file->name), PDO::PARAM_STR);
$q->bindValue(':name', $newname, PDO::PARAM_STR);
$q->bindValue(':size', $file->size, PDO::PARAM_INT);
$q->bindValue(':date', date('Y-m-d'), PDO::PARAM_STR);
$q->bindValue(':exp', null, PDO::PARAM_STR);
$q->bindValue(':del', sha1($file->tempfile), PDO::PARAM_STR);
$q->execute();
return array('hash' => $file->get_sha1(), 'name' => $file->name, 'url' => POMF_URL . $newname, 'size' => $file->size);
} else {
throw new Exception('Failed to change file permissions', 500);
}
} else {
throw new Exception('Failed to move file to destination', 500);
}
}
示例12: beforeInit
protected function beforeInit()
{
$this->data = $this->request->getArray(array("subject", "text", "mailsPerMinute", "newsletterType", 'fromEmail'));
$message = $this->customMessage->findByPk("newsletterFooter");
$this->data['newsletterFooterDescription'] = $message->description;
if ($this->data['newsletterType'] == 'csv') {
$csvFile = new UploadedFile('csvFile');
if ($csvFile->wasUploaded()) {
$this->data['emails'] = array_values(file($csvFile->getTempName(), FILE_IGNORE_NEW_LINES));
} else {
$this->data['emails'] = array();
}
}
}
示例13: realImgSrc
public static function realImgSrc($imgSrc, $type = "main", $size = "normal", $title = '')
{
if ($imgSrc) {
$path = "/uploads/images_thumbs/" . UploadedFile::fileNameToPath($imgSrc);
} else {
$imgSrc = "DefaultMainPhoto.jpg";
$path = "/templates/arfooo/images/";
}
switch ($size) {
case "small":
$imgSrc = "s" . $imgSrc;
break;
case "medium":
$imgSrc = "m" . $imgSrc;
break;
case "nano":
$imgSrc = "n" . $imgSrc;
break;
}
if ($title) {
$path .= NameTool::strToAscii($title) . '-';
}
$imgSrc = $path . $imgSrc;
return AppRouter::getResourceUrl($imgSrc);
}
示例14: createFromGlobals
/**
* Instantiate request from php _SERVER variable
* @param array server
*/
public static function createFromGlobals()
{
$server = $_SERVER;
$uriParts = parse_url($server['REQUEST_URI']);
$uriParts['host'] = $server['SERVER_NAME'];
$uriParts['port'] = $server['SERVER_PORT'];
$uriParts['scheme'] = isset($server['REQUEST_SCHEME']) ? $server['REQUEST_SCHEME'] : (isset($server['HTTPS']) && $server['HTTPS'] == 'on' ? 'https' : 'http');
if (function_exists('getallheaders')) {
// a correct case already
$apacheHeaders = getallheaders();
foreach ($apacheHeaders as $header => $value) {
$headers[$header] = array_map('trim', explode(',', $value));
}
} else {
$headers = array();
// normalize the header key
foreach ($server as $key => $value) {
if (substr($key, 0, 5) != 'HTTP_') {
continue;
}
$name = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
$headers[$name] = array_map('trim', explode(',', $value));
}
}
$request = new static($server['REQUEST_METHOD'], new Uri($uriParts), $headers, Stream::createFromContents(file_get_contents('php://input')), $server, $_COOKIE, UploadedFile::createFromGlobals($_FILES));
if ($server['REQUEST_METHOD'] == 'POST' && in_array($request->getMediaType(), array('application/x-www-form-urlencoded', 'multipart/form-data'))) {
$request->setParsedBody($_POST);
}
return $request;
}
示例15: actionUpload
/**
* Action to upload a file to an asset
* @return void Request ends
*/
public function actionUpload()
{
$input = craft()->request->getPost();
$file = UploadedFile::getInstanceByName('file');
$folder = craft()->assets->findFolder(array('sourceId' => $input['sourceId']));
craft()->assets->insertFileByLocalPath($file->getTempName(), $file->getName(), $folder->id, AssetConflictResolution::KeepBoth);
// DropzonePlugin::log($file->getName(), LogLevel::Info);
craft()->end();
}