php對文件進行hash運算的方法_PHP教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:php計算給定時間之前的函數用法實例
      這里給定一個時間,計算這個時間在多久前,比如:2天前,1年前

      這篇文章主要介紹了php對文件進行hash運算的方法,涉及針對文件的hash運算技巧,非常具有實用價值,需要的朋友可以參考下

      本文實例講述了php對文件進行hash運算的方法。分享給大家供大家參考。具體如下:

      這段代碼非常有用,如果你下載了一個文件,網站提供了hash結果,你可以對你下載下來的文件進行hash運算,以驗證下載的文件是否正確。

      1. <html> 
      2. <head> 
      3.   <title>Hash (Check) Files</title> 
      4.   <style type='text/css'
      5.    #ok{color:green;} 
      6.    #nono{color:red;} 
      7.   </style> 
      8. </head> 
      9. <body> 
      10.  <?php 
      11.    if(!emptyempty($_FILES)){ 
      12.    if ($_FILES["file"]["error"] > 0){ 
      13.     switch($_FILES["file"]["error"]){ 
      14.      case 1: 
      15.      echo "<b id='nono'>Error: The uploaded file exceeds the upload_max_filesize directive in php.ini</b><br>"
      16.      break
      17.      case 2: 
      18.      echo "<b id='nono'>Error: The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.</b><br>"
      19.      break
      20.      case 3: 
      21.      echo "<b id='nono'>Error: The uploaded file was only partially uploaded.</b><br>"
      22.      break
      23.      case 4: 
      24.      echo "<b id='nono'>Error: No file was uploaded.</b><br>"
      25.      break
      26.      case 6: 
      27.      echo "<b id='nono'>Error: Missing a temporary folder.</b><br>"
      28.      break
      29.      case 7: 
      30.      echo "<b id='nono'>Error: Failed to write file to disk.</b><br>"
      31.      break
      32.      case 8: 
      33.      echo "<b id='nono'>Error: A PHP extension stopped the file upload.</b><br>"
      34.      break
      35.      default
      36.      echo "<b id='nono'>Unknown error occured.</b><br>"
      37.     } 
      38.    } else { 
      39.     echo 'Upload: ' . $_FILES['file']['name'] . '<br>'
      40.     echo 'Type: ' . $_FILES['file']['type'] . '<br>'
      41.     echo 'Size: ' . (round($_FILES['file']['size'] / 1024, 2)) . ' Kb<br><br>'
      42.     if(array_search($_POST['algo'], hash_algos())===false){ 
      43.     echo 'Unknown hashing algorithm requested.<br>'
      44.     } else { 
      45.     echo 'Hashing Algorithm: '$_POST['algo'] . '<br>'
      46.     $hash = hash_file($_POST['algo'], $_FILES['file']['tmp_name']); 
      47.     echo 'Calculated hash: ' . $hash . '<br>'
      48.     if($_POST['exphash']!=='none' && !emptyempty($_POST['exphash'])){ 
      49.       echo 'Expected hash:   ' . $_POST['exphash'] . '<br><br>'
      50.       echo ($hash==$_POST['exphash'])? '<b id="ok">Hash matched expected value.</b>' : '<b id="nono">Hash did not match expected value.</b>'
      51.       echo '<br>'
      52.     } 
      53.     } 
      54.    } 
      55.    ?> 
      56.    <br> 
      57.    <button onClick="document.location.reload(true)">Again</button> 
      58.     <?php 
      59.    } else { 
      60.   ?> 
      61.   <form action="" method="post" enctype="multipart/form-data"
      62.    <input type="hidden" name="exphash" value="none"
      63.    <label for="file">Filename:</label> 
      64.    <input type="file" name="file" id="file"
      65.    <input type="submit" name="submit" value="Submit" /><br> 
      66.    <label>Expected hash(optional): <input type="text" name="exphash" size="100"></label> 
      67.    <br><br>Choose an algorithm (This is the list of all the available algorithms in your php installation)<br> 
      68.    <?php 
      69.    foreach(hash_algos() as $algo){ 
      70.     if($algo=='md5'){ 
      71.     echo "<label><input type='radio' name='algo' value='$algo' checked='checked'>$algo</label><br>"
      72.     } else { 
      73.     echo "<label><input type='radio' name='algo' value='$algo'>$algo</label><br>"
      74.     } 
      75.    } 
      76.    ?> 
      77.   </form> 
      78.  <?php 
      79.    } 
      80.   ?> 
      81. </body> 
      82. </html> 

      分享:php實現的mongodb操作類實例
      本文實例講述了php實現的mongodb操作類。分享給大家供大家參考。具體如下:

      來源:模板無憂//所屬分類:PHP教程/更新時間:2015-04-03
      相關PHP教程