How to create a ZIP file using PHP and delete it a

2019-01-17 22:02发布

I need to download images from other websites to my server. Create a ZIP file with those images. automatically start download of created ZIP file. once download is complete the ZIP file and images should be deleted from my server.

Instead of automatic download, a download link is also fine. but other logic remains same.

标签: php download zip
8条回答
欢心
2楼-- · 2019-01-17 22:40

Any idea how many zip file downloads get interrupted and need to be continued?

If continued downloads are a small percentage of your downloads, you can delete the zip file immediately; as long as your server is still sending the file to the client, it'll remain on disk.

Once the server closes the file descriptor, the file's reference count will drop to zero, and finally its blocks on disk will be released.

But, you might spent a fair amount of time re-creating zip files if many downloads get interrupted though. Nice cheap optimization if you can get away with it.

查看更多
Summer. ? 凉城
3楼-- · 2019-01-17 22:40

Other solution: Delete past files before creation new zip file:

    // Delete past zip files script
    $files = glob('*.zip'); //get all file names in array
    $currentTime = time(); // get current time
    foreach($files as $file){ // get file from array
        $lastModifiedTime = filemtime($file); // get file creation time

        // get how old is file in hours:
        $timeDiff = abs($currentTime - $lastModifiedTime)/(60*60);

        //check if file was modified before 1 hour:
        if(is_file($file) && $timeDiff > 1)
            unlink($file); //delete file
    }
查看更多
登录 后发表回答