簡體   English   中英

在特定功能內無法訪問變量? 不含脂肪的框架

[英]Variable not accessible within a particular function? Fat-Free-Framework

我成功編寫了會話變量。 我成功獲取了會話變量。 我什至可以手動定義它,但是它永遠不會傳遞給下面的函數($ fileBaseName,$ formFieldName)的文件名部分。

任何幫助是極大的贊賞。 謝謝!

$uuid = $f3->get('SESSION.uuid'); // get session uuid

$f3->set('UPLOADS', $f3->UPLOAD_IMAGES); // set upload dir

$files = $web->receive(
    function($file,$formFieldName) {

        if($file['size'] > (5 * 1024 * 1024)) // if bigger than 5 MB
            return false; // this file is not valid, return false will skip moving it

        $allowedFile = false; // do not trust mime type until proven trust worthy
        for ($i=0; $i < count($allowedTypes); $i++) {
            if ($file['type'] == $allowedTypes[$i]) {
                $allowedFile = true; // trusted type found!
            }
        }

        // return true if it the file meets requirements
        ($allowedFile ? true : false);
    },

    true, //overwrite

    function($fileBaseName, $formFieldName) {

        $pathparts = pathinfo($fileBaseName);

        if ($pathparts['extension']) {

            // custom file name (uuid) + ext
            return ($uuid . '.' . strtolower($pathparts['extension']));

        } else {
            return $uuid; // custom file name (md5)
        }
    }
);

傳遞給$web->receive()的兩個函數是closures 在PHP中,閉包看不到在聲明它們的范圍內聲明的變量。 要使這些變量可見,可以使用use關鍵字:

$uuid = $f3->get('SESSION.uuid'); // get session uuid

$f3->set('UPLOADS', $f3->UPLOAD_IMAGES); // set upload dir

$files = $web->receive(
    function($file,$formFieldName) {
        //...
    },

    true, //overwrite

    function($fileBaseName, $formFieldName) use ($uuid) {
        //...
    }
);

這應該使$ uuid在第二個函數中可見。

PHP變量范圍

由於未定義變量$uuid ,因此可能超出范圍。

您需要將變量傳遞給函數,聲明為全局變量或設置類屬性(如果這與類一起使用)。 如果在會話中設置了它,則可以直接調用它,而無需將其分配給加載會話的任何位置的變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM