rabark Posted January 12, 2007 Share Posted January 12, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/33912-using-shell-commands-from-a-script/ Share on other sites More sharing options...
effigy Posted January 12, 2007 Share Posted January 12, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/33912-using-shell-commands-from-a-script/#findComment-159234 Share on other sites More sharing options...
rabark Posted January 12, 2007 Author Share Posted January 12, 2007 Path to gunzip is: usr/contrib/bin/gunzipTried using the full path in the both the script and the shell interface. Once again, it works just fine in shell but not from the script (script still manages to execute all of my other commands).-Robert Quote Link to comment https://forums.phpfreaks.com/topic/33912-using-shell-commands-from-a-script/#findComment-159296 Share on other sites More sharing options...
effigy Posted January 12, 2007 Share Posted January 12, 2007 After changing the filename, what does this give you?[code]<?php echo '<pre>', passthru('gunzip filename 2>&1', $return_val), '</pre>'; echo "<b>$return_val</b>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33912-using-shell-commands-from-a-script/#findComment-159321 Share on other sites More sharing options...
rabark Posted January 12, 2007 Author Share Posted January 12, 2007 It gives me: [code]gunzip: stdin: unexpected end of file 1[/code]-Robert Quote Link to comment https://forums.phpfreaks.com/topic/33912-using-shell-commands-from-a-script/#findComment-159342 Share on other sites More sharing options...
effigy Posted January 12, 2007 Share Posted January 12, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/33912-using-shell-commands-from-a-script/#findComment-159381 Share on other sites More sharing options...
rabark Posted January 12, 2007 Author Share Posted January 12, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/33912-using-shell-commands-from-a-script/#findComment-159395 Share on other sites More sharing options...
effigy Posted January 12, 2007 Share Posted January 12, 2007 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 Link to comment https://forums.phpfreaks.com/topic/33912-using-shell-commands-from-a-script/#findComment-159407 Share on other sites More sharing options...
trq Posted January 12, 2007 Share Posted January 12, 2007 [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] Quote Link to comment https://forums.phpfreaks.com/topic/33912-using-shell-commands-from-a-script/#findComment-159422 Share on other sites More sharing options...
rabark Posted January 12, 2007 Author Share Posted January 12, 2007 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 Link to comment https://forums.phpfreaks.com/topic/33912-using-shell-commands-from-a-script/#findComment-159452 Share on other sites More sharing options...
effigy Posted January 12, 2007 Share Posted January 12, 2007 [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] Quote Link to comment https://forums.phpfreaks.com/topic/33912-using-shell-commands-from-a-script/#findComment-159459 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.