Question

Error: readdir() expects parameter 1 to be resource, string given

$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;
            }
        }

My function 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;
    }

I know the problem is $chaine is a string value, but I cannot fix the problem to make it accept by readdir. Is there a way to make $chaine become valuable for the readdir function?

Was it helpful?

Solution

The readdir function accepts a resource obtained from the opendir function:

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

See readdir and opendir

OTHER TIPS

readdir() accepts a file handle, not a string:

Quoting from the manual:

dir_handle: The directory handle resource previously opened with opendir(). If the directory handle is not specified, the last link opened by opendir() is assumed.`

So perhaps use opendir() first?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top