5 Replies - 1961 Views - Last Post: 23 September 2012 - 11:46 AM Rate Topic: -----

#1 Rudde  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-November 08

PHP won't read newest version of file.

Posted 23 September 2012 - 10:04 AM

Hello, I made this script that is running in console and it's a loop that get run every 5ft minute. It's checks for changes in a file, and makes the changes in the file if something is true. So my problem is that as long the loop is going it does not get access to new file information. It keeps looking at the same old file before the change. But if I quit the script and start it over it is completely capable of seeing the new information.
Is This A Good Question/Topic? 0
  • +

Replies To: PHP won't read newest version of file.

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 906
  • View blog
  • Posts: 3,170
  • Joined: 12-May 09

Re: PHP won't read newest version of file.

Posted 23 September 2012 - 10:10 AM

Seeing the source would be helpful in identifying your problem.
Was This Post Helpful? 0
  • +
  • -

#3 Rudde  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-November 08

Re: PHP won't read newest version of file.

Posted 23 September 2012 - 10:18 AM

function FileCheck($name) 
{
	global $location;

	$filename = $location."cache.db";
	$cache = fopen($filename, "r");
	$files = array();
	$files = explode("\n", fread($cache, filesize($filename)+1));
	fclose($cache);


	if (in_array(trim($name), $files, true))
	{
	
		return true;
		
	}
	
	/*
	foreach ($files as $i) 
	{
		if(trim($name) == $i) 
		{ 	
			return true; 
		}
	}
	*/
	
	return false;	

}

function FileToCache($name) 
{
	global $location;

	$filename = $location."cache.db";
	$cache = fopen($filename, "a+");
	fwrite($cache, trim($name)."\n");
	fclose($cache);
	
	return false;
}

while (true)
{

$feeds = 100;

for ($i = 0; $i < $feeds/4; $i++)
	{
	
		$out[4][$i] = str_replace(" ", "%20", $out[4][$i]);
		
		if (FileCheck($out[2][$i]))
		{
			echo "TRUE\n";
		}
		else
		{
			echo "FALSE\n";
		}
		
		if (FileCheck($out[2][$i]))                                // Checking if torrent is already downloaded
		{
			
			continue;
			
		}
		
		if ($out[1][$i] != "")                                               // Checking if torrent is freeleech
		{
		
			$freeleech = "Yes";
		
		}
		else
		{
		
			$freeleech = "No";
		
		}
		
		switch ($out[6][$i])                                                 // Convert size to GB
		{
		
			case "GB":
				$size = $out[5][$i];
				break;
				
			case "MB":
				$size = $out[5][$i]/1024;
				break;
				
			case "KB":
				$size = $out[5][$i]/1024/1024;
				break;
				
			default:
				$size = $out[5][$i];
				break;
		
		}
		
		$size = number_format($size, 0, ".", "");                         // remove decimal
		
		
		if ((!match($out[3][$i]) && $freeleech == "No") || $size > $max_size)
		{
			
			continue;
						
		}
		
		$meta = file_get_contents($out[4][$i]);
		
		$new_file = fopen($location."torrents/".$out[2][$i].".torrent", "a+");
		
		fwrite($new_file, $meta);
		fclose($new_file);
		
		FileToCache($out[2][$i]);
		
		echo $out[3][$i]." downloaded.\n"; 

	}
	echo "\nSleep for ".$sleep." minutes - ".date("d.m.Y H:i:s")."\n";
	
	sleep($sleep*60);	                      // Now, sleep for $sleep minutes





}


Hope this helps
Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 906
  • View blog
  • Posts: 3,170
  • Joined: 12-May 09

Re: PHP won't read newest version of file.

Posted 23 September 2012 - 11:20 AM

Okay so for some clarity, since the code is really disorganized:
the file that you want to be updated each iteration is $location."cache.db"?

What are the specific symptoms that tell you it's not reading the right file?

Describe the results you expect and provide the results you get.
Was This Post Helpful? 1
  • +
  • -

#5 Rudde  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-November 08

Re: PHP won't read newest version of file.

Posted 23 September 2012 - 11:39 AM

$location is just the location I have it set on the top of the code, and this code is taken out of contex.

How is it disorganized? :( What can I do to make it more organized?

I also found the solution to my problem, I just added a clearstatcache(); in the end of the loop.
Was This Post Helpful? 0
  • +
  • -

#6 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 906
  • View blog
  • Posts: 3,170
  • Joined: 12-May 09

Re: PHP won't read newest version of file.

Posted 23 September 2012 - 11:46 AM

A few things I would strive to avoid:
  • Jumping around flow control - things like if->break/continue. Used sparingly, they're effective tools. Used too frequently, you have "spaghetti code."
  • Few comments, and especially lacking in places I need them to know what you were thinking
  • Array indexing everywhere, with no real way to tell what the indexes are referring to
  • Sections of commented out code - just adds to the noise when trying to solve your problem.


And basically it's best to explain (or show) the output or results of your program and say what's wrong about it than tell us "It's reading the old file." We're able to help you more quickly and effectively that way.

"The program is printing TRUE when I call method FileCheck and I expect it to print FALSE."
Would be an example of a useful description.

This post has been edited by xclite: 23 September 2012 - 11:50 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1