dsjbest2014 2010-10-21 14:25
浏览 64
已采纳

如何添加一个txt文件并在php中创建一个ZIP [重复]

Possible Duplicate:
Open file, write to file, save file as a zip and stream to user for download

I have a txt file in uploads folder where i need to add this file and create a ZIP on fly and prompt the user to download the Zip file which should contain the text file .How do i do this and moreover what are the headers needed for prompting the user for download .can any one Help me out. I have used the same constucts from PHP Manual.Iam Using the same function to zip the file "http://davidwalsh.name/create-zip-php" and iam writing this code and iam getting prompted for zip download but iam getting the caution message when i extarct the file

$zip_file = "my-archive.zip";
$file_path="../../../downloads/";
$files_to_zip = array($file_path."test_testing.txt");
$result = create_zip($files_to_zip,'my-archive.zip');

header('Content-Description: File Transfer');    
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=".$zip_file);
header("Pragma: no-cache");
header("Expires: 0");

readfile($zip_file);    

The Caution error message is "one or more files in this archive uses ".."(parent folder) as part of its folder information "

  • 写回答

1条回答 默认 最新

  • dongxuan1314 2010-10-21 15:36
    关注

    In the create_zip() function, change

    foreach($valid_files as $file) {
        $zip->addFile($file,$file);
    }
    

    to

    foreach($valid_files as $file) {
        $zip->addFile($file,pathinfo($file,PATHINFO_BASENAME));
    }
    

    This will work as long as you don't have files with duplicate names but different directories in your array of files

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line