无法使readdir接受字符串var

时间:2013-09-29 15:06:38

标签: php string readdir

错误: readdir()期望参数1为资源,给定字符串

$directorypremium = 'uploads/premium/'.text2url($pseudo_cookies).'/'; 

while($file = readdir($directorypremium)) {
        if($file != '.' AND $file != '..')
            {
            $taille_fichier = filesize($directory.$file); $taille_fichier = round($taille_fichier / 1000); $total_size = $total_size + $taille_fichier;
            }
        }

我的功能text2url:

function text2url($chaine)
    {
    $chaine = htmlentities($chaine, ENT_NOQUOTES, 'utf-8');
    $chaine = preg_replace('#\&([A-za-z])(?:uml|circ|tilde|acute|grave|cedil|ring)\;#', '\1', $chaine);
    $chaine = preg_replace('#\&([A-za-z]{2})(?:lig)\;#', '\1', $chaine);
    $chaine = preg_replace('#\&[^;]+\;#', '', $chaine);
    $chaine = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $chaine);
    $chaine = str_replace('(', '', $chaine);
    $chaine = str_replace(')', '', $chaine);
    $chaine = str_replace('[', '', $chaine);
    $chaine = str_replace(']', '', $chaine);
    $chaine = str_replace('.', '-', $chaine);
    $chaine = trim($chaine);
    $chaine = str_replace(' ', '_', $chaine);

    return $chaine;
    }

我知道问题是$ chaine是一个字符串值,但我无法通过readdir解决问题。有没有办法让$ chaine对readdir函数有价值?

2 个答案:

答案 0 :(得分:2)

readdir函数接受从opendir函数获取的资源:

$dir = opendir($path);
while($file = readdir($dir)){
...

请参阅readdiropendir

答案 1 :(得分:1)

readdir()接受文件句柄,而不是字符串:

引用manual

  

<强> dir_handle :   先前使用opendir()打开的目录句柄资源。如果未指定目录句柄,则假定opendir()打开的最后一个链接。

所以也许首先使用opendir()