Click to See Complete Forum and Search --> : unzip functions


Sheldon
11-26-2005, 09:59 PM
I am trying to write an unzip function, Anyone have any ideas why i get this error?

Fatal error: Call to undefined function: *copy() in /home/sheldon/public_html/unzip.php on line 5



<?

function unzip($zip_file, $src_dir, $extract_dir)
{
*copy($src_dir $zip_file, $extract_dir $zip_file);
*chdir($extract_dir);
*shell_exec("unzip $zip_file");
}


unzip('testzip.zip','./','./');
?>


every thing is in the root dir.


Thanks

chazzy
11-26-2005, 10:14 PM
i think this works...
<?

function unzip($zip_file, $src_dir, $extract_dir)
{
*copy($src_dir.$zip_file, $extract_dir.$zip_file);
*chdir($extract_dir);
*shell_exec("unzip $zip_file");
}


unzip('testzip.zip','./','./');
?>
still get errors?

Sheldon
11-26-2005, 10:27 PM
I get no errors but it didnt unzip the file

Sheldon
11-26-2005, 10:36 PM
i get was not unpacked every time



<?

function unzip($zip_file, $src_dir, $extract_dir)
{
copy($src_dir.$zip_file, $extract_dir.$zip_file);
chdir($extract_dir);
shell_exec("unzip $zip_file");
}


if(unzip('forum.zip','./','./')){

echo($zip_file .'unpacked successfully');

}else{

echo($zip_file .'was not unpacked');

}



?>

chazzy
11-26-2005, 10:47 PM
does the php process user have proper write access to the directory and the directory to write to exists?

Sheldon
11-26-2005, 10:54 PM
mmm, good point!! i have set the .zip package to 777 for the test but didnt think the dir is set to have write access. Ill try that and post back.

Sheldon
11-26-2005, 10:58 PM
nope, the folder and all the files have full 777 perms. More reseach is needed i think.

chazzy
11-26-2005, 11:05 PM
what happens if you precede it with mkdir($extract_dir.$zip_file,0777)? probably won't do anythin but hey what the heck.

other than that, try changing the shell_exec to "unzip $extract_dir.$zip_file"?

you could also try putting the path to the unzip command (what os is this btw, i don't see it as a standard linux command...)

Sheldon
11-26-2005, 11:36 PM
Linux


<?

function unzip($zip_file, $src_dir, $extract_dir)
{
mkdir($extract_dir.$zip_file,0777);
copy($src_dir.$zip_file, $extract_dir.$zip_file);
chdir($extract_dir);
shell_exec("unzip $zip_file");
}


if(unzip('forum.zip','/zips/','/zips/')){

echo($zip_file .'unpacked successfully');

}else{

echo($zip_file .'was not unpacked');

}



?>

SpectreReturns
11-26-2005, 11:38 PM
http://php.net/zip

Why not just use the zlib?

sitehatchery
11-27-2005, 12:13 AM
PCLZip functions worked for me. Check out http://www.phpconcept.net/pclzip/index.en.php. Very easy to use.

Sheldon
11-27-2005, 12:40 AM
Thanks, ill have a look :)

bokeh
11-27-2005, 04:39 AM
Sheldon, I'm surprised no one has pointed this out but your function will never raise the if conditional because it doesn't return anything. In the following example the if statement always returns failure even though what happened in the function was true.<?php // prints "IF statement failure"

function GoGoGadget()
{
(1==1); // this is true
}

if(GoGoGadget()){
echo('IF statement successful');
}else{
echo('IF statement failure');
}

?>This is how it should be done:<?php // prints "IF statement successful"

function GoGoGadget()
{
return(1==1); // this is true
}

if(GoGoGadget()){
echo('IF statement successful');
}else{
echo('IF statement failure');
}

