Jump to content

unzip file with extractTo results in owner problem


flyingdutchman

Recommended Posts

Hi everybody,

 

I am writing a small php-file for unzipping zip-files on a remote server, using following code:

 

$zip = new ZipArchive;
$res = $zip->open($filename);
 
if ($res === TRUE) {
$zip->extractTo('./');
$zip->close();
 
The php unzipfile is uploaded to the same dir as the zipfile. Opening the phpfile in a browser enables me to start off the unzip. 
 
Unzipping works just fine, also deleting afterwards of the original zip-file and unzip-php.
Problem however is the owner of the newly created files/directories >> everything set to  33 33
Result: I cannot change, delete, rename etc any file / folder anymore (have no root-access on the server)
 
I have googled the issue for 3 hours now, but no workable solutions have come up :(
Is it possible to:
 
1/ combine the extractTo function with chown in some way?
2/ perform some kind of login before extracting the files?
 
Any hint or suggestion more than welcome!
Arthur
 
ps: for those interested, I can send the complete code of the unzip.php
 
 

 

Link to comment
Share on other sites

There is something wrong in your explanation about file/directory permissions. If php engine has a permit to unzip and create new files in this particular directory it probably has rights to rename/delete its files. Run a second script by php to scan/rename/delete or whatever you want to do with those files.

Link to comment
Share on other sites

Thnx guys for replying,

 

@Jazzman; same thoughts here: if I can unzip and create files/folders, I should be able to manipulate them ...

@ Josh: ... so I tried looping the zip_entries and set owners while file being created:

 

Result .... nothing. Every file and folder is created with owner and group "33 33" ....

Is there something wrong with the way I try setting the ownership? >> see code below

 

// ------ Loop entries -----------

 

while($zip_entry=zip_read($zip)) {
 
$zdir = dirname(zip_entry_name($zip_entry));
$zname = zip_entry_name($zip_entry);
  
$zip_fs = zip_entry_filesize($zip_entry);
if(empty($zip_fs)) continue;
 
$zz = zip_entry_read($zip_entry,$zip_fs);
$z = fopen($zname,"w");
 
chmod($z, 0755);
chown($z, $my_usr); 
chgrp($z, $my_usr);
 
fwrite($z,$zz);    
fclose($z);
 
....
 
zip_entry_close($zip_entry);
Link to comment
Share on other sites

 

@Jazzman; same thoughts here: if I can unzip and create files/folders, I should be able to manipulate them ...

 

No you! Let "php" to do this. 

 

PS: Can you show us some result of:

 

ls -la /directory/file_name

Edited by jazzman1
Link to comment
Share on other sites

Hi Jazzman, thanks for sticking with me.

 

These are the settings for the zip-file and the directory in which it is residing:

 

Directory:
 
in filezilla: flcdmpe (0777)  10016 1xxx
in commandprompt: drwxrwxrwx 9 arttuur psacln  4096 Feb 17 07:19 sp_unzip
 
Zipfile:
 
in filezilla: adfrw (0777)  10016 1xxx
in commandprompt: -rwxrwxrwx 1 arttuur psacln 969 Feb 17 08:46 topdir7.zip
 
How do let php take care of things??
Gr.
Arthur
Link to comment
Share on other sites

I don't see any permissions directory issue here, nor the files. Let me see what permissions have the unziped file? Also, post the script that unzips the archives, maybe php sets some specific permissions into new files.

 

 

How do let php take care of things??

 

You need to create(write) a php script.

Link to comment
Share on other sites

Hi Jazzman (u by any chance play the saxophone like me?)

 

Permission to unzipped items:

 

Directory:
in filezilla: adfrw (0755)   33 33
in commandprompt: drwxr-xr-x 1 www-data www-data 4096 Feb 16 13:42 a1 (dir)
 
File:
in filezilla: flcdmpe (0644) 33 33
in commandprompt: -rw-r--r-- 1 www-data www-data 8 Feb 16 13:42 aaa.txt
 
I am using a php-script (see below) that is ftp'd to the same directory on server where the zip-file lies.
Opening the file (in this case) will trigger the unzip. Normally I have an little interface for selecting the zipfile + other options (delete php-unzipper)
 
Since I cannot detect an upload- / attach button, I'll hand you the code as text.
The code was written by a certain Yarms. You can find it at  http://php.net/manual/ro/ref.zip.php (needs a bit of scrolling down)
 
Gr.
Arthur  
 
-------------------------
 
<?php

function unzip($file){

    
$zip=zip_open(realpath(".")."/".$file);
    if(!
$zip) {return("Unable to proccess file '{$file}'");}

    
$e='';

    while(
$zip_entry=zip_read($zip)) {
       
$zdir=dirname(zip_entry_name($zip_entry));
       
$zname=zip_entry_name($zip_entry);

       if(!
zip_entry_open($zip,$zip_entry,"r")) {$e.="Unable to proccess file '{$zname}'";continue;}
       if(!
is_dir($zdir)) mkdirr($zdir,0777);

       
#print "{$zdir} | {$zname} \n";

       
$zip_fs=zip_entry_filesize($zip_entry);
       if(empty(
$zip_fs)) continue;

       
$zz=zip_entry_read($zip_entry,$zip_fs);

       
$z=fopen($zname,"w");
       
fwrite($z,$zz);
       
fclose($z);
       
zip_entry_close($zip_entry);

    } 
    
zip_close($zip);

    return(
$e);


function 
mkdirr($pn,$mode=null) {

  if(
is_dir($pn)||empty($pn)) return true;
  
$pn=str_replace(array('/'''),DIRECTORY_SEPARATOR,$pn);

  if(
is_file($pn)) {trigger_error('mkdirr() File exists'E_USER_WARNING);return false;}

  
$next_pathname=substr($pn,0,strrpos($pn,DIRECTORY_SEPARATOR));
  if(
mkdirr($next_pathname,$mode)) {if(!file_exists($pn)) {return mkdir($pn,$mode);} }
  return 
false;
}


unzip("test.zip");

?>
Edited by flyingdutchman
Link to comment
Share on other sites

Add chmod($zname, 0755) or chmod($zname, 0775) or chmod($zname, 0777) depends on your wishes.

 while($zip_entry=zip_read($zip)) {
       $zdir=dirname(zip_entry_name($zip_entry));
       $zname=zip_entry_name($zip_entry);

       if(!zip_entry_open($zip,$zip_entry,"r")) {$e.="Unable to proccess file '{$zname}'";continue;}
       if(!is_dir($zdir)) mkdirr($zdir,0777);

       #print "{$zdir} | {$zname} \n";

       $zip_fs=zip_entry_filesize($zip_entry);
       if(empty($zip_fs)) continue;

       $zz=zip_entry_read($zip_entry,$zip_fs);

       $z=fopen($zname,"w");
       fwrite($z,$zz);
       fclose($z);
       zip_entry_close($zip_entry);

      chmod($zname, 0755); // here
    } 
Link to comment
Share on other sites

The file's creator and owner is  "www-data" , maybe you're on Debian or Debian based distro. By default the file's permission when the file being created is 0644 (-rw-r--r--), which means nobody unless the owner won't have permission to modify/delete the file. By setting chmod to 0775 (for groups) or 0777 (for others) will it give full rights to this file. 

 

@off: I was a keyboard player ;)

Link to comment
Share on other sites

(loud thinking) ..  this is probably never gonna work since opening the unzip.php by browser means that the script is executed as root.

It will never allow a anonymous user to change file-permissions or ownership. It surprises me that the unzip, including creation of files works at all!

 

Maybe there is a way to "open/execute" the unzip.php in a ftp commandline-session. In which case I would be logged in to the server ....

I have read about php.exe, but that is not recognized as a valid command in my dos-prompt ....

 

Maybe I better get used to the idea that uploading a cms-based website takes me upto 30 minutes ....

Arthur

Link to comment
Share on other sites

Maybe there is a way to "open/execute" the unzip.php in a ftp commandline-session. In which case I would be logged in to the server ....

You would need to logon via SSH (not ftp) to execute php on the command line and if you get that far, you may as well just skip the php file and unzip the file yourself via command line.

 

I have read about php.exe, but that is not recognized as a valid command in my dos-prompt ....

That's because php.exe isn't a core program that comes on computers. It's a program you download and install like any other program. It just so happens that most hosting providers out there install it on their server because it's a popular choice for making web based scripts.

 

If you want to poke at it on your own computer, I suggest you try WAMP or XAMPP.

Link to comment
Share on other sites

Hi folks,

 

@Jazzman: the nutshell-version:

 

I develop websites with Drupal. Uploading them is a P.I.T.A. because of the fact that every file (3000+) is tossed over (and handshaked I guess) seperately.

Solution: Zip the project, upload as one file (10sec instead of 30 min) and unwrap at the other side.

The script I showed you looked promising uptill the moment I noticed the minor access-problem I described...

 

@ Josh: 

Actually I develop every website on my local XAMPP server. Tomorrow I'll give it a shot with SSH (here in Europe it is past 21:30, time for a little nightcap )

It'll be new territory once again. Keep you posted on my progress ...

 

Greetz,

Arthur

Link to comment
Share on other sites

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.