예제 #1
0
 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();
 }
예제 #2
0
 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);
 }
예제 #3
0
 /**
  * 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;
     }
 }
예제 #4
0
 /**
  *
  * @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;
 }
예제 #5
0
    $t->skip();
} catch (sfValidatorError $e) {
    $t->pass('->clean() throws an sfValidatorError if the file is required and no file is uploaded');
    $t->is($e->getCode(), 'required', '->clean() throws an error code of required');
}
$v = new testValidatorFile(array('required' => false));
$t->is($v->clean(array('tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'name' => '', 'size' => 0, 'type' => '')), null, '->clean() handles the required option correctly');
// sfValidatedFile
// ->getOriginalName() ->getTempName() ->getSize() ->getType()
$t->diag('->getOriginalName() ->getTempName() ->getSize() ->getType()');
sfToolkit::clearDirectory($tmpDir . '/foo');
if (is_dir($tmpDir . '/foo')) {
    rmdir($tmpDir . '/foo');
}
$f = new sfValidatedFile('test.txt', 'text/plain', $tmpDir . '/test.txt', strlen($content));
$t->is($f->getOriginalName(), 'test.txt', '->getOriginalName() returns the original name');
$t->is($f->getTempName(), $tmpDir . '/test.txt', '->getTempName() returns the temp name');
$t->is($f->getType(), 'text/plain', '->getType() returns the content type');
$t->is($f->getSize(), strlen($content), '->getSize() returns the size of the uploaded file');
// ->save() ->isSaved() ->getSavedName()
$t->diag('->save() ->isSaved() ->getSavedName()');
$f = new sfValidatedFile('test.txt', 'text/plain', $tmpDir . '/test.txt', strlen($content));
$t->is($f->isSaved(), false, '->isSaved() returns false if the file has not been saved');
$t->is($f->getSavedName(), null, '->getSavedName() returns null if the file has not been saved');
$filename = $f->save($tmpDir . '/foo/test1.txt');
$t->is($filename, $tmpDir . '/foo/test1.txt', '->save() returns the saved filename');
$t->is(file_get_contents($tmpDir . '/foo/test1.txt'), file_get_contents($tmpDir . '/test.txt'), '->save() saves the file to the given path');
$t->is($f->isSaved(), true, '->isSaved() returns true if the file has been saved');
$t->is($f->getSavedName(), $tmpDir . '/foo/test1.txt', '->getSavedName() returns the saved file name');
$f = new sfValidatedFile('test.txt', 'text/plain', $tmpDir . '/test.txt', strlen($content), $tmpDir);
$filename = $f->save($tmpDir . '/foo/test1.txt');
 /**
  * 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;
 }
예제 #8
0
 /**
  * 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;
 }
예제 #9
0
 /**
  * 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;
 }