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?
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
I'm wondering why in some of your instructions you use relative paths, for example:
$theDir = "img/ncaa/".$team."";
And in other ones you are using absolute paths like:
$img = "/img/ncaa/".$team."/".substr($inner, 0, 4).".png";
You must choose one strategy: Absolute or relative paths. Mixing both approaches will create a mess in your program, and I believe this is why your mkdir is not working.
Before the foreach loop, print your $team value. If the value is empty, set it explicitly with $team = "Boston Celtics" before the for-each loop and see if it works.