PHP中严格的标准错误

时间:2013-08-25 15:31:24

标签: php

我在这里看过类似的帖子,但无法破解解决方案。我在strtolower($file['name']))),行上获得了严格的标准:只有变量应该通过引用传递错误。现在尝试寻找解决方案,但无法弄清楚如何纠正。你能突出我出错的地方吗?

注意:代码根据@ Kolink的建议编辑:

<?php
require_once 'upload_config.php';

$mydirectory = myUploadDir();

$uploaded_file_counter = 0;
$UploadLimit = $_POST['counter'];

for ($i = 0; $i <= $UploadLimit; $i++) {
    $file_tag = 'filename' . $i;
    $filename = $_FILES[$file_tag]['name'];

    if ($filename != null) {
        $rand = time();
        $str = "$rand$filename";

        // set folder name in here.
        $filedir = myUploadDir();

        //change the string format.
        $string = $filedir . $str;

        $patterns[0] = "/ /";
        $patterns[1] = "/ /";
        $patterns[1] = "/ /";

        $replacements[1] = "_";

        $dirname = strtolower(preg_replace($patterns, $replacements, $string));
        //end of changing string format

        //checking the permitted file types
        if ($check_file_extentions) {
            $allowedExtensions = allowedfiles();

            foreach ($_FILES as $file) {
                if ($file['tmp_name'] > '') {
                    /*if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) */
                    if (!in_array(array_reverse(explode(".", strtolower($file['name'])))[0], $allowedExtensions)) {
                        $fileUploadPermission = 0;
                    } else {
                        $fileUploadPermission = 1;
                    }
                }
            }
        } else {
            $fileUploadPermission = 1;
        }

        //end of checking the permitted file types
        if ($fileUploadPermission) {
            if (move_uploaded_file($_FILES[$file_tag]['tmp_name'], $dirname)) {
                echo "<p>"; ?>
                <div><?php echo "<img style='height:100px;width:200px' src='$dirname'>"; ?></div> <?php
                echo "</p>";

                $uploaded_file_counter += 1;
            }
        }
    }
}

if ($uploaded_file_counter == 0) {
    echo "<br /> <b style='font-weight:bold;color:red'>Opss! Please select an image file<b>";
} else {
    echo "<br /> <b>You request " . $i . " image files to upload and " . $uploaded_file_counter . " files uploaded sucessfully</b>";
}
?>

1 个答案:

答案 0 :(得分:2)

end通过引用获取其参数,因为它通过移动其内部指针来修改原始数组。

由于您似乎使用的是PHP 5.4,因此您可以执行此操作以获取最后一个:

if( !in_array(array_reverse(explode(".",strtolower($file['name'])))[0],$allowedExtensions))

也就是说,为了便于阅读,最好分几个步骤:

$parts = explode(".",strtolower($file['name']));
$extension = $parts[count($parts)-1];
if( !in_array($extension,$allowedExtensions)) {