} public function getAbsolutePath() { return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->path; } public function getAssetPath() { if ($this->path == null) { return 'uploads/images/400x300.png'; } else { return 'uploads/images/' . $this->path;
public function process() { if ($this->options['delete_old'] && $this->old_file !== $this->getFileAbsolutePath()) { $this->removeFile($this->old_file); } if (null === $this->file) { return; } // echo "<pre>";\Doctrine\Common\Util\Debug::dump(realpath(__DIR__ . '/../../../../../web/media/uploads/g'), 5);die("</pre>"); $this->file->move($this->entity->getUploadDir(), $this->entity->getPath()); }
/** * Move File to a temporary storage directory for processing * temporary directory must have 0755 permissions in order to be processed * * @param Symfony\Component\HttpFoundation\File\UploadedFile $csv_import * @return Symfony\Component\HttpFoundation\File $moved_file */ private function moveFile($csv_import) { // Check if directory exists make sure it has correct permissions, if not make it $destination_directory = storage_path('imports/tmp'); if (is_dir($destination_directory)) { //chmod($destination_directory, 0755); } else { //mkdir($destination_directory, 0755, true); } // Get file's original name $original_file_name = $csv_import->getClientOriginalName(); // Return moved file as File object return $csv_import->move($destination_directory, $original_file_name); }
public function testImageUpload() { $client = $this->createClient(); $this->importDatabaseSchema(); $client->enableProfiler(); $crawler = $client->request('GET', '/'); $this->assertTrue($client->getResponse()->isSuccessful()); //Photos not uploaded yet $this->assertSame($crawler->filter('div.photo')->count(), 0); $client->enableProfiler(); $fileToUpload = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__ . '/../Fixtures/images/sonata-admin-iphpfile.jpeg', 'sonata-admin-iphpfile.jpeg'); $client->submit($crawler->selectButton('Upload')->form(), array('title' => 'Some title', 'photo' => $fileToUpload, 'date[year]' => '2013', 'date[month]' => '3', 'date[day]' => '15')); $crawler = $client->followRedirect(); //added 1 photo $this->assertSame($crawler->filter('div.photo')->count(), 1); $photos = $this->getEntityManager()->getRepository('TestBundle:Photo')->findAll(); $this->assertSame(sizeof($photos), 1); $photo = $photos[0]; $this->assertSame($photo->getTitle(), 'Some title'); //path to images dir and directory naming config in Tests/Functional/config/default.yml $this->assertSame($photo->getPhoto(), array('fileName' => '/2013/03/sonata-admin-iphpfile.jpeg', 'originalName' => 'sonata-admin-iphpfile.jpeg', 'mimeType' => 'application/octet-stream', 'size' => $fileToUpload->getSize(), 'path' => '/photo/2013/03/sonata-admin-iphpfile.jpeg', 'width' => 671, 'height' => 487)); }
public function upload() { // the file property can be empty if the field is not required if (null === $this->file) { return; } // set the path property to the filename where you will save the file $this->setUrl(uniqid() . '.' . $this->file->guessExtension()); // we use the original file name here but you should // sanitize it at least to avoid any security issues // move takes the target directory and then the target filename to move to $this->file->move($this->getUploadRootDir(), $this->getUrl()); // clean up the file property as you won't need it anymore $this->file = null; }
public static function upload($url, $gallery = NULL, $title = '') { $img_data = @file_get_contents($url); if (!$img_data) { return false; } $tmp_path = storage_path(md5($url)); try { file_put_contents($tmp_path, $img_data); } catch (Exception $e) { echo 'Error #' . $e->getCode() . ':' . $e->getMessage() . "<br/>\n"; echo 'In file: ' . $e->getFile() . ' (' . $e->getLine() . ')'; die; } $file = new \Symfony\Component\HttpFoundation\File\UploadedFile($tmp_path, basename($url)); ## Check upload & thumb dir $uploadPath = Config::get('site.galleries_photo_dir'); $thumbsPath = Config::get('site.galleries_thumb_dir'); if (!File::exists($uploadPath)) { File::makeDirectory($uploadPath, 0777, TRUE); } if (!File::exists($thumbsPath)) { File::makeDirectory($thumbsPath, 0777, TRUE); } ## Generate filename $fileName = time() . "_" . rand(1000, 1999) . '.' . $file->getClientOriginalExtension(); #echo $fileName; ## Get images resize parameters from config $thumb_size = Config::get('site.galleries_thumb_size'); $photo_size = Config::get('site.galleries_photo_size'); ## Get image width & height $image = ImageManipulation::make($file->getRealPath()); $w = $image->width(); $h = $image->height(); if ($thumb_size > 0) { ## Normal resize $thumb_resize_w = $thumb_size; $thumb_resize_h = $thumb_size; } else { ## Resize "by the smaller side" $thumb_size = abs($thumb_size); ## Resize thumb & full-size images "by the smaller side". ## Declared size will always be a minimum. $thumb_resize_w = $w > $h ? null : $thumb_size; $thumb_resize_h = $w > $h ? $thumb_size : null; } ## Resize thumb image $thumb_upload_success = ImageManipulation::make($file->getRealPath())->resize($thumb_resize_w, $thumb_resize_h, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save($thumbsPath . '/' . $fileName); if ($photo_size > 0) { ## Normal resize $image_resize_w = $photo_size; $image_resize_h = $photo_size; } else { ## Resize "by the smaller side" $photo_size = abs($photo_size); ## Resize full-size images "by the smaller side". ## Declared size will always be a minimum. $image_resize_w = $w > $h ? null : $photo_size; $image_resize_h = $w > $h ? $photo_size : null; } ## Resize full-size image $image_upload_success = ImageManipulation::make($file->getRealPath())->resize($image_resize_w, $image_resize_h, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save($uploadPath . '/' . $fileName); ## Delete original file unlink($file->getRealPath()); ## Gallery - none, existed, new $gallery_id = 0; if (is_int($gallery) && $gallery > 0) { $gallery_id = $gallery; } elseif (is_string($gallery)) { $gal = Gallery::create(['name' => $gallery]); $gallery_id = $gal->id; } ## Create MySQL record $photo = Photo::create(array('name' => $fileName, 'gallery_id' => $gallery_id, 'title' => $title)); return $photo; }
/** * Save given file * * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file * @param $path * * @return array * @throws Exception */ function save_file(Symfony\Component\HttpFoundation\File\UploadedFile $file, $path) { $fileName = sprintf("%d_%s", time(), str_replace(' ', '_', $file->getClientOriginalName())); if (File::exists("{$path}/{$fileName}")) { throw new Exception('File already exists!'); } if (!File::isDirectory($path)) { File::makeDirectory($path, 755, true); } if (!$file->move($path, $fileName)) { throw new Exception('Failed to save!'); } return ['filename' => $fileName, 'size' => $file->getSize()]; }
/** * Upload file to server * * @param Symfony\Component\HttpFoundation\File\UploadedFile $file * @param boolean $isImage * * @return string */ public static function uploadFile($file, $isImage = true) { $newFileName = self::createNewFileName($file->getClientOriginalName()); $finalFileName = $newFileName . '.' . $file->getClientOriginalExtension(); if ($isImage) { $uploadDir = self::getImageUploadPath(); $returnFileName = $newFileName; } else { $uploadDir = self::getFileUploadPath(); $returnFileName = $finalFileName; } // Upload file to server $uploadFile = $file->move($uploadDir, $finalFileName); if (empty($uploadFile)) { return ''; } if ($isImage) { // Create thumbnail image $thumbWidth = Config::get('app.image_sizes.size.thumb'); $thumbName = Config::get('app.image_sizes.name.thumb') . $finalFileName; copy($uploadDir . $finalFileName, $uploadDir . $thumbName); $thumbImage = ResizeImage::make($uploadDir . $thumbName); $thumbImage->orientate(); $thumbImage->fit($thumbWidth)->save($uploadDir . $thumbName); } return $returnFileName; }
/** * Valid an image * * @param Symfony\Component\HttpFoundation\File\UploadedFile $photo * @return boolean */ public function isValidImage($photo) { $mimesAllowed = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'); return in_array($photo->getMimeType(), $mimesAllowed); }
/** * VALIDATION RULE. * * Carga un documento XML de un archivo, devuelve FALSE en caso de error. * * @access public * @param string $attribute * @param Symfony\Component\HttpFoundation\File\UploadedFile $value * @param array $parameters * @return boolean * * @throws ErrorException */ public function fileisxml($attribute, $value, $parameters) { $carga = $this->xml()->isXML($value->getRealPath()); return $carga; }