Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select a directory, zip it, then download.

Status
Not open for further replies.

canajun

Programmer
Sep 28, 2002
57
CA
I am trying to modify a script to be able to select a directory from a list of directories, zip it, then download it.. so far, I haven't been successful.

here is the form which lists the directories:

Code:
<table width="541" border="0" align="center" bgcolor="#FFFF00">
<tr>
    
    <td valign="middle" bgcolor="#FFFF00"><form action="getcustomerfiles.php" method="post"><div align="center"><span class="style26">Directory: </span><select name="file">
          
<? $folder = "../reseller/"; 
$handle = opendir($folder); 

# Making an array containing the files in the current directory: 
while ($file = readdir($handle)) 
{ 
    $files[] = $file; 
} 
closedir($handle); 

#echo the files 
foreach ($files as $file) { 
    print "<option value=$folder$file>$file</option>"; 
} 
?></select><input name="Get Directory" type="submit" value="Get Directory" /></div></form></td></tr></table>

This seems to work ok, and passes the directory name as 'file'; next:

Code:
<?PHP
@$directory = addslashes($_POST['file']);
// this script zips up a directory (on the fly) and delivers it 
// C.Kaiser 2002
// No Copyright, free to use.
// get the filename of this php file without extension.  
// that is also the directory to zip and the name of the
// zip file to deliver
//  $filename_no_ext=str_replace(".php","",basename($SCRIPT_NAME)); 
  
$file =str_replace("../reseller/","",$directory);


  // we deliver a zip file
  header("Content-Type: archive/zip");

  // filename for the browser to save the zip file
  header("Content-Disposition: attachment; filename=$file".".zip");

  // get a tmp name for the .zip
  $tmp_zip = tempnam ("tmp", "tempname") . ".zip";

  // zip the stuff (dir and all in there) into the tmp_zip file
  `zip -r $tmp_zip $file`;
 
  // calc the length of the zip. it is needed for the progress bar of the browser
  $filesize = filesize($tmp_zip);
  header("Content-Length: $filesize");

  // deliver the zip file
  $fp = fopen("$tmp_zip","r");
  echo fpassthru($fp);

  // clean up the tmp zip file
  `rm $tmp_zip `;
?>

So, after my modification, I get a file appropriatley name after the directory, but instead of the directory, I get an empty zip file. It should be noted the above script work perfectly when used as the author intended, but I need to be able to select a directory from a list, and the list is not static.

Here is the contents of the error log:

Code:
[24-Apr-2007 19:15:17] PHP Warning:  filesize(): Stat failed for /tmp/tempnameFF9AYc.zip (errno=2 - No such file or directory) in /home/xxx/public_html/admin/getcustomerfiles.php on line 27
[24-Apr-2007 19:15:17] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/xxx/public_html/admin/getcustomerfiles.php:27) in /home/xxx/public_html/admin/getcustomerfiles.php on line 28
[24-Apr-2007 19:15:17] PHP Warning:  fopen(/tmp/tempnameFF9AYc.zip): failed to open stream: No such file or directory in /home/xxx/public_html/admin/getcustomerfiles.php on line 31
[24-Apr-2007 19:15:17] PHP Warning:  fpassthru(): supplied argument is not a valid stream resource in /home/xxx/public_html/admin/getcustomerfiles.php on line 32

any help at fixing this mess would be appreciated.. I have tried all day, with many different combos, with no success.

Thanks!
 
the error reported above indicates that your zip file never gets created. have you set the permissions correctly?

this line
Code:
print "<option value=[red]\"[/red]$folder$file[red]\"[/red]>$file</option>";
should be amended as shown in red

i would not use .. in folder names. instead get a real path and use that
Code:
$file =str_replace("../reseller/","",$directory);
becomes
$file = realpath($_POST['file']);
 
Ok, that made it work, but now the zip file contains all of the directories in the real path.. ie to get to the end directory, you have to drill down through the path to it.. is there any way to just have the selected directory in the zip file?

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top