public function storeFile(UploadedFile $file) { $hashedName = sha1($file->getBasename() . filemtime($file->getPath())); $extension = strtolower($file->getClientOriginalExtension()); $finalName = $hashedName . '.' . $extension; while (file_exists($this->targetDirectory . DIRECTORY_SEPARATOR . $this->hashToLocation($finalName))) { $hashedName .= '1'; $finalName = $hashedName . '.' . $extension; } $hashed = $this->hashToLocation($finalName); $directory = dirname($this->targetDirectory . DIRECTORY_SEPARATOR . $hashed); if (!is_dir($directory)) { $ret = mkdir($directory, 0777, true); if (!$ret) { throw new DiskAssetException('Cannot create a directory for uploading the files!'); } } $file->move($directory, $hashed); return $finalName; }
/** * Returns an array of data for upload model * * @param UploadedFile $uploadedFile * @return array */ protected function prepareUploadData(UploadedFile $uploadedFile) { return ['extension' => $uploadedFile->getExtension(), 'mimetype' => $uploadedFile->getMimeType(), 'size' => $uploadedFile->getSize(), 'name' => $uploadedFile->getBasename('.' . $uploadedFile->getExtension())]; }
/** * Extract the file information from the uploaded file, into the internal properties */ private function setFileInfo() { if ($this->file === null) { return; } if ($this->file instanceof \Symfony\Component\HttpFoundation\File\UploadedFile) { //load file information $fileInfo = pathinfo($this->file->getClientOriginalName()); $extension = $fileInfo['extension']; $name = $fileInfo['filename']; } else { $extension = $this->file->guessExtension(); $name = $this->file->getBasename(); } //set the file type using the type mapping if (array_key_exists(strtolower($extension), $this->typeMapping)) { $this->type = $this->typeMapping[strtolower($extension)]; } else { $this->type = self::TYPE_UNKNOWN; } // The filesize in bytes. $this->fileSize = $this->file->getSize(); $this->name = $this->removeSpecialCharacters($name); $this->extension = $extension; $this->path = str_replace(Shopware()->OldPath(), '', $this->getUploadDir() . $this->getFileName()); if (DIRECTORY_SEPARATOR !== '/') { $this->path = str_replace(DIRECTORY_SEPARATOR, '/', $this->path); } }
public function upload(UploadedFile $file) { $fileName = $file->getBasename('.' . $file->getExtension()) . '.' . $file->guessExtension(); $file->move($this->targetDir, $fileName); return $fileName; }
/** * @param UploadedFile $fileObject * @return array */ private function getExifReadData(UploadedFile $fileObject) { Log::debug($fileObject->getPath()); Log::debug($fileObject->getFilename()); Log::debug($fileObject->getBasename()); Log::debug($fileObject->getClientMimeType()); Log::debug($fileObject->getExtension()); Log::debug($fileObject->getType()); $result = []; $mimeType = $fileObject->getClientMimeType(); // upload_igm and thumbnail directory make $dirName = date('Ym'); $uploadDir = public_path() . '/upload_img/' . $dirName; $thumbnailDir = public_path() . '/thumbnail/' . $dirName; $this->chkDir($uploadDir); $this->chkDir($thumbnailDir); //@todo オリジナル動画をリサイズする $extension = explode('/', $fileObject->getClientMimeType())[1]; $newFileName = sha1($fileObject->getFilename() . time()) . '.' . $extension; $saveFileName = $uploadDir . '/' . $newFileName; $fileObject->move($uploadDir, $newFileName); $thumbnail = Image::make(file_get_contents($saveFileName)); $thumbnail->resize(100, null, function ($constraint) { $constraint->aspectRatio(); }); $thumbnail->save($thumbnailDir . '/' . $newFileName, 100); $result['file_name'] = $dirName . '/' . $newFileName; if ($mimeType !== 'image/jpeg') { return $result; } $exif = exif_read_data($saveFileName); if ($exif === false) { return false; } $result['file_date_time'] = isset($exif['DateTimeOriginal']) ? $exif['DateTimeOriginal'] : null; $result['file_size'] = $exif['FileSize']; $result['mime_type'] = $exif['MimeType']; $result['width'] = $exif['COMPUTED']['Width']; $result['height'] = $exif['COMPUTED']['Height']; $result['make'] = isset($exif['Make']) ? $exif['Make'] : null; $result['model'] = isset($exif['Model']) ? $exif['Model'] : null; $result['orientation'] = isset($exif['Orientation']) ? $exif['Orientation'] : null; if (isset($exif["GPSLatitude"])) { //緯度を60進数から10進数に変換 $lat = $this->convert_float($exif["GPSLatitude"][0]) + $this->convert_float($exif["GPSLatitude"][1]) / 60 + $this->convert_float($exif["GPSLatitude"][2]) / 3600; //南緯の場合はマイナスにする if ($exif["GPSLatitudeRef"] == "S") { $lat *= -1; } //経度を60進数から10進数に変換 $lng = $this->convert_float($exif["GPSLongitude"][0]) + $this->convert_float($exif["GPSLongitude"][1]) / 60 + $this->convert_float($exif["GPSLongitude"][2]) / 3600; //西経の場合はマイナスにする if ($exif["GPSLongitudeRef"] == "W") { $lng *= -1; } $result['lat'] = $lat; $result['lng'] = $lng; } return $result; }
/** * @param string $key multi-part form item key name * @param string $filepath path to file * @param string|null $mimetype file mimetype, if none provided, will be guess from filepath * @param string|null $clientName optionnal client filename, if none provided, basename of filepath will be used */ public function attachFile($key, $filepath, $mimetype = null, $clientName = null) { $clientName = $clientName ? $clientName : basename($filepath); $file = new File($filepath, true); $file = new UploadedFile($file->getRealPath(), $clientName, $file->getBasename(), $file->getMimeType(), $file->getSize(), 0); $this->getRequest()->files->set($key, $file); }