Jump to content

Recommended Posts

I'm trying to write a script that reads files from a directory, and then displays the file contents to the screen depending on which file you click.  In the directory, there are regular files and then there are .gz files.  Obviously, to be able to display the .gz file contents to the screen I have to unzip it first. 

Unfortunately, I can't seem to get my script to unzip the .gz files (I'm using `gunzip filename`).  Every other command in the backtick operators (stuff like "cd" and "ls") executes except for the gunzip command.  I went into the shell and copy/pasted my command from the script and things worked out just fine.  So does PHP not recognize gzip/gunzip or something?

Any help would be greatly appreciated.

-Robert   
Link to comment
https://forums.phpfreaks.com/topic/33912-using-shell-commands-from-a-script/
Share on other sites

Which Unixy OS are you on? [tt]gunzip [/tt]may not be in your path. Try:

[code]
<?php echo `which gunzip`; ?>
[/code]

[b]Update:[/b] On second thought,[tt] which [/tt]may not be either. See what path the command line gives you and try using the full path.
Great; now we know that we're talking to gunzip. gunzip is telling us that it is reading stdin, which provided the "unexpected end of file." Make sure that your code is correct, your file exists, and that you're specifying the proper path.
Well, like I said, it works in the shell.  So I know that the file exists and that I'm specifying the right path.  As for the code, I'm doing this:

[code]$cmd = "cd ..; cd ..; cd dirA/dirB/dirC; gunzip filename";
`$cmd`;[/code]

I'm new to PHP, but as far as I can tell, that code should work (it doesn't throw errors at any rate).

-Robert
Yes, that should work. Did you use the cd's in the example I gave? The backticks will not give you a return value, so you need to use code (like I showed) that will. You also need to combine STDERR with STDOUT.

Try more troubleshooting tricks:

1. Instead of gunzip how about using pwd or ls to verify that you are where you think you are?
2. Maybe the cd's aren't getting through (permissions)? Perhaps you should use[tt] && [/tt]as a command separator instead of[tt] ;[/tt].
[quote]Obviously, to be able to display the .gz file contents to the screen I have to unzip it first[/quote]

Not so. tar has a -t switch which will display the contents of *tar.gz archives.

[code]
<?php
  $cmd = "../../dirA/dirB/dirC tar -t filename";
  $out = shell_exec("$cmd");
  echo "<pre>$out</pre>";
?>
[/code]
Yeah, I used "ls" previously to see if my script was working in the directory I told it to and it was.  Tried separating the commands with the ampersands, but I got the same result.

[quote author=effigy link=topic=122119.msg503440#msg503440 date=1168633400]
You also need to combine STDERR with STDOUT.
[/quote]

What do you mean?

[quote author=thorpe link=topic=122119.msg503455#msg503455 date=1168634472]
Not so. tar has a -t switch which will display the contents of *tar.gz archives.
[/quote]

I tried that and the shell gave me something about not being able to read.  Does it matter that the files are just ".gz" and not "tar.gz"?

Also, I've been using Google to help shed some light on PHP's file (de)compressing abilities and I keep running into talk about zlib.  Do I need to have that installed on the PHP server (I'm not sure, but I don't think it's installed)?

-Robert
[quote author=rabark link=topic=122119.msg503486#msg503486 date=1168636322]
[quote author=effigy link=topic=122119.msg503440#msg503440 date=1168633400]
You also need to combine STDERR with STDOUT.
[/quote]
What do you mean?
[/quote]

For testing purposes--and most likely production, for that matter--you need the see what kind of errors and return values you're getting. By default, you're not going to see STDERR, so you need to combine it with STDOUT as I did above:

[tt]gunzip filename 2>&1[/tt]
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.