1

I have created a little module to try and unzip files that has just been uploaded through node creation. Here is my unzip function (the $file parameter is a file object):

function unzip($file) {
  watchdog('beep','Unzipping a project!!');
  $zip = new ZipArchive;
    //This Works
    $res = $zip->open(drupal_realpath($file->uri));
    // This does not
    //$res = $zip->open($file->uri);
  if ($res === TRUE) {
    $zip->extractTo("/Applications/MAMP/htdocs/sites/default/files/");
    $zip->close();
    watchdog('Zip sucess','Unzipping worked!' . drupal_realpath($file->uri));
  } else {
    watchdog('Zip Failure','Unzipping failed for ' . drupal_realpath($file->uri));
  }
}

Included in this snipet are two different ways I attempted to open the zip file.

This first one works on my local MAMP server but would not work on a producitons server:

$res = $zip->open(drupal_realpath($file->uri));

This created a path like this:

/Applications/MAMP/htdocs/sites/default/files/Tumble arcade file_5.zip

and

$res = $zip->open($file->uri);

creates a path like this:

public://Tumble arcade file.zip

Now the first path is interpreted correctly by $zip->open and the file is unzipped correctly but with the second path, the zip open fails. I believe this is happening because php does not know how to handle the second path and needs something more like this:

http://localhost/sites/default/files/Tumble%20arcade%20file_5.zip

Am I on the right track and if so how would I get this other kind of path? If not, any idea what might instead be the problem?

1 Answer 1

2

There's a nice in-built function, file_create_url():

Creates a web-accessible URL for a stream to an external or local file.

e.g.

$url = file_create_url($file->uri);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.