?>In the case of your function you could do something like the following or similar:function unzip($zip_file, $src_dir, $extract_dir)
{
return((mkdir($extract_dir.$zip_file,0777))
and
(copy($src_dir.$zip_file, $extract_dir.$zip_file))
and
(chdir($extract_dir))
and
(shell_exec("unzip $zip_file")));
}Or:function unzip($zip_file, $src_dir, $extract_dir)
{
if(!mkdir($extract_dir.$zip_file,0777)) return false;
if(!copy($src_dir.$zip_file, $extract_dir.$zip_file)) return false;
if(!chdir($extract_dir)) return false;
return(shell_exec("unzip $zip_file"));
} In fact there are numerous ways to do this. Once you do a return nothing further in the function will run.

Sheldon
11-27-2005, 02:22 PM
Thanks Bokeh, I will try something out and get back to you.

Sheldon
11-27-2005, 02:40 PM
ok, i made this out of it

in the root dir is a .zip file called testzip.zip

but it wont work for me.

to try go to
http://www.slweb.co.nz/unzip.php

<?php

include('common/header.php');
//include('common/auth.php');


function unzip($zip_file, $src_dir, $extract_dir)
{
if(!mkdir($extract_dir.$zip_file,0777)) return false;
if(!copy($src_dir.$zip_file, $extract_dir.$zip_file)) return false;
if(!chdir($extract_dir)) return false;
return(shell_exec("unzip $zip_file"));
}


if($_GET['action'] == "submit"){ //form is submitted

if($file = $_POST['zipped'] && $location = $_POST['location']){ //find the name * location of the file

if(unzip($file , $location , '/zips/')){
print 'File was unpacked'; //works!!
}else{
print '**** sheldon it didnt work'; //no go :(
}
}else{ //file cant be found
print 'The file you are trying to unzip cannot be found';
}

echo '<p>';
echo $location; //the result of lines is prints the location and 1 ?? not the filename?
echo $file;
echo '</p>';

}else{ //form has not been submitted, print the form

?>

<form action="unzip.php?action=submit" method="post" enctype="multipart/form-data">
<fieldset class="fieldset">
<legend class="header">Un-zipper</legend>

<label for="location">Type location on server:</label><input type="text" class="form_element" name="location" id="location"><br><br>

<label for="upload_file">upload file name:</label><input type="text" class="form_element" name="zipped" id="zipped"><br><br>

<span class="center"><input type="submit" value="Un-zip File" class="form_element"></span>
</fieldset>
</form>


<?php } /* End the orignal if statement*/
include('common/footer.php');
?>

chazzy
11-27-2005, 04:01 PM
It's saying your mkdir didn't work.

Warning: mkdir(/zips/1): No such file or directory in /home/sheldon/public_html/unzip.php on line 9
**** sheldon it didnt work

/1

for input, i put in '/' and 'testzip.zip'

however, i feel that your logic is off

if($file = $_POST['zipped'] && $location = $_POST['location'])

so what you're saying is if you're able to set the file to be post['zipped'] and the location to be post['location'] then do it? otherwise don't do it?

maybe...

$file = $_POST['zipped'];
$location = $_POST['location'];
if(file_exists($location.$file)){
//try
}
else{
//the file doesn't exist...
}


edit: and that explains why it says the file's 1...your logic's off ;)

Sheldon
11-27-2005, 04:15 PM
mmm, Thanks Chazzy, I made that change and it says the file /testzip.zip can not be found, yet it is there.

http://www.slweb.co.nz/unzip.php


Thanks

chazzy
11-27-2005, 05:05 PM
of course it doesn't exist...

shrug.

hmm
what's the code look like now btw?

Sheldon
11-27-2005, 05:07 PM
<?php

include('common/header.php');
//include('common/auth.php');


function unzip($zip_file, $src_dir, $extract_dir)
{
if(!mkdir($extract_dir.$zip_file,0777)) return false;
if(!copy($src_dir.$zip_file, $extract_dir.$zip_file)) return false;
if(!chdir($extract_dir)) return false;
return(shell_exec("unzip $zip_file"));
}


