Konara mudiyanse
Konara mudiyanse

Reputation: 3

Strict standard error in PHP

I've looked at similar posts here but couldn't crack a solution. I'm getting the Strict Standards: Only variables should be passed by reference error here on the strtolower($file['name']))), line. Tried hours now looking for a solution but couldn't figure out how to correct. Can you highlight where I've gone wrong?

Note: Code Edited as per @Kolink's suggestion below:

<?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>";
}
?>

Upvotes: 0

Views: 249

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

end takes its argument by reference, since it modifies the original array by moving its internal pointer.

Since you appear to be using PHP 5.4, you can do this instead to get the last one:

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

That said, it's probably better to do it in several steps for readability:

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

Upvotes: 1

Related Questions