Sha1_file函数对我不起作用

时间:2015-02-13 10:27:02

标签: php database sha1

function addFlyer($db){
if (isset($_FILES['file_array'])) {
    $name_array = $_FILES['file_array']['name'];
    $tmp_name_array = $_FILES['file_array']['tmp_name'];
    $type_array = $_FILES['file_array']['type'];
    $size_array = $_FILES['file_array']['size'];
    $error_array = $_FILES['file_array']['error'];

    for ($i = 0; $i < count($tmp_name_array); $i++) {
        if (file_exists('images/' . sha1_file($name_array[$i]))) {
            // File exists already in the folder.
            $uploadOk = 0;
            header("Location: admin.php?action=errorupload");   
        }

        else {
            if (move_uploaded_file($tmp_name_array[$i], "images/" . sha1_file($name_array[$i]))) {
                //upload complete
                $uploadOk = 1;
            } else {
                //upload failed.
                header("Location: admin.php?action=errorupload");
            }
        }
}

    if($uploadOk == 1 && isset($_POST['month'], $_POST['title'], $_POST['content'], $_POST['content1'], $_POST['content2'])) {
        $query = $db->prepare("INSERT INTO version (month, title, content, content1, content2, imgurls, imgurlm, imgurll, imgurlfb) VALUES(:month, :title, :content, :content1, :content2, :imgurls, :imgurlm, :imgurll, :imgurlfb)");
        $query->bindParam(':month', $_POST['month'], PDO::PARAM_STR);
        $query->bindParam(':title', $_POST['title'], PDO::PARAM_STR);
        $query->bindParam(':content', $_POST['content'], PDO::PARAM_STR);
        $query->bindParam(':content1', $_POST['content1'], PDO::PARAM_STR);
        $query->bindParam(':content2', $_POST['content2'], PDO::PARAM_STR);
        $query->bindParam(':imgurls', sha1_file($_FILES['file_array']['name'][0]), PDO::PARAM_STR);
        $query->bindParam(':imgurlm', sha1_file($_FILES['file_array']['name'][1]), PDO::PARAM_STR);
        $query->bindParam(':imgurll', sha1_file($_FILES['file_array']['name'][2]), PDO::PARAM_STR);
        $query->bindParam(':imgurlfb', sha1_file($_FILES['file_array']['name'][3]), PDO::PARAM_STR);

        $query->execute();
        header("Location: admin.php");
    }

} }

目前我得到了这段代码,但它没有散列文件,也不会把它放在数据库中。现在出了什么问题?它只是转到我做的错误页面,有人知道解决方案吗?

1 个答案:

答案 0 :(得分:1)

当然它不起作用。

$name_array = $_FILES['file_array']['name'];
...
if (file_exists('images/' . sha1_file($name_array[$i]))) {

您可以从有关file uploads的PHP文档中了解到 $_FILES['...']['name']是:

  

客户端计算机上的文件的原始名称

您将该名称传递给sha1_file(),并希望它在您的服务器上找到该文件并进行处理。

如果您只想存储每个文件一次并通过使用其内容的SHA1命名该文件来执行此操作,那么您应该在$_FILES['file_array']['tmp_name']中SHA1服务器上等待处理的内容。或$tmp_name_array如果您喜欢这种方式。

你应该只为每个文件调用sha1_file()一次而不是两次,因为这是一项耗时的操作。

试试这个(我只更改了包含$local_file_name的行):

for ($i = 0; $i < count($tmp_name_array); $i++) {
    $local_file_name = sha1_file($tmp_name_array[$i]);
    if (file_exists('images/'.$local_file_name)) {
        // File exists already in the folder.
        $uploadOk = 0;
        header("Location: admin.php?action=errorupload");   
    } else {
        if (move_uploaded_file($tmp_name_array[$i], 'images/'.$local_file_name)) {
            //upload complete
            $uploadOk = 1;
        } else {
            //upload failed.
            header("Location: admin.php?action=errorupload");
        }
    }
}