if($_GET['action'] == "submit"){ //form is submitted

$file = $_POST['zipped'];
$location = $_POST['location'];
if(file_exists($location.$file)){

if(unzip($file , $location , '/zips/')){
print 'File was unpacked'; //works!!
}else{
print '**** sheldon it didnt work'; //no go :(
}
}else{ //file cant be found
print 'The file you are trying to unzip cannot be found';
}

echo '<p>';
echo $location; //the result of lines is prints the location and 1 ?? not the filename?
echo $file;
echo '</p>';

}else{ //form has not been submitted, print the form

?>

<form action="unzip.php?action=submit" method="post" enctype="multipart/form-data">
<fieldset class="fieldset">
<legend class="header">Un-zipper</legend>

<label for="location">Type location on server:</label><input type="text" class="form_element" name="location" id="location"><br><br>

<label for="upload_file">upload file name:</label><input type="text" class="form_element" name="zipped" id="zipped"><br><br>

<span class="center"><input type="submit" value="Un-zip File" class="form_element"></span>
</fieldset>
</form>


<?php } /* End the orignal if statement*/
include('common/footer.php');
?>

chazzy
11-27-2005, 05:28 PM
this is all i could find on it returning false negatives.

http://us3.php.net/manual/en/features.safe-mode.php

edit: and if you don't feel like changing safe mode settings, you can switch from file_exists to is_readable

Sheldon
11-27-2005, 05:38 PM
with replace file_exists and is_readable

i now get

Warning: mkdir(/zips/testzip.zip): No such file or directory in /home/sheldon/public_html/unzip.php on line 9

where line 9 is
if(!mkdir($extract_dir.$zip_file,0777)) return false;

chazzy
11-27-2005, 07:08 PM
I still feel like you have a permission issue then, somewhere, even though it was already noted that there i no permission issue. does the /zips directory exist?

Sheldon
11-27-2005, 07:23 PM
yep and it's perms a set to 0777

http://www.slweb.co.nz/zips/

I thin kmaybe because its trying to make the dir called testzip.zip when really do we want copy to dir /zipz/ ?

chazzy
11-27-2005, 07:56 PM
wait...

your httpd home is /home/sheldon/public_html/ right? is this a shared environment? are you trying to make this in /home/sheldon/public_html/zips/ ?

Sheldon
11-27-2005, 08:12 PM
yes
to all of that

chazzy
11-27-2005, 08:44 PM
a call like this should work...


unzip('forum.zip','/home/sheldon/public_html/zips/','/home/sheldon/public_html/zips/');


you might need a config variable with the home directory.

Sheldon
11-27-2005, 09:04 PM
i still get this error,

Warning: mkdir(/home/sheldon/public_html/zips/forum.zip): File exists in /home/sheldon/public_html/unzip.php on line 9
**** sheldon it didnt work
testzip.zip

with this code now

<?php

include('common/header.php');
//include('common/auth.php');


function unzip($zip_file, $src_dir, $extract_dir)
{
if(!mkdir($extract_dir.$zip_file,0777)) return false;
if(!copy($src_dir.$zip_file, $extract_dir.$zip_file)) return false;
if(!chdir($extract_dir)) return false;
return(shell_exec("unzip $zip_file"));
}


if($_GET['action'] == "submit"){ //form is submitted

$file = $_POST['zipped'];
$location = $_POST['location'];
if(is_readable($location.$file)){

if(unzip('forum.zip','/home/sheldon/public_html/'.$location ,'/home/sheldon/public_html/zips/')){
print 'File was unpacked'; //works!!
}else{
print '**** sheldon it didnt work'; //no go :(
}
}else{ //file cant be found
print 'The file you are trying to unzip cannot be found';
}

echo '<p>';
echo $location; //the result of lines is prints the location and 1 ?? not the filename?
echo $file;
echo '</p>';

}else{ //form has not been submitted, print the form

?>

<form action="unzip.php?action=submit" method="post" enctype="multipart/form-data">
<fieldset class="fieldset">
<legend class="header">Un-zipper</legend>

<label for="location">Type location on server:</label><input type="text" class="form_element" name="location" id="location"><br><br>

<label for="upload_file">upload file name:</label><input type="text" class="form_element" name="zipped" id="zipped"><br><br>

<span class="center"><input type="submit" value="Un-zip File" class="form_element"></span>
</fieldset>
</form>


<?php } /* End the orignal if statement*/
include('common/footer.php');
?>




