Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    199
    Thanks
    30
    Thanked 0 Times in 0 Posts

    file_get_contents via cron job doesn't work?

    I have a script that works 100% fine when I access it via a web URL, but when it runs via a cron job, it creates the directories for the images and inserts the data into the mysql db, but won't save the images. Any idea why?

    PHP Code:
    class images {
            private function 
    dl_images($images){
                
                
    $getyear date("Y");
                
    $getmonth date("m");
                
    $getday date("d");
                
                
    $post_id $this->db->insert_id;
                
                
    $thumb_folder "../images/$getyear/$getmonth/$getday/$post_id/";

                if (!
    is_dir("/public_html/images/$getyear/$getmonth/$getday/$post_id")) {
                    
    mkdir("/public_html/images/$getyear/$getmonth/$getday/$post_id"0755true);
                }
                
                
    $pickey 1;
                
                foreach(
    $images AS $image):

                    if(!
    $this->is_image($image)) continue;
                    
                    
    $image_name endexplode("/"$image ) );

                    
    copyemz ($image$thumb_folder $image_name); 
                    
                    
    $img file_get_contents($image);
                    
                    
    $imagethumb = new SimpleImage();
                    
                    
    $imagethumb->load($image);
                    
                    
    $imagethumb->resize(110,80);
                    
                    
    $image_name2 "thumb_$image_name";
                    
                    
    file_put_contents$thumb_folder $image_name $img );

                    
    $imagethumb->save$thumb_folder $image_name2 );
                    
                    
    $pickey++; 
                    
                endforeach; 
    // END OF LOOPING THROUGH IMAGES    
                
            
    }

    My cron job:

    PHP Code:
    /usr/local/bin/php -/public_html/feed/cron.php 

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    There paths look wrong to me.
    If this is a shared host, I doubt you'll find anything on /public_html unless you are jailed. Provide an absolute path to this, or run relative to JUST this script via dirname(__FILE__). The actual $thumb_folder path looks correct as a relative, but the /public_html does not. More often than not that should be /users/youruser/public_html for example (although you can just use a relative if you run it against the dirname(__FILE__) first).

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    199
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    There paths look wrong to me.
    If this is a shared host, I doubt you'll find anything on /public_html unless you are jailed. Provide an absolute path to this, or run relative to JUST this script via dirname(__FILE__). The actual $thumb_folder path looks correct as a relative, but the /public_html does not. More often than not that should be /users/youruser/public_html for example (although you can just use a relative if you run it against the dirname(__FILE__) first).
    I changed the path in the script for security, the actual path I am using is /home/user/public_html

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    May be a silly question; have you instantiated and called this class in cron.php?

    Add some quick error reporting as well, and log it to a file / print it out and redirect it from the cron to a log. See if it has anything in particular.
    Also, are these predefined directories, or are these directories that are being created by the cron? If they are precreated, then its highly probable that you do not have write privilege to the directories from the cron tab. The cron is not run via the httpd user in use, rather by your own user. So with 0755 you have full control for owner and read/execute for group and everyone.
    The first thing I'd try is opening it up to 0777. If it writes its now a permission problem. To fix, you can decide if this is something that you will always need to write to from both nobody and from your user, and if so, chown it to your own user and chgrp it to nobody (or whatever the httpd group is under) and provide it with 0775 instead.

    If your crontab lets you change it, I'd change it to the httpd user (nobody, apache, httpd, www-data, etc it varies I'm afraid - use PHP to create a file / directory then use a chown to see who owns it if you can't access the security on the machine). Stat it to find the user:
    PHP Code:
    $stat stat($path);
    print_r(posix_getpwuid($stat['uid'])); 
    Edit:
    In hinds sight, you'll need to change the group. User can only be specified in a system crontab if I'm not mistaken.
    Last edited by Fou-Lu; 01-09-2012 at 09:43 PM.

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Posts
    199
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    May be a silly question; have you instantiated and called this class in cron.php?

    Add some quick error reporting as well, and log it to a file / print it out and redirect it from the cron to a log. See if it has anything in particular.
    Also, are these predefined directories, or are these directories that are being created by the cron? If they are precreated, then its highly probable that you do not have write privilege to the directories from the cron tab. The cron is not run via the httpd user in use, rather by your own user. So with 0755 you have full control for owner and read/execute for group and everyone.
    The first thing I'd try is opening it up to 0777. If it writes its now a permission problem. To fix, you can decide if this is something that you will always need to write to from both nobody and from your user, and if so, chown it to your own user and chgrp it to nobody (or whatever the httpd group is under) and provide it with 0775 instead.

    If your crontab lets you change it, I'd change it to the httpd user (nobody, apache, httpd, www-data, etc it varies I'm afraid - use PHP to create a file / directory then use a chown to see who owns it if you can't access the security on the machine). Stat it to find the user:
    PHP Code:
    $stat stat($path);
    print_r(posix_getpwuid($stat['uid'])); 
    Edit:
    In hinds sight, you'll need to change the group. User can only be specified in a system crontab if I'm not mistaken.
    Sorry for the late reply, I've been away visiting another country, just got back to work on this script. When you say call and instantiate this script from the cron.php, do you mean require_once()?

    And I added error reporting with error_reporting(E_ALL);, but no errors at all. That script does a lot more than just store the images, it reads data and stores it in a mysql database which works fine, it's only the images having problems. The directories are created dynamically at the beginning of the script. How can I change to the httpd user?

    I tried changing the permissions to 777 but still doesn't work.
    Last edited by Remix919; 02-08-2012 at 07:46 PM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No, I mean new Images().
    I just realized as well that this method is private. If this is all the class has, it will never be able to run since that method can never be called except from within Images, and since no other methods exist in Images it can never be called.

  • #7
    Regular Coder
    Join Date
    Jan 2006
    Posts
    199
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    No, I mean new Images().
    I just realized as well that this method is private. If this is all the class has, it will never be able to run since that method can never be called except from within Images, and since no other methods exist in Images it can never be called.
    Will it work fine if I make it public? And is there any negative aspect to making all the classes public? The script is pretty basic and doesn't process any sensitive information or anything. I only made them private functions because the tutorial I had looked at did it this way.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No you still need to call it. This method requires an argument to be provided for it as well.
    I thought you said you had this working on a stand alone page? What did you do there?

    Private functions cannot be accessed outside of that class. There is no way to call it without invoking something within the class, which may have been done via a constructor.

  • #9
    Regular Coder
    Join Date
    Jan 2006
    Posts
    199
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    No you still need to call it. This method requires an argument to be provided for it as well.
    I thought you said you had this working on a stand alone page? What did you do there?

    Private functions cannot be accessed outside of that class. There is no way to call it without invoking something within the class, which may have been done via a constructor.
    It works fine when I access it with a web browser, it's just the cronjob that's having problems. And the cron script works, it's actually just the part with the images not working, it doesn't save any of the images. And this must be the constructor your talking about?

    PHP Code:
            function __construct($url){
                
                
    $this->url $url;
                
                
    $this->db = new MySQLi("localhost","user","12345","classifieds");
                
                
    $this->get_posts();
                
                
    $this->format_posts();
                
                
    $this->add_posts();
                
            }
            
            private function 
    add_posts(){

                
    $sql $this->db->query("SELECT idCat FROM `cats` WHERE `url` = '$this->url'");

                
    $info $sql->fetch_object();
                
                
    $cat_id $info->idCategory;
                
                
    $wp_fields "(`isConfirmed`,`idCategory`,`title`,`description`,`email`,`place`,`name`,`insertDate`,`hasImages`)";

                
    $date date("Y-m-d H:i:s");
                
                
    $gmdate gmdate("Y-m-d H:i:s");
                
                foreach(
    $this->ads AS $post):
                
                    if( 
    $this->is_old_post$post ) )    continue;
                    
                    
    $getimages $post["images"];
                    if(
    $getimages[0] != "") {
                    
    $checkimages 1;
                    } else {
                    
    $checkimages 0;
                    }

                    
    $wp_query "(1,'$cat_id','" $this->db->real_escape_string($post["title"]) . "','" $this->db->real_escape_string($post["description"]) . "','noreply@yourdomain.com','" $this->db->real_escape_string($post["location"]) . "','Guest','$date','$checkimages')";
                
                    
    $this->db->query("INSERT INTO table $wp_fields VALUES $wp_query");
                    
                    
    $post_id $this->db->insert_id;
                    
                    
    $this->dl_images$post["images"] );
                    
                    
    $this->records_added++;
                
                endforeach;
                
            } 

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yeah, it will either be called __construct, or be named the same as the class itself. PHP 5 took the route of the __construct instead of retaining the PHP4 class name convention (which IMO is really bizarre as most OO language just use the name of the class).
    If images has a constructor that calls that method, or other methods in it that call that method, then that's fine. But from just the class image provided, it doesn't have the capability to actually do anything in any context. I see a call to $this->dl_images indicating that this entire class is that of images. This is the problem with posting partial data.
    Creating an instance requires a simple new images($url);. That code has to be available for cron.php to use.

    This brings us back to the problem with the permissions. Have you logged the results of the script errors to a file or emailed to evaluate them?

  • #11
    Regular Coder
    Join Date
    Jan 2006
    Posts
    199
    Thanks
    30
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Yeah, it will either be called __construct, or be named the same as the class itself. PHP 5 took the route of the __construct instead of retaining the PHP4 class name convention (which IMO is really bizarre as most OO language just use the name of the class).
    If images has a constructor that calls that method, or other methods in it that call that method, then that's fine. But from just the class image provided, it doesn't have the capability to actually do anything in any context. I see a call to $this->dl_images indicating that this entire class is that of images. This is the problem with posting partial data.
    Creating an instance requires a simple new images($url);. That code has to be available for cron.php to use.

    This brings us back to the problem with the permissions. Have you logged the results of the script errors to a file or emailed to evaluate them?
    I turned on the error reporting but it didn't log any errors?

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Make sure you write to your own log file in an accessible location as well or have the cron email you the results. This isn't executed by your webserver, so you won't likely see anything within a log file on the webhost.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •