Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : String Functions : sha1_file

sha1_file

Calculate the sha1 hash of a file (PHP 4 >= 4.3.0, PHP 5, PECL hash:1.1-1.3)
string sha1_file ( string filename [, bool raw_output] )


Code Examples / Notes » sha1_file

gubatron

P2P programs like LimeWire utilize sha1 to identify files. However they do it in base32. Here's an utility class if you want to write a Gnutella client in PHP5
/**
Utility base32 SHA1 class for PHP5
Copyright (C) 2006  Karl Magdsick (original author for Python)
                   Angel Leon (ported to PHP5)
                   Lime Wire LLC
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
class SHA1 {
 static $BASE32_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
 /** Given a file it creates a magnetmix */
 static function fileSHA1($file) {
   $raw = sha1_file($file,true);
   return SHA1::base32encode($raw);
 } //fileSHA1
 /** Takes raw input and converts it to base32 */
 static function base32encode($input) {
   $output = '';
   $position = 0;
   $storedData = 0;
   $storedBitCount = 0;
   $index = 0;
   while ($index < strlen($input)) {
     $storedData <<= 8;
     $storedData += ord($input[$index]);
     $storedBitCount += 8;
     $index += 1;
     //take as much data as possible out of storedData
     while ($storedBitCount >= 5) {
       $storedBitCount -= 5;
       $output .= SHA1::$BASE32_ALPHABET[$storedData >> $storedBitCount];
       $storedData &= ((1 << $storedBitCount) - 1);
     }
   } //while
   //deal with leftover data
   if ($storedBitCount > 0) {
     $storedData <<= (5-$storedBitCount);
     $output .= SHA1::$BASE32_ALPHABET[$storedData];
   }
   return $output;
 } //base32encode
}


schiros

If you've got a script that allows user file upload, and you want to prevent multiple uploads of the same file:
<?
session_start();
$isDuplicate = false;
if(isset($_FILES["filename"]["tmp_name"]) && file_exists($_FILES["filename"]["tmp_name"])) {
$fileHash = sha1_file($_FILES["filename"]["tmp_name"]);
if(!isset($_SESSION["check_filelist"])) {
$_SESSION["check_filelist"] = array($fileHash);
}
elseif(in_array($fileHash,$_SESSION["check_filelist"])) {
$isDuplicate = true;
}
else {
$_SESSION["check_filelist"][] = $fileHash;
}

if($isDuplicate) {
echo "You've already uploaded that file";
}
else{
// do some stuff
}
}

?>


admin

checking the sha1 of the file. ideal for download scripts making sure the file the user is downloading is not currupt
by checking the sha1 key
<?php
$filename = './Path/To/Your/File.zip';
if (file_exists($filename)) {
echo"i see the file";
} else {
echo "recheck the link file maybe broken";
}
$file = sha1_file('./Path/To/Your/File.zip');
echo"
is file hash valid?";
echo"hash = add the hash here
";
echo"hash of file :";
print($file);
?>


Change Language


Follow Navioo On Twitter
addcslashes
addslashes
bin2hex
chop
chr
chunk_split
convert_cyr_string
convert_uudecode
convert_uuencode
count_chars
crc32
crypt
echo
explode
fprintf
get_html_translation_table
hebrev
hebrevc
html_entity_decode
htmlentities
htmlspecialchars_decode
htmlspecialchars
implode
join
levenshtein
localeconv
ltrim
md5_file
md5
metaphone
money_format
nl_langinfo
nl2br
number_format
ord
parse_str
print
printf
quoted_printable_decode
quotemeta
rtrim
setlocale
sha1_file
sha1
similar_text
soundex
sprintf
sscanf
str_getcsv
str_ireplace
str_pad
str_repeat
str_replace
str_rot13
str_shuffle
str_split
str_word_count
strcasecmp
strchr
strcmp
strcoll
strcspn
strip_tags
stripcslashes
stripos
stripslashes
stristr
strlen
strnatcasecmp
strnatcmp
strncasecmp
strncmp
strpbrk
strpos
strrchr
strrev
strripos
strrpos
strspn
strstr
strtok
strtolower
strtoupper
strtr
substr_compare
substr_count
substr_replace
substr
trim
ucfirst
ucwords
vfprintf
vprintf
vsprintf
wordwrap
eXTReMe Tracker