flyingdutchman Posted February 15, 2014 Share Posted February 15, 2014 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 Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 15, 2014 Share Posted February 15, 2014 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. Quote Link to comment Share on other sites More sharing options...
.josh Posted February 15, 2014 Share Posted February 15, 2014 AFAIK there's no zipArchive method/arg that will automatically do that, but php does have chown chgrp and chmod functions that you could use after the extraction. Quote Link to comment Share on other sites More sharing options...
flyingdutchman Posted February 16, 2014 Author Share Posted February 16, 2014 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); } Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 16, 2014 Share Posted February 16, 2014 (edited) @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 February 16, 2014 by jazzman1 Quote Link to comment Share on other sites More sharing options...
flyingdutchman Posted February 17, 2014 Author Share Posted February 17, 2014 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 Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 17, 2014 Share Posted February 17, 2014 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. Quote Link to comment Share on other sites More sharing options...
flyingdutchman Posted February 17, 2014 Author Share Posted February 17, 2014 (edited) 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 ------------------------- <?phpfunction 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 February 17, 2014 by flyingdutchman Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 17, 2014 Share Posted February 17, 2014 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 } Quote Link to comment Share on other sites More sharing options...
flyingdutchman Posted February 19, 2014 Author Share Posted February 19, 2014 ..... zilch I have put the chmod line at the suggested place, Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 19, 2014 Share Posted February 19, 2014 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 Quote Link to comment Share on other sites More sharing options...
flyingdutchman Posted February 19, 2014 Author Share Posted February 19, 2014 (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 Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 19, 2014 Share Posted February 19, 2014 Why don't you tell us the whole story? I like to read them before to get in my bed Quote Link to comment Share on other sites More sharing options...
.josh Posted February 19, 2014 Share Posted February 19, 2014 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. Quote Link to comment Share on other sites More sharing options...
flyingdutchman Posted February 19, 2014 Author Share Posted February 19, 2014 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.