Autor Zpráva
jenda69
Profil
Ahoj, snad to tu nebylo, nic jsem tu nenašel. Potřebuju zjistit CRC nebo jinej kontrolní součet u souboru na svým webu. Je velkej, má 700MB a potřeboval bych zkontrolovat, jestli se uploadl správně.

Na php.net jsem našel tohle. Myslím, že to by snad i mělo být ono, ale zas nejsem tak dobrej v PHP, abych věděl, kam zadat ten název souboru a prostě všechno, aby mi to CRC vypsalo.. Díky za odpověď.

I needed the crc32 of a file that was pretty large, so I didn't want to read it into memory.

So I made this:

<?php
$GLOBALS['__crc32_table']=array(); // Lookup table array
__crc32_init_table();

function __crc32_init_table() { // Builds lookup table array
// This is the official polynomial used by
// CRC-32 in PKZip, WinZip and Ethernet.
$polynomial = 0x04c11db7;

// 256 values representing ASCII character codes.
for($i=0;$i <= 0xFF;++$i) {
$GLOBALS['__crc32_table'][$i]=(__crc32_reflect($i,8) << 24);
for($j=0;$j < 8;++$j) {
$GLOBALS['__crc32_table'][$i]=(($GLOBALS['__crc32_table'][$i] << 1) ^
(($GLOBALS['__crc32_table'][$i] & (1 << 31))?$polynomial:0));
}
$GLOBALS['__crc32_table'][$i] = __crc32_reflect($GLOBALS['__crc32_table'][$i], 32);
}
}

function __crc32_reflect($ref, $ch) { // Reflects CRC bits in the lookup table
$value=0;

// Swap bit 0 for bit 7, bit 1 for bit 6, etc.
for($i=1;$i<($ch+1);++$i) {
if($ref & 1) $value |= (1 << ($ch-$i));
$ref = (($ref >> 1) & 0x7fffffff);
}
return $value;
}

function __crc32_string($text) { // Creates a CRC from a text string
// Once the lookup table has been filled in by the two functions above,
// this function creates all CRCs using only the lookup table.

// You need unsigned variables because negative values
// introduce high bits where zero bits are required.
// PHP doesn't have unsigned integers:
// I've solved this problem by doing a '&' after a '>>'.

// Start out with all bits set high.
$crc=0xffffffff;
$len=strlen($text);

// Perform the algorithm on each character in the string,
// using the lookup table values.
for($i=0;$i < $len;++$i) {
$crc=(($crc >> 8) & 0x00ffffff) ^ $GLOBALS['__crc32_table'][($crc & 0xFF) ^ ord($text{$i})];
}

// Exclusive OR the result with the beginning value.
return $crc ^ 0xffffffff;
}

function __crc32_file($name) { // Creates a CRC from a file
// Info: look at __crc32_string

// Start out with all bits set high.
$crc=0xffffffff;

if(($fp=fopen($name,'rb'))===false) return false;

// Perform the algorithm on each character in file
for(;;) {
$i=@fread($fp,1);
if(strlen($i)==0) break;
$crc=(($crc >> 8) & 0x00ffffff) ^ $GLOBALS['__crc32_table'][($crc & 0xFF) ^ ord($i)];
}

@fclose($fp);

// Exclusive OR the result with the beginning value.
return $crc ^ 0xffffffff;
}
?>


//nemusí to bejt CRC, ale i jinej kontrolní součet. Jen abych si ověřil, jeslti je nahráno v pohodě.. ale na hostingu je safemode, takže md5_file() nejde
Azu
Profil *
a co takhle si přečíst manuál k PHP, by si jinka zjistil, že je tam přímo funkce pro CRC, MD5 a další
jenda69
Profil
Zkoušel jsem tu k MD5, jak jsem už psal výše, ale nešlo to kvůli safe mode...
//edit: tak jo, nakonec se mi to povedlo přes sha1_file
Azu
Profil *
tak to je naprostá kravina, kvůli safe modu se rozhodně nemůže stát že by nešlo MD5, stejně jako CRC32
Toto téma je uzamčeno. Odpověď nelze zaslat.

0