将字符串的一部分转换为哈希值

时间:2014-01-10 16:02:33

标签: php

有没有办法散列字符串的一部分。例如,我有一个名为“abc.jpg”的文件,我可以在“.jpg”哈希之前加上字母吗? 第一个文件是upload_form.php

<form enctype="multipart/form-data" action="upload_file.php" method="POST">
   <input type="hidden" name="MAX_FILE_SIZE" value="500"/>
   <input type="file" name="pix" size="60"/><br/>
   <input type="submit" name="submit" value="Upload"/>
</form>

第二个文件是upload_file.php

<?php
 if(!isset($_POST['submit']))
 {
   include("form_upload.php");
 }
 else
 {
  //if file is too large too upload
   if($_FILES['pix']['tmp_name']=="")
    {
     echo "<p>file is too large too upload, Please make sure the file you want to upload is smaller than 5MB</p>";
     include("form_upload.inc");
     exit();
    }
    //if the file is not jpeg format
   if(!preg_match("/^image\/jpeg$/",$_FILES['pix']['type']))
    {
     echo "<p>only jpg file is accepted !</p>";
     include("form_upload.inc");
     exit();
    }
    // if everything is ok
    else
    {
     $destination='C:\xampp\htdocs\test\my_application\picture\ '.$_FILES['pix']['name'];
     $tmp_file=$_FILES['pix']['tmp_name'];
     move_uploaded_file($tmp_file,$destination);
     echo "<p>picture was upload successfully</p>";
     echo "<p>Name :{$_FILES['pix']['name']}</p>";
     echo "<p>Size {$_FILES['pix']['size']}</p>";
     echo "<p>Type {$_FILES['pix']['type']}</p>";
     echo "<img src='picture/ {$_FILES['pix']['name']}' alt='picture'/>";
    }
 }
?>

我想将图片名称转换为md5值,但只将名称转换为“.jpg”。有没有办法做到这一点

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

$hash = md5(basename($_FILES['pix']['name'], '.jpg')) . '.jpg';

如果您知道扩展名为.jpg

你也可以通过在点上爆炸并获取生成的数组的最后一个元素来获得扩展(只是一个例子,你也可以使用正则表达式或其他):

$ext = '.' . end(explode('.', $_FILES['pix']['name']));
$hash = md5(basename($_FILES['pix']['name'], $ext)) . $ext;

答案 1 :(得分:0)

thankyou我发现在散列值之后添加字符串“.jpg”的另一种方式。