you might need a config variable with the home directory.

please explain more chazzy

chazzy
11-27-2005, 09:21 PM
perfect.
now the error's because there's a file with the same name. it found your file finally.

just a config variable

$homedir = '/home/sheldon/public_html';
function unzip($zip_file, $src_dir, $extract_dir)
{
if(!mkdir($homedir.$extract_dir.$zip_file,0777)) return false;
if(!copy($homedir.$src_dir.$zip_file, $homedir.$extract_dir.$zip_file)) return false;
if(!chdir($homedir.$extract_dir)) return false;
return(shell_exec("unzip $homedir.$src_dir.$zip_file"));
}


how do you want to put the files? in a directory named "files" if the file is named "files.zip"?

SpectreReturns
11-27-2005, 09:25 PM
mkdir creates a directory, you're giving it a filename and a chmod.

chazzy
11-27-2005, 09:27 PM
linux doesn't like directories with the names of a file that exists in the parent directory, last i remember. it gets confused using vi, chown, chdir, cd, etc.

Sheldon
11-28-2005, 05:35 PM
perfect.
now the error's because there's a file with the same name. it found your file finally.

just a config variable

$homedir = '/home/sheldon/public_html';
function unzip($zip_file, $src_dir, $extract_dir)
{
if(!mkdir($homedir.$extract_dir.$zip_file,0777)) return false;
if(!copy($homedir.$src_dir.$zip_file, $homedir.$extract_dir.$zip_file)) return false;
if(!chdir($homedir.$extract_dir)) return false;
return(shell_exec("unzip $homedir.$src_dir.$zip_file"));
}


how do you want to put the files? in a directory named "files" if the file is named "files.zip"?


Do i use this code?

I would like to unzip them in the /zips/ folder?

chazzy
11-28-2005, 10:59 PM
but see that's the problem.
you're in a shared environment. what you see as / is not the root directory, it's /home/sheldon, usually, so you need to have it unzip to /home/sheldon/zips/
you can use the code, yes, but you'll need to tweak it to look for that directory.

bokeh
11-29-2005, 03:01 AM
$homedir = '/home/sheldon/public_html'; That looks like a file your specifying not a directory. Also I believe you are better off using $_SERVER['DOCUMENT_ROOT'] rather than the absolute path to server root. Using the latter will mean your scripts will break if you move from one server to another.

chazzy
11-29-2005, 08:08 AM
$homedir = '/home/sheldon/public_html'; That looks like a file your specifying not a directory. Also I believe you are better off using $_SERVER['DOCUMENT_ROOT'] rather than the absolute path to server root. Using the latter will mean your scripts will break if you move from one server to another.

except that his scripts won't be residing in /home/sheldon/public_html, which is a directory, as I just pointed out, he most likely wants to put them in /home/sheldon/zips/ which you would not be able to dynamically generate

bokeh
11-29-2005, 08:30 AM
My dodgy eyesight mistook the underscore for a dot.

chazzy
11-29-2005, 08:35 AM
My dodgy eyesight mistook the underscore for a dot.

do you see someway the /${user_home}/zips/ could be automatically generated though? I've seen the following directory structures for when it comes to public_html:


/var/home/chazzy/docs/htdocs/public_html/
-> where zips would be /var/home/chazzy/zips/

/home/chazzy/public_html/
-> /home/chazzy/zips/

/usr/home/chazzy/httpd/htdocs/
-> /usr/home/chazzy/zips/


