سؤال

I'm trying to create an online system whereby I takes files (a variety of pictures) that have been organized in folders on a server and zip them into a single folder and then download.

So far, looking at stackoverflow, I have come up with this and when I test using the linked HTML, the resulting zip file that is downloaded is a text file with a zip extension (ie. user-date.zip) that houses a few errors:

<head>
<title>File Zipper</title>
</head>

<br />
<b>Warning</b>:  filesize(): stat failed for Files/adsfa-20140329.zip in <b>D:\xampp\htdocs\filezipper\ZipScript2.php</b> on line <b>33</b><br />
<br />
<b>Warning</b>:  readfile(Files/adsfa-20140329.zip): failed to open stream: No such file or directory in <b>D:\xampp\htdocs\filezipper\ZipScript2.php</b> on line <b>35</b><br />

and here is my ZipScript2.php code:

<head>
<title>File Zipper</title>
</head>

<?php
$filelist = $_POST['filestozip'];
$username = $_POST['user'];
$today = date("Ymd");
$packagename = $username.'-'.$today.'.zip';
$package = new ZipArchive;

$package->open($packagename, ZipArchive::CREATE);

foreach ($filelist as $file) {
  $package->addFile('/Files/'.$file.'/pics', 'Files/pics'); 
}
$package->close();

if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
    header('Content-Type: "application/octet-stream"');
    header('Content-Disposition: attachment; filename="'.basename('Files/'.$packagename).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header("Content-Transfer-Encoding: binary");
    header('Pragma: public');
    header("Content-Length: ".filesize('Files/'.$packagename));
} else {
    header('Content-Type: "application/octet-stream"');
    header('Content-Disposition: attachment; filename="'.basename('Files/'.$packagename).'"');
    header("Content-Transfer-Encoding: binary");
    header('Expires: 0');
    header('Pragma: no-cache');
    header("Content-Length: ".filesize('Files/'.$packagename));
}
readfile('Files/'.packagename);
?>

In my htdocs folder (I am using dreamweaver to create this) I have my php file alone with my "Files" directory which houses the pictures in their respective subdirectories. Note: The subdirectories are named according to the names that are passed by my HTML form, and they do match (I have checked by inserting various echo commands to test).

I'm not sure that is causing this to happen - can any of you shed some light on the situation? Hopefully with a fix? :)

هل كانت مفيدة؟

المحلول

You have to fix two bugs:

  • Line 21, 26, 29 and 35: put a slash before "Files", as you did with the $package->addFile() statement.

  • Erase all the HTML code before the opening "<?php" tag. HTTP headers will be sent but won't work if there is HTML before they are sent to the browser. The opening php tag should be the first line of your script.

See also: Creating and Downloading a file using PHP

Regards,

-Gorka

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top