Link to home
Start Free TrialLog in
Avatar of webseth
webseth

asked on

Warning: Invalid argument supplied for foreach() - Suddenly....

I get the following error:
Warning: Invalid argument supplied for foreach() in /home/xxxxxx/public_html/cron_folder/pics_cron.php on line 25. Just popped up out of nowhere. Script has been working perfectly for a while now. Then this. Contacted the hosting company and they say it's nothing that they have done.

When the script searched for the file on the remote server, there will only come back one match. Any help I ccould get would be great thanks!

<?
     define ( "LOG_FILE", "/home/xxxxxx/public_html/sbpictures/log.txt" ); // log file that records the complete process
     define ( 'FTP_USER', 'xxxxxx' ); // ftp login username
     define ( 'FTP_PASS', 'xxxxxx' ); // ftp login password
     define ( 'FTP_HOST', 'xxxxxx.com' ); // ftp server name
     define ( 'SAVE_PATH', '/home/xxxxxx/public_html/sbpictures/' ); // inlude the trailing '/'

     $files = array ();

     log_file ( 'SCRIPT (START): New database dump process started', 1 );

     $io = ftp_connect ( FTP_HOST ) or log_file ( 'FTP CONNECT: connection error, ' . FTP_HOST, 0 );

     log_file ( 'FTP (CONNECT): connection to ' . FTP_HOST . ' returned true', 1 );

     if ( ( $lu = @ftp_login ( $io, FTP_USER, FTP_PASS ) ) !== false )
     {
          log_file ( 'FTP (LOGIN): login to ' . FTP_HOST . ' returned true', 1 );
             
                     $datestamp = date('Ymd');
                   $day = date('D');
                   $regex = "/Daily\.[0-9]+\.$day\.".$datestamp."RES001_photos\.zip/";
                   $filenames = ftp_nlist( $io , ".");
                  
                   foreach($filenames as $name) {
                   if(preg_match($regex,$name))
                   $get_file = $name;
                   }
               // path and name of file on the FTP server (download file)
             $new_file = $get_file; // the local temp zip file name and path... (save file)

          $lf = fopen ( $new_file, 'wb' ) or log_file ( 'FILE ERROR: can not create temp file, ' . $new_file, 0 );
             
              ftp_pasv($io, true) or log_file ( 'FTP PASV: Unable to set passive mode.', 0 );

          if ( ftp_fget ( $io, $lf, $get_file, FTP_BINARY, 0 ) !== false )
          {
               log_file ( 'FTP (GET): get download file ' . $get_file . ' returned true', 1 );

               fclose ( $lf );

               log_file ( 'FILE (SAVE): saved down load file ' . $get_file . ' to ' . $new_file, 1 );

               $zf = zip_open ( $new_file ) or log_file ( 'FILE ERROR: can not open to unzip, ' . $new_file, 0 );

               if ( $zf )
               {
                    while ( $zs = zip_read ( $zf ) )
                    {
                         if ( zip_entry_open ( $zf, $zs, 'rb' ) !== false )
                         {
                              $zo = zip_entry_read ( $zs, zip_entry_filesize ( $zs ) );
                              $fn = zip_entry_name ( $zs );

                              $fs = fopen ( SAVE_PATH . $fn, 'wb' );
                              fputs ( $fs, $zo );
                              fclose ( $fs );

                              $files[$fn] = SAVE_PATH . $fn;
                         }
                    }

                    zip_close ( $zf );

                    log_file ( 'FILE (UNZIP): process download file, ' . $new_file . ' returned true', 1 );

                    unlink ( $new_file );
               }
               else
               {
                    log_file ( 'FILE ERROR: can not open or read ' . $new_file . ' (check permissions)', 0 );
               }

          }
          else
          {
               log_file ( 'FILE ERROR: can not download ' . $get_file . ' from ' . FTP_HOST . ' (check path)', 0 );
          }

          ftp_close ( $io );

     }
     else
     {
          log_file ( 'FTP ERROR: user not allowed access on ' . FTP_HOST, 0 );
     }

     log_file ( 'SCRIPT (END): New database dump process finished, exiting...', 2 );


     function log_file ( $data, $type )
     {
          $time = date ( 'r', time () );
          $fo = fopen ( LOG_FILE, 'a' );
          fputs ( $fo, $time . ', ' . $data . "\r\n" );

          if ( $type == 2 )
          {
               fputs ( $fo, str_repeat ( '-', 72 ) . "\r\n\r\n" );
          }

          fclose ( $fo );

          if ( $type == 1 )
          {
               return;
          }

          exit ();
     }
                //remove the files from the downloded zip file

      foreach ( $in AS $k => $v )
      {
     unlink ( $v ); // $v <= hold the path and name of the file to remove
      }

          // done
?>
ASKER CERTIFIED SOLUTION
Avatar of ljubiccica
ljubiccica
Flag of Slovenia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of webseth
webseth

ASKER

We Have a Problem...
Make the same thing for $io...

if($io===false){
echo "We have a problem with ftp_connect";
}else{
echo "The ftp_connect is not a problem!";
}
Avatar of webseth

ASKER

"The ftp_connect is not a problem!".....
Check if there is a directory you are serhing for...

You search in root of the server... Did you mean to search in $regex directory?

Then you need to do something like  
$filenames = ftp_nlist( $io , ".".$regex);

or chmod from your root changed...

Check with your host what the CHMOD is...


Avatar of webseth

ASKER

regex is the file that I am searching for. The file changes daily. Could be Daily.012.Thu.20070809RES001_photos.zip etc.... depending on the day, but the file is always in the main directory. i can change the permissions as needed...
Avatar of webseth

ASKER

Turns out, was a server issue. Thanks for the help!