then again, it all depends on if he even has access to those higher level directories...

bokeh
11-29-2005, 08:46 AM
// assumes "zips" is the sibling of document root (i.e. they share the same parent directory)
$extract_dir = $_SERVER['DOCUMENT_ROOT'].'/../zips/';

chazzy
11-29-2005, 12:20 PM
// assumes "zips" is the sibling of document root (i.e. they share the same parent directory)
$extract_dir = $_SERVER['DOCUMENT_ROOT'].'/../zips/';

is that going to be the best bet? in two of the examples i showed, /zips/ is not sibling to document_root...

bokeh
11-29-2005, 02:00 PM
in two of the examples i showed, /zips/ is not sibling to document_root...My answer relates to Sheldon's post. I don't see the connection between your example with three seperate directories and the original question. Could you explain what the connection is? Personally I always relate my include paths to document root as this makes things easy no matter what server or directory structure the files are located within.

Sheldon
11-29-2005, 02:27 PM
Where am I setting $home_dir, or do i remove it, as $extract_dir & $location_dir.$location is the full addresses to the file to be unzipped?


<?php

include('common/header.php');
//include('common/auth.php');


function unzip($zip_file, $src_dir, $extract_dir)
{
if(!mkdir($homedir.$extract_dir.$zip_file,0777)) return false;
if(!copy($homedir.$src_dir.$zip_file, $homedir.$extract_dir.$zip_file)) return false;
if(!chdir($homedir.$extract_dir)) return false;
return(shell_exec("unzip $homedir.$src_dir.$zip_file"));
}


if($_GET['action'] == "submit"){ //form is submitted

$extract_dir = $_SERVER['DOCUMENT_ROOT'].'/../zips/';
$location_dir = $_SERVER['DOCUMENT_ROOT'].'/';
$file = $_POST['zipped'];
$location = $_POST['location'];
if(is_readable($location.$file)){

if(unzip($file,$location_dir.$location ,$extract_dir)){
print 'File was unpacked'; //works!!
}else{
print '**** sheldon it didnt work'; //no go :(
}
}else{ //file cant be found
print 'The file you are trying to unzip cannot be found';
}

echo '<p>';
echo $location_dir;
echo $location;
echo $file;
echo '</p>';

}else{ //form has not been submitted, print the form

?>

<form action="unzip.php?action=submit" method="post" enctype="multipart/form-data">
<fieldset class="fieldset">
<legend class="header">Un-zipper</legend>

<label for="location">Type location on server:</label><input type="text" class="form_element" name="location" id="location"><br><br>

<label for="upload_file">upload file name:</label><input type="text" class="form_element" name="zipped" id="zipped"><br><br>

<span class="center"><input type="submit" value="Un-zip File" class="form_element"></span>
</fieldset>
</form>


<?php } /* End the orignal if statement*/
include('common/footer.php');
?>


Thanks every one so far for all of your help.

chazzy
11-29-2005, 03:57 PM
$home_dir should be /home/sheldon

extract dir should be the relative path from there.
src dir should be the relative path from there as well.

does the above code still not work?


My answer relates to Sheldon's post. I don't see the connection between your example with three seperate directories and the original question. Could you explain what the connection is? Personally I always relate my include paths to document root as this makes things easy no matter what server or directory structure the files are located within.

