如何检查具有不同文件名的2个图像是否与PHP相同

时间:2017-12-02 12:14:08

标签: php image

我从WEB获取图像并将它们发送到AWS上的S3存储桶。每个图像都有一个随机名称。但我不想存储同卵双胞胎。因此,在将图像存储在我的服务器上(然后将其发送到S3)之前,我需要检查是否已存在相同的图像(在S3上)。

我无法对我的服务器进行检查,因为成功发送到S3后文件将被删除。

这是我的代码:

$filenameIn  = $url_img;
$nome = randomString();
$nome_img = "$nome.png";
$filenameOut = __DIR__ . '/img/' . $nome_img;
$contentOrFalseOnFailure = file_get_contents($filenameIn);
$bucket = 'bucket-img';

if( !$contentOrFalseOnFailure ) {
return "error1: ...";
}

$byteCountOrFalseOnFailure = file_put_contents($filenameOut, $contentOrFalseOnFailure);

if( !$byteCountOrFalseOnFailure ) {
return "error2: ...";
}

// Instantiate the S3 client with your AWS credentials
$aws = new Aws\S3\S3Client([
  'version' => 'latest',
  'region'  => 'sa-east-1',
  'credentials' => array(
      'key'    => 'omissis',
      'secret' => 'omissis',
  )
 ]);

 /*
  I BELIEVE AT THIS POINT I SHOULD MAKE A COMPARISON TO CHECK IF THE FILE THAT 
  IS ABOUT TO BE SENT TO S3 HAS AN IDENTICAL TWIN THERE, REGARDLESS OF THEIR 
  RANDOM NAME. IF SO, UPLOAD SHOULD BE ABORTED AND THE SCRIPT SHOULD PROVIDE 
  ME WITH THE URL OF THE TWIN. 
 */
 try{
 // Send a PutObject request and get the result object.
$result = $aws->putObject([
    'Bucket' => $bucket,
    'Key'        => $nome_img,
    'SourceFile' => $filenameOut,
    'ACL' => 'public-read'
]);

$r = $aws->getObjectUrl($bucket, $nome_img);
return $r;
} catch (S3Exception $e) {
  return $e->getMessage() . "\n";
}

正如我在代码中所说,我应该得到双文件的URL,如果存在的话。

这可能吗?

0 个答案:

没有答案