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; }
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; }
$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'); $t->is($filename, 'foo/test1.txt', '->save() returns the saved filename relative to the path given'); $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->getSavedName(), $tmpDir . '/foo/test1.txt', '->getSavedName() returns the saved file name'); $filename = $f->save('foo/test1.txt'); $t->is($filename, 'foo/test1.txt', '->save() returns the saved filename relative to the path given'); $t->is(file_get_contents($tmpDir . '/foo/test1.txt'), file_get_contents($tmpDir . '/test.txt'), '->save() saves the file to the given path and uses the path if the file is not absolute'); $t->is($f->getSavedName(), $tmpDir . '/foo/test1.txt', '->getSavedName() returns the saved file name'); $filename = $f->save(); $t->is(file_get_contents($tmpDir . '/' . $filename), file_get_contents($tmpDir . '/test.txt'), '->save() returns the generated file name is none was given'); $t->is($f->getSavedName(), $tmpDir . '/' . $filename, '->getSavedName() returns the saved file name'); try { $f = new sfValidatedFile('test.txt', 'text/plain', $tmpDir . '/test.txt', strlen($content)); $f->save(); $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));
/** * 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; }
/** * 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; }