2014-05-23 12:40 阅读 61


i am creating Hash values with following code, now what happens is that when i test the hash value on Windows local Xampp server i get hash value which is different for same code that runs on Linux.

  move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newname);
    "Stored in: " . "upload/" . $_FILES["file"]["name"];
    $image = "upload/" . $newname;
    $sign = md5(file_get_contents($image));

Now i dont know why is this happening. For the same code that i just pasted above.

EDIT: Opening question again. The solution i found worked only for Linux which means linux and windows now give me same hash but when an image is uploaded from Mac(IOS) it is still generating different Hash.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dsf323233323332 dsf323233323332 2014-05-23 13:28

    Ok i found answer to my question, I still dont know why there are two different hashes been generated for the same code in windows and Linux

    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newname); 
        "Stored in: " . "upload/" . $_FILES["file"]["name"];
        $image = "upload/" . $newname;
        $sign = md5(file_get_contents($image));//This is code block that i was implmenting before solution

    What i tried here was i replaced my above code with following code

     move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newname);
            "Stored in: " . "upload/" . $_FILES["file"]["name"];
            $image = "upload/" . $newname;
            $sign = md5_file($image);// Changed here

    From this i think Hash values may be same when generated by md5() but if this function accepts file as input then hash values are calculated differently, i dont know if this is a PHP side issue or really OS level issue but if i go on with using md5_file() for generating hash of file i dont get different hash.

    点赞 评论 复制链接分享
  • dongxikuo5171 dongxikuo5171 2014-06-04 07:53

    Windows and Linux has different line endings, and . So when the file is read, the the content of files is different.

    Try uploading Text file with no new line or a Binary file. Also check difference in bytes read. It should be equal to number of new lines in next file.

    点赞 评论 复制链接分享
  • dsfg3241 dsfg3241 2014-06-04 21:01

    Might look at the page for fopen() about how to avoid line endings problems but it basically comes down to using 'wb' and 'rb' when writing and reading the file. Here's the link to the fopen page.

    Does mean probably can't use file_get_content() as doesn't seem to have option to set read mode.

    点赞 评论 复制链接分享