actually, it doesn't always work that way. if you want to move your files outside of your http server's path (so you can't access them directly), which is what sheldon is looking for, it could be several directories away and not always sibling.

and since we're using a shell function, it doesn't need to reside anywhere relative to the http directory.

bokeh
11-29-2005, 04:12 PM
$home_dir should be /home/sheldonActually $home_dir should be deleted! It doesn't exist anywhere else in the script and certainly not in the function.http server's path (so you can't access them directly)My method has nothing to do with an http path. It is a server file path. I tested the method and there is no problem going back up the server path and then down another branch. The only reason this wouldn't work is if permissions were insufficient or there were an open_base_directory restriction in place but those two items would affect all methods. By the way in sheldon's case 'zips' is the sibling of the root directory.

chazzy
11-29-2005, 04:38 PM
Actually $home_dir should be deleted!
irrelevant, as he asked for the definition. at this point i believe he realizes that it's not used in his script
It doesn't exist anywhere else in the script and certainly not in the function.My method has nothing to do with an http path. It is a server file path. I tested the method and there is no problem going back up the server path and then down another branch. The only reason this wouldn't work is if permissions were insufficient or there were an open_base_directory restriction in place but those two items would affect all methods. By the way in sheldon's case 'zips' is the sibling of the root directory.


Using the latter will mean your scripts will break if you move from one server to another.


earlier you brought up this point, so what do we do when he's forced to use another directory structure? either way his script will break when it's not sibling to ${http}
I never said that there was a problem moving to a sibling. My whole point, this entire time, has been that if it's not a sibling, you can't use DOCUMENT_ROOT to guarantee finding the directory. as i mentioned above with the three examples, those are all different ways i've seen directories setup for users.

bokeh
11-29-2005, 05:15 PM
irrelevant, as he asked for the definition. at this point i believe he realizes that it's not used in his scriptIf the script and function refer to variables that don't exist either globally or withˇn the function I don't see that as irrelevant, I see it as a major problem.earlier you brought up this point, so what do we do when he's forced to use another directory structure?Every directory structure can refer to document root and if the change in directory structure broke my method it would also break every other method. The point with using document root is that it is something that doesn't change from server to server. They all have a document root, but they don't all have a directory called sheldon. not sibling to ${http}I don't see what you are getting at here. It has nothing to do with an http path.
My whole point, this entire time, has been that if it's not a sibling, you can't use DOCUMENT_ROOT to guarantee finding the directory.Any thoughtless change to the directory structure would have the same effect. above with the three examplesHow could any server path reference 3 directories at the same time. Are you looking for some kind of magic trick. Obviously I chose the one example that was relevant to the post.

chazzy
11-30-2005, 09:12 AM
If the script and function refer to variables that don't exist either globally or withˇn the function I don't see that as irrelevant, I see it as a major problem.
I refer to variables that aren't defined in scripts all the time, when trying to figure out problems. What's ${ORA_HOME} when it's not defined in any context.

Every directory structure can refer to document root and if the change in directory structure broke my method it would also break every other method. The point with using document root is that it is something that doesn't change from server to server. They all have a document root, but they don't all have a directory called sheldon.
and what I still don't think you're understanding is that not every directory for housing your "public_html" content will be a child to "/home/username". Using document_root to dictate where the zip files go will not always be accurate since he seems to want them to be outside his "public_html" directory.

I don't see what you are getting at here. It has nothing to do with an http path.
${http} is a term we use where I work to refer to the path of all htdocs in apache or WebLogic.

Any thoughtless change to the directory structure would have the same effect.
using document_root doesn't work if you put the script in a different subdirectory.

How could any server path reference 3 directories at the same time. Are you looking for some kind of magic trick. Obviously I chose the one example that was relevant to the post.
what are you talking about here? my examples were three separate servers.

Honestly, this is becoming a flame war. If you would like to continue this discussion, I will happily give you my email address. None of what we are arguing about is relevant to this post any longer, unfortunately, and it appears to be my fault.

bokeh
11-30-2005, 10:35 AM
Honestly, this is becoming a flame war.Chazzy, you are becoming very boring! It would seem you have made it your personal mission to try to discredit anything I post. If you go back through this post and read your replies again you will see everything has been directed towards me and nothing has been any help towards solving the problem. You write your replies as if you do this professionally and then ramble on about it being irrelevant if variables are missing and as well, because of your inability to understand my posts you try to discredit them. I test all my posts and know them to work. If I have not tested a piece of code I state this when I post it. Lastly it is much easier to be an art critic than an artist. You it would seem are the former.