Jump to content

Fopen not giving errors


Garethp

Recommended Posts

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);

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.