FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Cronjob Fopen() Problem





Aredon
I've been playing around with file permissions all over the place, but I can't seem to get the cronjob to open the files in a cache I have made.
Quote:

PHP Warning:
fopen(/home/aredon/domains/clanhunter.frih.net/public_html/navigation/cache/armory/testiment.xml):
failed to open stream: Permission denied in
/usr/home/aredon/domains/clanhunter.frih.net/public_html/navigation/modules/armory.php
on line 132

It's flat out refusing to open the file when I run it as a cronjob, but if I open it in the browser it runs just fine. I have a few assumptions as to why, but no way to fix it really... any ideas?

I did notice that all the cache files are owned by Apache instead of my user account... if that makes a difference.
rockacola
Have you tried changing permission level of testiment.xml ?
Use your FTP client -> Change File Permission -> Numeric Value: 755
Aredon
rockacola wrote:
Have you tried changing permission level of testiment.xml ?
Use your FTP client -> Change File Permission -> Numeric Value: 755

Problem is that the XML file is generated by another script, but yes I had tried that. I was unable to set the permission from either the script itself or from DA. Which I found to be very strange, so I copied the folder and deleted the old one. For some strange reason the cache folder is now accessible by the cronjob. (I suspect it had something to do with ownership?) Only problem now is that everything else that was writing to the folder has stopped working.

All in all I think I'm ok with the user-viewed pages being all cached. Since it drastically improves loadtime when the cache is expired and needs updated. However, I would still like to have a button option for users to update the cache for their character. So at some point I now need to solve the permission problem for non-cron scripts.

We'll go over the current permissions just so you can get an idea and help me out Smile.


  • Cache (dir)

    • UID - Aredon
    • GID - Apache
    • Level - 755

  • Armory (dir)

    • UID - Aredon
    • GID - Apache
    • Level - 755

  • Each XML character file (cache)

    • UID - Aredon
    • GID - Apache
    • Level - 666 (changed - originaly 644, did not affect the cronjob's ability to write)


Currently the situation has reversed itself. When running the cronjob in the browser it returns the error: (only when trying to create a new file - when attempting to update a file I only get a chmod error. which I had somewhat expected. In any case I need the function to create a file no matter where it's ran...)
Quote:

Warning: fopen(/home/aredon/domains/clanhunter.frih.net/public_html/navigation/cache/armory/triosan.xml) [function.fopen]: failed to open stream: Permission denied in /usr/home/aredon/domains/clanhunter.frih.net/public_html/navigation/modules/armory.php on line 132
can't open file

However, while running through the command line it works fine. Don't ask me how I reversed it, I'm not even sure myself.

Since you're sure to ask, here are the two functions we are concerned with.
Code:

function pull_char_armory($name){
   define("USE_CURL", true);
   $realm = "Hyjal";
   $url = "http://www.wowarmory.com/character-sheet.xml?r=" . $realm . "&n=" . $name ;
   ## Start cURL
   $ch = curl_init();
   $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
   curl_setopt ($ch, CURLOPT_URL, $url);
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
   $f = curl_exec($ch);
   curl_close($ch);
   ## Finish cURL
   if(!$xml = new SimpleXMLElement($f)){
      return FALSE;
   }else{
      if(!$xml->characterInfo->characterTab->characterBars->health['effective'])return FALSE;
      char_xml_write($f,strtolower($name));
      return $xml;
   }
}


And the function it calls:

Code:

function char_xml_write($xml,$char){
   $filename='/home/aredon/domains/clanhunter.frih.net/public_html/navigation/cache/armory/'.$char.'.xml';
   $fh=fopen($filename, 'w') or die("can't open file");
   if(fwrite($fh, $xml)){
      chmod("$filename", 0666);
      $stuff=true;
      $sql="SELECT * FROM `armory_cache` WHERE `char`='".mysql_real_escape_string($char)."'";
      $result=mysql_query($sql);
      if(!$row=mysql_fetch_object($result)){
         $sql="INSERT INTO `armory_cache` (`char`,`timestamp`) VALUES ('".mysql_real_escape_string($char)."','".time()."')";
         @mysql_query($sql);
      }else{
         $sql="UPDATE `armory_cache` SET `timestamp`='".time()."' WHERE `char`='".mysql_real_escape_string($char)."'";
         @mysql_query($sql);
      }
   }
    fclose($fh);
   return $stuff;
}


The only thing I can think of would be to make the armory folder 777, which I know is taboo.
Aredon
update:
I have reverted the file permission 666 change as it basicaly didn't do anything. As a result I no longer get the chmod() error. Then after a good nights sleep I started troubleshooting it again, and on a goof I changed the armory folder to 775. At the moment both the cron and non-cron scripts are able to create/update chache data! Cool
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.