drhozgt6007 2012-10-03 07:11
浏览 143

mkdir()函数给出无效参数错误

I'm practicing scraping data from some third party sites with an html parser. While I'm looping thru the given tags, I use the mkdir() function to create a new folder that is named after one of the variables that is being used in the loop. My code is as follows:

foreach($s->find('a') as $t)
{
    $inner = $t->plaintext;
    if(!is_dir("img/ncaa/".$team."")
    && !file_exists("/img/ncaa/".$team."/".substr($inner, 0, 4).".png"))
    {
        foreach($t->find('img') as $l)
        {
               $url = $l->src;
        }   

        mkdir("img/ncaa/".$team."");

        $img = "/img/ncaa/".$team."/".substr($inner, 0, 4).".png";
        file_put_contents($img, file_get_contents($url));
    }   
}

I get an error message reading: Warning: mkdir() [function.mkdir]: Invalid argument in

When I comment out all of the above code and simply write

mkdir("img/ncaa/Boston Celtics");

That seems to work. I thought for a bit that it may have had something to do with permissions, but it didn't.

Any suggestions?

  • 写回答

3条回答 默认 最新

  • doupa1883 2012-10-03 07:14
    关注

    Run this instead:

    foreach($s->find('a') as $t)
    {
        $inner = $t->plaintext;
        if(!is_dir("img/ncaa/".$team."")
        && !file_exists("/img/ncaa/".$team."/".substr($inner, 0, 4).".png"))
        {
            foreach($t->find('img') as $l)
            {
                   $url = $l->src;
            }   
    
            $theDir = "img/ncaa/".$team."";
            var_dump($theDir);
            mkdir($theDir);
    
            $img = "/img/ncaa/".$team."/".substr($inner, 0, 4).".png";
            file_put_contents($img, file_get_contents($url));
        }   
    }
    

    And inspect the output, it will probably reveal the error

    评论

报告相同问题?