确认文件是否已成功下载的布尔方法-PHP

时间:2018-07-20 03:18:43

标签: php

我是PHP的新手。因此,我正在尝试将MySQL查询转换为xls文件,强制下载,并在下载文件后立即将其压缩。但是下载完成后,我似乎无法自动将其压缩。

HTML:

public class News {

    private String message, author, thumb_author, type;
    private Long timestamp;

    public Map<String,String> images; // this is a new field for the images

    public String getMessage() {
       return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    ...

BACKUP_DB.PHP:

将查询转换为xls并下载:

<form method="post" action="backup_db.php">
 <input type="submit" name="export" value="Export" />
</form>

现在将其压缩,然后将zip文件移动到其他目录...

    $connect = mysqli_connect("localhost", "root", "", "overtimedtr");
    $output = '';

    $query = "SELECT * FROM dtr";
    $result = mysqli_query($connect, $query);

    if(mysqli_num_rows($result) > 0){

        $output .= '
        <table class="table" bordered="1">  
                <tr>  
                    <th>dtrID</th>  
                    <th>employeeID</th>  
                    <th>date</th>  
                    <th>remarks</th>
                    <th>start time</th>
                    <th>end time</th>
                    <th>total hours</th>
                    <th>employee signature</th>
                    <th>leadman signature</th>
                    <th>timekeeper signature</th>
                </tr>
            ';

            while($row = mysqli_fetch_array($result)){

                $output .= '
                    <tr>  
                        <td>'.$row["dtr_id"].'</td>  
                        <td>'.$row["emp_id"].'</td>  
                        <td>'.$row["date"].'</td>  
                        <td>'.$row["remarks"].'</td>  
                        <td>'.$row["start_time"].'</td>
                        <td>'.$row["end_time"].'</td>
                        <td>'.$row["total_hours"].'</td>
                        <td>'.$row["emp_signature"].'</td>
                        <td>'.$row["leadman_signature"].'</td>
                        <td>'.$row["keeper_signature"].'</td>
                    </tr>
                ';
            }
        $output .= '</table>';
        header("Content-Type: application/xls");
        header("Content-Disposition: attachment; filename=download.xls");
        echo $output;

我的猜测是,脚本在浏览器甚至无法将文件插入到if(file_exists("C:/Users/genesis/Downloads/download.xls")){ $zip = new ZipArchive(); $db_file = date("Y-m-d-H-i-s"); $filename = "C:/DTRbackups/".$db_file.".zip"; if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) { exit("cannot open <$filename>n"); } $zip->setPassword('secret'); $zip->addFile("C:/Users/genesis/Downloads/download.xls" , $db_file.".xls"); $zip->setEncryptionName($db_file.".xls", ZipArchive::EM_AES_256); $zip->close(); //Now delete the .xls file without any warning @unlink("C:/Users/genesis/Downloads/download.xls"); }else if(!file_exists("C:/Users/genesis/Downloads/download.xls")){ echo "File not found!"; } } 之前执行和结束的速度都太快了,因为该脚本还不存在,脚本无法找到文件。(我不是真的确定这是否真的是PHP的工作原理),这就是原因,也许压缩部分无法正常工作。因此,我在想,如果PHP中有某种方法/函数可以检查下载完成后是否已成功将下载的文件成功插入C:/Users/genesis/Downloads/download.xls中,,那么也许我可以解决方法,也许是这样的:

C:/Users/genesis/Downloads/

有没有类似的方法可以实现这一目标?不过,我对任何更好的解决方案都持开放态度,我只需要将我的MySQL数据转换为if(fileInsertedSuccessfully("C:/Users/genesis/Downloads/download.xls")){ //do the zipping here } 并将其压缩,这两个操作都只需单击一下按钮即可完成。

0 个答案:

没有答案
相关问题