public function saveFile(sfValidatedFile $validatedFile) { $file = $this->getFile(true); $file->setType($validatedFile->getType()); $file->setOriginalFilename($validatedFile->getOriginalName()); $file->getFileBin()->setBin(file_get_contents($validatedFile->getTempName())); $file->save(); }
public function setFromValidatedFile(sfValidatedFile $obj) { $this->setType($obj->getType()); $this->setOriginalFilename($obj->getOriginalName()); $this->setName(strtr($obj->generateFilename(), '.', '_')); $bin = new FileBin(); $bin->setBin(file_get_contents($obj->getTempName())); $this->setFileBin($bin); }
public function __construct($url, $callback = null) { register_shutdown_function(array(&$this, "__destruct")); $options = array(CURLOPT_USERAGENT => sfConfig::get('app_version_name') . ' (' . sfConfig::get('app_version_rev') . ' ' . sfConfig::get('app_version_comment') . ') ' . sfConfig::get('app_version_url')); // todo abstact this out $this->original_url = $url; $original_name = preg_replace('#^.*/#', '', $this->original_url); // we'll need to do this for redirects fixme $this->b = $b = new disconnectedCurl($url, $options); // probably want to have a way to gather statistics here try { $b->run($callback); // we should HEAD this first } catch (Exception $e) { throw limeException::createFromException($e, 'curl'); } if ($b->isRunning()) { throw new limeException('curl', 'still running'); } $mime_type = $b->getHeader('Content-Type'); // of course we shouldn't trust people to have their servers configured right; sigh todo $mime_type = preg_replace('/;.*$/', '', $mime_type); // remove stuff after the semicolon delimiter "charset=UTF-8" etc parent::__construct($original_name, $mime_type, $b->getFile(), filesize($b->getFile())); }
public function save($file, $fileMode = 0666, $create = true, $dirMode = 0777) { if (!$file) { throw new RuntimeException('You must set $file.'); } $mctime = str_replace('.', '', microtime(true)); $file_name = strtolower(substr($this->translitIt($file), 0, 40)) . '-' . $mctime . $this->getExtension(); $saved = parent::save($file_name, $fileMode, $create, $dirMode); return $saved; }
public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777) { // let the parent class save the file and do what it normally does $saved = parent::save($file, $fileMode, $create, $dirMode); /* Here we will put all our custom logic. Say, to create a thumbnail, or maybe manipulate it in whatever way you see fit. There are many possibilities here, so see the extended class sfValidatedFile in the Symfony 1.4 API Documentation to get a better idea about what you can manipulate and extend */ // return the saved file as normal return $saved; }
/** * Crop and save file to filesystem at the inputted folder * * @param sfValidatedFile $file * @param <string> $path * @param <integer> $width * @param <integer> $height * @param <boolean> $crop * @return <boolean> false | <string> saved file location */ public static function savePicture(sfValidatedFile $file, $path, $width, $height, $crop) { $filename = $file->getOriginalName(); $img = new Upload($file->getTempName()); $pos = strrpos($filename, '.') + 1; $type = substr($filename, $pos); $newName = $basename = md5(substr($filename, 0, --$pos) . rand(1111, 9999)); $img->file_new_name_body = $newName; $img->file_new_name_ext = $type; $img->file_is_image = true; $img->image_resize = true; $img->image_src_type = strtolower($type); // check for resize x if ($img->image_src_x > $width) { $img->image_x = $width; } else { $img->image_x = $img->image_src_x; } // check for resize y if ($img->image_src_y > $height) { $img->image_y = $height; } else { $img->image_y = $img->image_src_y; } $img->image_ratio_crop = $crop; $img->file_auto_rename = false; $img->file_overwrite = true; // execution $img->process($path); if (!file_exists($path . '/' . $img->file_dst_name)) { return false; } if (!chmod($path . '/' . $img->file_dst_name, 0777)) { return false; } else { return $path . '/' . $img->file_dst_name; } }
public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777) { parent::save($file, $fileMode, $create, $dirMode); $file_data = new majaxMediaFileData(); $data = fread(fopen($this->savedName, 'rb'), $this->size); $file_data->binary_data = $data; $file_data->save(); $file_info = new majaxMediaFileInfo(); $file_info->name = $this->originalName; $file_info->size = $this->size; $file_info->mime = $this->type; $file_info->file_data_id = $file_data->id; $file_info->save(); unlink($this->savedName); return $file_info->id; }
/** * Constructor * * @param string $name * @param string $tmp_name * @param string $type * @param string $error * @param string $size * @param string $path: The directory where the uploaded file will be saved. * Default is null. $path might be delegated through * a whole form validation process, where all * instances of sfFilebasePluginUploadedFiles be * generated by the validator and files moved by * the orm-form instance. * */ function __construct($originalName, $type, $tempName, $size, $path = null, $error = null) { parent::__construct($originalName, $type, $tempName, $size, $path); if (empty($error)) { $this->error = self::UPLOAD_ERR_OK; } else { $this->error = $error; } if (empty($size) || empty($type)) { $tmp_file = sfFilebasePlugin::getInstance()->getFilebaseFile($tempName); if (empty($size)) { $size = $tmp_file->getSize(); $this->size = $size; } if (empty($type)) { $type = $tmp_file->getMimeType('application/octet-stream'); $this->type = $type; } } }
/** * Generates a file name for uploaded asset. * * @param sfValidatedFile $file * @return string unique file name. */ public function generateFilenameFilename(sfValidatedFile $file) { $fs = new lyMediaFileSystem(); return $fs->generateUniqueFilename($file->getPath(), $file->getOriginalName()); }
/** * Take array of data for a single upload and create an image, assigning * content of $tags as tag (CSV string or single tag or array). * * This common logic is abstracted out so it can easily be used by other plugins * that require image upload but need to handle the actual upload logic themselves. * * @param array $upload * @param mixed $tags * @return sfImagePoolImage */ public static function createImageFromUpload($upload, $tags = null) { // upload was ok, mime type ok and file isn't too big so let's move it into the image pool // location and then create a new object for it. $file = new sfValidatedFile($upload['name'], $upload['type'], $upload['tmp_name'], $upload['size'], sfImagePoolPluginConfiguration::getBaseDir()); // this will generate the unique filename $new_filename = $file->save(); $image_data = array('original_filename' => $file->getOriginalName(), 'filename' => $new_filename, 'mime_type' => $file->getType()); $image = new sfImagePoolImage(); $image->fromArray($image_data); // now set tags if they've been supplied if ($tags) { $image->addTag($tags); } $image->save(); return $image; }
public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777) { // let the parent class save the file and do what it normally does $saved = parent::save($file, $fileMode, $create, $dirMode); return $saved; }
/** * Physically creates asset * * @param string $asset_path path to the asset original file * @param bool $move do move or just copy ? */ public function create(sfValidatedFile $file) { $this->file = $this->getAvailableFileName(dmString::slugify(dmOs::getFileWithoutExtension($file->getOriginalName())) . dmOs::getFileExtension($file->getOriginalName(), true)); $this->clearCache(); $file->save($this->getFullPath()); $this->refreshFromFile(); return $this; }
/** * Physically creates asset * * @param string $asset_path path to the asset original file * @param bool $move do move or just copy ? */ public function create(sfValidatedFile $file) { $this->file = $this->getAvailableFileName(dmOs::sanitizeFileName($file->getOriginalName())); $this->clearCache(); $file->save($this->getFullPath()); $this->refreshFromFile(); return $this; }
public function generateImageFilename(sfValidatedFile $file) { return 'rank_' . $this->id . $file->getExtension($file->getOriginalExtension()); }
public function generateImage3Filename(sfValidatedFile $file) { return "image3" . $file->getExtension(); }
$t->fail('->save() throws an Exception if you don\'t give a filename and the path is empty'); } catch (Exception $e) { $t->pass('->save() throws an Exception if you don\'t give a filename and the path is empty'); } try { $f->save($tmpDir . '/test.txt/test1.txt'); $t->fail('->save() throws an Exception if the directory already exists and is not a directory'); } catch (Exception $e) { $t->pass('->save() throws an Exception if the directory already exists and is not a directory'); } // ->getExtension() $t->diag('->getExtension()'); $f = new sfValidatedFile('test.txt', 'text/plain', $tmpDir . '/test.txt', strlen($content)); $t->is($f->getExtension(), '.txt', '->getExtension() returns file extension based on the content type'); $f = new sfValidatedFile('test.txt', 'image/x-png', $tmpDir . '/test.txt', strlen($content)); $t->is($f->getExtension(), '.png', '->getExtension() returns file extension based on the content type'); $f = new sfValidatedFile('test.txt', 'very/specific', $tmpDir . '/test.txt', strlen($content)); $t->is($f->getExtension(), '', '->getExtension() returns an empty string if it does not know the content type'); $f = new sfValidatedFile('test.txt', '', $tmpDir . '/test.txt', strlen($content)); $t->is($f->getExtension(), '', '->getExtension() returns an empty string if the content type is empty'); $t->is($f->getExtension('bin'), 'bin', '->getExtension() takes a default extension as its first argument'); // ->getOriginalExtension() $t->diag('->getOriginalExtension()'); $f = new sfValidatedFile('test.txt', 'text/plain', $tmpDir . '/test.txt', strlen($content)); $t->is($f->getOriginalExtension(), '.txt', '->getOriginalExtension() returns the extension based on the uploaded file name'); $f = new sfValidatedFile('test', 'text/plain', $tmpDir . '/test.txt', strlen($content)); $t->is($f->getOriginalExtension(), '', '->getOriginalExtension() returns an empty extension if the uploaded file name has no extension'); $t->is($f->getOriginalExtension('bin'), 'bin', '->getOriginalExtension() takes a default extension as its first argument'); unlink($tmpDir . '/test.txt'); sfToolkit::clearDirectory($tmpDir . '/foo'); rmdir($tmpDir . '/foo');
/** * * @param sfValidatedFile $file * @return <type> */ protected function isValidResume($file) { $validFile = false; $mimeTypes = array_values($this->allowedFileTypes); $originalName = $file->getOriginalName(); if ($file instanceof orangehrmValidatedFile && $originalName != "") { $fileType = $file->getType(); if (!empty($fileType) && in_array($fileType, $mimeTypes)) { $validFile = true; } else { $fileType = $this->guessTypeFromFileExtension($originalName); if (!empty($fileType)) { $file->setType($fileType); $validFile = true; } } } return $validFile; }
/** * Saves the uploaded file. * * This method can throw exceptions if there is a problem when saving the file. * * If you don't pass a file name, it will be generated by the generateFilename method. * This will only work if you have passed a path when initializing this instance. * * @param string $file The file path to save the file * @param int $fileMode The octal mode to use for the new file * @param bool $create Indicates that we should make the directory before moving the file * @param int $dirMode The octal mode to use when creating the directory * * @return string The filename without the $this->path prefix * * @throws Exception */ public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777) { if (is_null($file)) { $file = $this->getOriginalName(); $parts = explode(".", $file); $ext = $parts[count($parts) - 1]; $file = preg_replace("/\\.{$ext}/i", sprintf("%04d", "-" . rand(0, 999)) . ".{$ext}", $file); } if ($file[0] != '/' && $file[0] != '\\' && !(strlen($file) > 3 && ctype_alpha($file[0]) && $file[1] == ':' && ($file[2] == '\\' || $file[2] == '/'))) { if (is_null($this->path)) { throw new RuntimeException('You must give a "path" when you give a relative file name.'); } $file = $this->path . DIRECTORY_SEPARATOR . $file; } // get our directory path from the destination filename $directory = dirname($file); if (!is_readable($directory)) { if ($create && !mkdir($directory, $dirMode, true)) { // failed to create the directory throw new Exception(sprintf('Failed to create file upload directory "%s".', $directory)); } // chmod the directory since it doesn't seem to work on recursive paths chmod($directory, $dirMode); } if (!is_dir($directory)) { // the directory path exists but it's not a directory throw new Exception(sprintf('File upload path "%s" exists, but is not a directory.', $directory)); } if (!is_writable($directory)) { // the directory isn't writable throw new Exception(sprintf('File upload path "%s" is not writable.', $directory)); } $this->savedName = $file; //return is_null($this->path) ? $file : str_replace($this->path.DIRECTORY_SEPARATOR, '', $file); $ret = parent::save($file, $fileMode, $create, $dirMode); //$img = new sfImage( $file ); if ($ret) { $s3 = new S3Voota(); if (strpos($file, "politicos")) { $s3->createPoliticoFromFile($file); } else { if (strpos($file, "instituciones")) { $s3->createInstitucionFromFile($file); } else { if (strpos($file, "usuarios")) { $ret = $s3->createUsuarioFromFile($file); } else { if (strpos($file, "partidos")) { $s3->createPartidoFromFile($file); } else { if (strpos($file, "propuestas")) { $s3->createFromFile("propuestas", $file); } else { if (strpos($file, "docs")) { $s3->createDocFromFile("docs", $file); } else { if (strpos($file, "elecciones")) { $s3->createFromFile("elecciones", $file); } } } } } } } } return $ret; }