• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

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
?>
0
webseth
Asked:
webseth
  • 4
  • 3
1 Solution
 
ljubiccicaCommented:
Hey!

On line 25 try this:

if($filenames===false){
echo "We have a problem";
}else{
foreach($filenames as $name) {
if(preg_match($regex,$name))
$get_file = $name;
}
}

And tell me what you've got...

Greets
Ljubiccica
0
 
websethAuthor Commented:
We Have a Problem...
0
 
ljubiccicaCommented:
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!";
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
websethAuthor Commented:
"The ftp_connect is not a problem!".....
0
 
ljubiccicaCommented:
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...


0
 
websethAuthor Commented:
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...
0
 
websethAuthor Commented:
Turns out, was a server issue. Thanks for the help!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now