如何在php中回显日期

时间:2013-11-04 04:53:16

标签: php date

这段代码正在做Zip&下载完美,但我想更改时间,我该怎么做

当我保存文件夹时,保存时间我想用日期保存我该怎么做

这是时间脚本我怎样才能更改日期,当我更改Y-m-d但这不起作用时显示此错误解析错误:语法错误,第26行下载list.php中的意外T_STRING

请帮我解决这个问题

感谢

$filename = Y-m-d " Backup.zip"; (not working)

$filename =  time() ." Backup.zip"; ( working code)

downloadlist.php

<?php
        // function download($file) downloads file provided in $file
        function download($file) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            exit;
        }

        $files = $_POST['file'];
        if(empty($files)) 
        {
            echo("You haven't selected any file to download.");
        } 
        else 
        {
            $zip = new ZipArchive();
            $filename =  time() ." Backup.zip"; //adds timestamp to zip archive so every file has unique filename
            if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { // creates new zip archive
                    exit("Cannot open <$filename>\n");
            }
            $N = count($files);
            for($i=0; $i < $N; $i++)
            {
              $zip->addFile($files[$i], $files[$i]); //add files to archive
            }
            $numFiles = $zip->numFiles;
            $zip->close();

            $time = 8; //how long in seconds do we wait for files to be archived.
            $found = false;
            for($i=0; $i<$time; $i++){

             if($numFiles == $N){   // check if number of files in zip archive equals number of checked files
                download($filename);
                $found = true;
                break;
             }
             sleep(1); // if not found wait one second before continue looping
         }

         if($found) { }
             else echo "Sorry, this is taking too long";
         }
    ?>

list.php的

<?php

function listDir($dirName) 
{  
    $forbidden_files=array('.htaccess','.htpasswd');

    $allow_ext=array('.pdf','.doc','.docx','.xls','.xlsx','.txt');
    ?><form name="filelist" action="downloadList.php" method="POST"><?php echo "\n";
    if ($handle = opendir($dirName)) {
        while (false !== ($file = readdir($handle))     ) {
            $allowed=(strpos($file,'.')!==false  &&  in_array(substr($file,strpos($file,'.')) ,$allow_ext ));

           if ($file != "." && $file != ".."  && $allowed ) { ?>    <input type=checkbox name="file[]" value="<?php  echo "$file";?>"><?php  echo "$file"; ?><br><?php  echo "\n";
            }
        }
    closedir($handle);
    }
    ?><br><input type="submit" name="formSubmit" value="Zip and download" /></form><?php
}
listDir('.');  ?>

1 个答案:

答案 0 :(得分:-1)

  

$ filename = Y-m-d“Backup.zip”; (不工作)

因为您未在此处使用date()。此外,您不是使用.连接字符串。

$filename = date('Y-m-d')."Backup.zip";

以上代码适合您。