getFileHash() public static method

Generate a md5 hash of a file. This is eqauls to md5sum command
public static getFileHash ( string $sourceFile ) : false | string
$sourceFile string The path to the file
return false | string Returns false or the md5 hash of this file
示例#1
0
 /**
  * @todo its a copy from the old colde, refactor code
  * @param string $fileSource
  * @param string $fileName
  * @param int $folderId
  */
 public function addFile($fileSource, $fileName, $folderId = 0, $isHidden = false)
 {
     if (empty($fileSource) || empty($fileName)) {
         throw new Exception("Unable to create file where file source and/or file name is empty.");
     }
     $fileInfo = FileHelper::getFileInfo($fileName);
     $baseName = Inflector::slug($fileInfo->name, '-');
     $fileHashName = Storage::createFileHash($fileName);
     $fileHash = FileHelper::getFileHash($fileSource);
     $mimeType = FileHelper::getMimeType($fileSource);
     $newName = implode([$baseName . '_' . $fileHashName, $fileInfo->extension], '.');
     $savePath = $this->serverPath . '/' . $newName;
     if (is_uploaded_file($fileSource)) {
         if (!@move_uploaded_file($fileSource, $savePath)) {
             throw new Exception("error while moving uploaded file from {$fileSource} to {$savePath}");
         }
     } else {
         if (!@copy($fileSource, $savePath)) {
             throw new Exception("error while copy file from {$fileSource} to {$savePath}.");
         }
     }
     $model = new StorageFile();
     $model->setAttributes(['name_original' => $fileName, 'name_new' => $baseName, 'name_new_compound' => $newName, 'mime_type' => $mimeType, 'extension' => strtolower($fileInfo->extension), 'folder_id' => (int) $folderId, 'hash_file' => $fileHash, 'hash_name' => $fileHashName, 'is_hidden' => $isHidden ? 1 : 0, 'file_size' => @filesize($savePath)]);
     if ($model->validate()) {
         if ($model->save()) {
             $this->deleteHasCache($this->fileCacheKey);
             $this->_filesArray[$model->id] = $model->toArray();
             return $this->getFile($model->id);
         }
     }
     return false;
 }
示例#2
0
 public function testGetHashFile()
 {
     $this->assertSame(false, FileHelper::getFileHash('notexists.jpg'));
     $this->assertSame('7dff5cc5a1d8f04004b4a0075d3eeeae', FileHelper::getFileHash(__DIR__ . '/../../data/hashfile.txt'));
 }