Corey
Corey

Reputation: 781

How to get SHA of file after upload in PHP

I have a site which copies a file from a URL to my sever. I need a way of getting a SHA of the file after it has been copied.

I was using @copy($url,$upload_path) to copy the file but this returns a boolean I need something that returns the file. Does anything like that exist?

I need to get the file afterwards for sha1_file($file)

Thank!

Upvotes: 0

Views: 463

Answers (2)

Adam Wagner
Adam Wagner

Reputation: 16107

sha1_file takes a filename. The $upload_path you've supplied to copy is a filename. You should be able to do:

sha1_file($upload_path)

to get your sha1.

Upvotes: 1

DaveRandom
DaveRandom

Reputation: 88647

You can just

if (@copy($url,$upload_path)) {
  $hash = sha1_file($upload_path);
}

$upload_path already contains the value you would need to pass to sha1_file().

And, as a general rule, the @ operator is evil. I will admit that this particular usage of it is arguably valid, but as a rule of thumb it should treated as a last resort.

Upvotes: 4

Related Questions