Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行cron作业时PHP文件中出现错误_Php_Cron - Fatal编程技术网

运行cron作业时PHP文件中出现错误

运行cron作业时PHP文件中出现错误,php,cron,Php,Cron,我试图每4天通过cron运行一个php文件。 它尝试对文件和数据库进行备份 #!/usr/bin/php -q <?php if($filecount<=4) { $host = "internal-db.host.gridserver.com"; //host name $username = "user"; //username $password = "Password"; // your password $dbname = "Dataabse"; // database n

我试图每4天通过cron运行一个php文件。 它尝试对文件和数据库进行备份

#!/usr/bin/php -q
<?php
if($filecount<=4)
{
$host = "internal-db.host.gridserver.com"; //host name
$username = "user"; //username
$password = "Password"; // your password
$dbname = "Dataabse"; // database name

$dir = "/backup-all";
if(!(file_exists($dir))) {

}

$zip = new ZipArchive();
//backup_tables($host, $username, $password, $dbname);


/* backup the db OR just a table */
function backup_tables1($host,$user,$pass,$name,$tables = '*')
{
$con = mysql_connect($host,$user,$pass);
mysql_select_db($name,$con);

//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
$return = "";

//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "nn".$row2[1].";nn";

while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = preg_replace("#n#","n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");n";
}
$return.="nnn";
}

//save file
$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}



if (glob("*.sql") != false)
{
$filecount = count(glob("*.sql"));
$arr_file = glob("*.sql");

for($j=0;$j<$filecount;$j++)
{
$res = $zip->open($arr_file[$j].".zip", ZipArchive::CREATE);
if ($res === TRUE)
{
$zip->addFile($arr_file[$j]);
$zip->close();
unlink($arr_file[$j]);
}
}
}



//get the current folder name-start
$path = dirname($_SERVER['PHP_SELF']);
$position = strrpos($path,'/') + 1;
$folder_name = substr($path,$position);
//get the current folder name-end



$zipname = date('Y/m/d');
$str = "figata-".$zipname.".zip";
$str = str_replace("/", "-", $str);


// open archive
if ($zip->open($str, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
// initialize an iterator
// pass it the directory to be processed
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("/home/161441/users/.home/domains/growing-your-business.org/html/"));
// iterate over the directory
// add each file found to the archive

foreach ($iterator as $key=>$value) {
if( strstr(realpath($key), "figata") == FALSE) {
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}

}
// close and save archive
$zip->close();


if(glob("*.sql.zip") != false) {
$filecount = count(glob("*.sql.zip"));
$arr_file = glob("*.sql.zip");

for($j=0;$j<$filecount;$j++)
{
unlink($arr_file[$j]);
}
}


//get the array of zip files
if(glob("*.zip") != false) {
$arr_zip = glob("*.zip");
}

//copy the backup zip file to site-backup-figata folder
foreach ($arr_zip as $key => $value) {   // **Line 142**
if (strstr($value, "figata")) {
$delete_zip[] = $value;
copy("$value", "$dir/$value");
}
}


for ($i=0; $i < count($delete_zip); $i++) {
unlink($delete_zip[$i]);
}

$dir ="home/domains/growing-your-business.org/backup-growing/";//dir absolute path
$interval = strtotime('-96 hours');//files older than 24hours

foreach (glob($dir."*.zip") as $file) // **Line 157**
    //delete if older
    if (filemtime($file) <= $interval ) unlink($file);

echo '<script>alert("Archive created successfully.")</script>';
}
else
{
    echo '<script>alert("Archive Not created successfully.")</script>';
}
?>
#/usr/bin/php-q

对于第142行

您可以先检查
$arr\u zip
是否已设置,或者对其进行定义

$arr_zip = array( ); //create an empty array

//get the array of zip files
if(glob("*.zip") != false) {
    $arr_zip = glob("*.zip");
}

//copy the backup zip file to site-backup-figata folder
foreach ($arr_zip as $key => $value) {   // **Line 142**
    if (strstr($value, "figata")) {
        $delete_zip[] = $value;
        copy("$value", "$dir/$value");
    }
}

我更喜欢定义它,而不是检查它的设置

对于第157行

几乎可以复制您在第142行中所做的操作:

$files = array( );

if( glob($dir."*.zip") != false ) {
    $files = glob($dir."*.zip");
}
foreach ($files as $file) // **Line 157**

你能突出显示142行和157行吗?你能试着把代码缩进吗?。我发现它很难阅读…在第一个网站上,它看起来像是一个路径问题。当CLI从另一个“工作目录”运行时,则从Web服务器运行。您应该手动从CLI运行脚本,并将一些调试语句放在适当的位置,例如,
print\r($iterator)
。根据@giorgio的观察,
$dir=“home/domains/
应该是
$dir=“/home/domains/
绝对路径必须以
/
开头,否则路径将附加到当前位置
glob
可能返回
false
if( isset( $arr_zip ) )
{
    foreach ($arr_zip as $key => $value) {   // **Line 142**
        if (strstr($value, "figata")) {
            $delete_zip[] = $value;
            copy("$value", "$dir/$value");
        }
    }
}
$files = array( );

if( glob($dir."*.zip") != false ) {
    $files = glob($dir."*.zip");
}
foreach ($files as $file) // **Line 157**