Jump to content

Recommended Posts

I am new to Drupal and am attempting to build a custom module to connect to ftp, download a zip file and unzip the file on the server. The zip file contains two product csv files to update price lists on an ecommerce site. I have gotten it worked out to connect, and download the file. But no matter what code I try I cannot seem to get the files extracted. I can get the code working within a separate php file for testing but once I put it in the php and run from the cron tab it refuses to unzip. I am at a loss as to what to try next. I would really appreciate any pointers I could get!

 

These are some of the things I have tried... the closest I have gotten is to have the first csv file within to create and start but it never finishes and the second file never starts. After changing the code so many times I couldn't even begin to tell you what I had when it was able to get that far.

 

$server_file = 'ItemExport.zip';
$shell = "unzip $server_file";
$shell = escapeshellcmd($shell);
exec($shell);

 

$zip = new ZipArchive();
if ($zip->open('ItemExport.zip') === TRUE) {
   for($i = 0; $i < ($zip->numFiles); $i++) {
    $filename = $zip->getNameIndex($i);
    $zip->extractTo('.', array(trim($filename)));
}
   $zip->close();
} else {
   echo 'failed';
}

 

$zip = zip_open("ItemExport.zip");
if ($zip) {
 while ($zip_entry = zip_read($zip)) {
   $fp = fopen(zip_entry_name($zip_entry), "w");
   if (zip_entry_open($zip, $zip_entry, "r")) {
  $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
  fwrite($fp,"$buf");
  zip_entry_close($zip_entry);
  fclose($fp);
   }
 }
 zip_close($zip);
}

 

$zip_entry = 'ItemExport.zip';
$file_name = array('StandardExport.csv', 'AmazonExport.csv');
//get the file's size
$fileSize = zip_entry_filesize($zip_entry);
for($x = 0; $x<2;$x++)
{
//open the target file
$outFile = fopen($file_name[$x],"wb");
while ($fileSize>0) {
 //read/write only up to 10kb per step
 $readSize = min($fileSize,10240);
 //decrease the size of the remaining data to read
 $fileSize -= $readSize;
 //get the data
 $content = zip_entry_read($zip_entry, $readSize);
 //write the data (if any)
 if ($content !== false)
  fwrite($outFile,$content);
}
fclose($outFile);
}

Link to comment
https://forums.phpfreaks.com/topic/268714-drupal-7-php-unzipping-problem/
Share on other sites

I ended up just moving the files. If anyone has a better approach I would greatly appreciate it! =) Thanks! Pam

 

$shell = "unzip $local_file";
   $shell = escapeshellcmd($shell);
   exec($shell,$nu);
rename("StandardExport.csv", "$file_path/StandardExport.csv");
rename("AmazonExport.csv", "$file_path/AmazonExport.csv");

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.