public function moveUploadedFile(UploadedFile $file, $uploadBasePath, $relativePath, $fileName) { $originalName = $file->getFilename(); // use filemtime() to have a more determenistic way to determine the subpath, otherwise its hard to test. // $relativePath = date('Y-m', filemtime($file->getPath())); $targetFileName = $relativePath . DIRECTORY_SEPARATOR . $originalName; $targetFilePath = $uploadBasePath . DIRECTORY_SEPARATOR . $targetFileName; $ext = $file->getExtension(); $i = 1; while (file_exists($targetFilePath) && md5_file($file->getPath()) != md5_file($targetFilePath)) { if ($ext) { $prev = $i == 1 ? "" : $i; $targetFilePath = $targetFilePath . str_replace($prev . $ext, $i++ . $ext, $targetFilePath); } else { $targetFilePath = $targetFilePath . $i++; } } $targetDir = $uploadBasePath . DIRECTORY_SEPARATOR . $relativePath; if (!is_dir($targetDir)) { $ret = mkdir($targetDir, umask(), true); if (!$ret) { throw new \RuntimeException("Could not create target directory to move temporary file into."); } } //$file->move($targetDir, basename($targetFilePath)); //$file->move($targetDir, basename($fileName.'.'.$ext)); $file->move($targetDir, basename($fileName)); return str_replace($uploadBasePath . DIRECTORY_SEPARATOR, "", $targetFilePath); }
public function moveUploadedFile(UploadedFile $file, $uploadBasePath, $relativePath) { $originalName = $file->getFilename() . '.' . $file->guessExtension(); $targetFileName = $relativePath . DIRECTORY_SEPARATOR . $originalName; $targetFilePath = $uploadBasePath . DIRECTORY_SEPARATOR . $targetFileName; $ext = $file->getExtension(); $i = 1; while (file_exists($targetFilePath) && md5_file($file->getPath()) != md5_file($targetFilePath)) { if ($ext) { $prev = $i == 1 ? "" : $i; $targetFilePath = $targetFilePath . str_replace($prev . $ext, $i++ . $ext, $targetFilePath); } else { $targetFilePath = $targetFilePath . $i++; } } $targetDir = 'uploads'; if (!is_dir($targetDir)) { $ret = mkdir($targetDir, 777, true); if (!$ret) { throw new \RuntimeException("Could not create target directory to move temporary file into."); } } $file->move($targetDir, basename($targetFilePath)); return str_replace($uploadBasePath . DIRECTORY_SEPARATOR, "", $targetFilePath); }
/** * converts UploadedFile to $_FILES array * * @return array */ public function getFileArray() { $array = array('name' => $this->uploaded_file->getClientOriginalName(), 'type' => $this->uploaded_file->getClientMimeType(), 'tmp_name' => $this->uploaded_file->getPath() . $this->getOSDirectorySeparator() . $this->uploaded_file->getFilename(), 'error' => $this->uploaded_file->getError(), 'size' => $this->uploaded_file->getSize(), 'dimension' => array('width' => 0, 'height' => 0)); if (preg_match('/^image/', $array['type'])) { list($array['dimension']['width'], $array['dimension']['height']) = getimagesize($this->uploaded_file); } return $array; }
public function run(UploadedFile $file, MediaProvider $media) { $media->ext = $file->getClientOriginalExtension(); $media->path = $file->getPath(); // by default /tmp $media->file = $file->getClientOriginalName(); $media->meta = empty($media->meta) ? [] : $media->meta; $media->sizes = empty($media->sizes) ? [] : $media->sizes; $media->caption = $file->getClientOriginalName(); $media->alt = $file->getClientOriginalName(); }
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; }
/** * Helper function to decompress zip files. * @param UploadedFile $file * @param $targetDirectory * @throws Exception */ private function unzip(UploadedFile $file, $targetDirectory) { $filter = new \Zend_Filter_Decompress(array('adapter' => $file->getClientOriginalExtension(), 'options' => array('target' => $targetDirectory))); $filter->filter($file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename()); }
/** * {@inheritdoc} */ public function path() { return $this->source->getPath() . '/' . $this->source->getFilename(); }
/** * @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; }