Garethp Posted September 16, 2008 Share Posted September 16, 2008 Ok, so I'm making a script to detect whether comics exist on certain days from Ctrl-Alt-Del (ctrlaltdel-online.com) using Fopen to respond when the file exists using an if, but responds even when it doesn't. Here's the code <?php $Year = 2002; $Month = 12; $Day = 24; ini_set('display_errors', 0); while($Year != 2003) { $Montha = $Month; $Daya = $Day; if(strlen($Month) == 1) { $Montha = "0" . $Month; } if(strlen($Day) == 1) { $Daya = "0" . $Day; } $File = "http://ctrlaltdel-online.com/comics/" . $Year . "" . $Montha . "" . $Daya . ".jpg"; if (!fopen($File, "r")) { } else { echo $File . "<br>"; } $Day += 1; if($Day == 32) { $Month += 1; $Day = 1; if($Month == 13) { $Year = $Year + 1; $Month = 1; } } }; ini_set('display_errors', 1); ?> Quote Link to comment Share on other sites More sharing options...
paul2463 Posted September 16, 2008 Share Posted September 16, 2008 why write it this way? not the answer to your question, just a coding question if (!fopen($File, "r")) { } else { echo $File . "<br>"; } why not just use if (fopen($File, "r")) { echo $File . "<br>"; } Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 16, 2008 Author Share Posted September 16, 2008 I did, didn't work, so I tried it the other way just in case. Still didn't work. I didn't think it would, but oh well Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 16, 2008 Share Posted September 16, 2008 You script is not getting an error because the website is returning the "page not found" page to your script. You have to find another way of determining that the comic is not there. Here's one way: <?php $start = strtotime('2002-12-24 '); $end = strtotime('2003-01-01'); ini_set('display_errors', 1); for ($i = $start; $i < $end; $i += 86400) { $File = "http://ctrlaltdel-online.com/comics/" . date('Ymd',$i) . ".jpg"; $tmp = array_map('trim',file($File)); if (strlen($tmp[0]) == 44) echo $File . "<br>"; } ?> BTW, I recoded your loop logic to make it much shorter. Ken Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 16, 2008 Author Share Posted September 16, 2008 Thanks. But I don't understand what array_map('trim',file($File)); does. What I saw, array_map applies an array to a function and out puts the results. But I still don't understand what it does. Just out of curiosities sake. Also if (strlen($tmp[0]) == 44) Wouldn't that be true if you got the 404 page, not the picture? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 16, 2008 Share Posted September 16, 2008 The file() function returns the contents of a file into an array. This <?php $tmp = array_map('trim',file($File)); ?> is trimming any whitespace from each entry in the returned array. It's really not needed, but if you take it out, you have to add one to the length you're testing. The first entry in the array contains either the JPEG signature of the picture or the doctype line of the 404 page <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> The signature line is 45 characters long (44 if you leave the trim in place). Ken Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 16, 2008 Author Share Posted September 16, 2008 But how does it split it into an array, what identifier does it use? Quote Link to comment Share on other sites More sharing options...
kodosai Posted September 16, 2008 Share Posted September 16, 2008 It uses $tmp as the array handle... A "basic" usage of file() is $myfile = file('myfilename.txt'); He's just, think of it as nesting functions so: $tmp = array_map('trim', file('myfilename.txt'); works in this order. Get me an array in an internal handle for file('myfilename.txt'), then perform array_map() on that results and trim whitespace, and assign the result to the $tmp array... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.