Shebang #! unrecognized in Ubuntu, File created in Windows7 | Troubleshoot encoding - php

Basically, the file was broken some where along the way from Windows7 to Ubuntu.
How can I look at a binary representation of the file to see what happened?

PHP command line script still have to have the <?php opener in them.
#!/usr/bin/php
echo "hi mom!\n";
will not work, it has to be
#!/usr/bin/php
<?php
echo "hi mom!\n";
This is because there's no such thing as a "php script". There are only various text files that have PHP code blocks embedded within them. Even in CLI mode, PHP expects/requires to see at least one <?php block. Otherwise the interpreter won't kick in and won't see any of the code, even though you've stated it's a PHP script with the shebang.
PHP cli mode is basically a hacked-in afterthought. PHP started out as a server-side CGI script parser and has not fundamentally changed from that mode.

Did you run with a ./?
IE:
./myscript.php

Try opening it with vi(m) and you'll see the problem. It's a bad intepreter (^M) at the end of each line. Try converting it (fromdos or dos2unix), this shoul'd fix the problem ;-)

My guess is that the files created on Windows have a BOM that is confusing matters.

When using Notepad++ on a Windows machine, one can change the EOL character from Windows to UNIX by going to
Edit > EOL Conversion > UNIX format
Double-check Notepad++'s status bar at the bottom-right to confirm your selection.
After saving and running from the command line, you should find that the PHP interpreter directive is now properly recognized.

Related

Debian - /usr/bin/env: 'php\r': No such file or directory

So go straight to the problem, when I run ./yii seems I got that error from Debian:stretch that I ran from Docker.
However when I run /usr/bin/env php -v I got the correct output and there's no problem on it.
Seems there's a problem on new line being translated as string and I have no idea how to fix it.
Sorry if my English a bit messy and thanks in advance.
Just note:
I've been trying to edit that file using nano within debian but it's useless. I'm getting the same error.
I've check php file within /usr/bin/php and it's exist both php and php7.1
I can run php -v without problem as well
You should convert the file with UNIX new line convention.
You have a DOS file, which has the extra \r character before \n, which is interpreted as a character in the command. So system will check the program php\r and not php, and so it fails.
tr -d '\15' < original_file > converted_file
should do the work (StackOverflow has many other methods and tricks)

Using popen() to upload ardiuno sketch

I am trying to use PHP to compile and upload an Arduino sketch through the command line. Right now a user uploads an ino or pde file through a form and it is transferred to a directory for later use. Using the uploaded file's location as a variable, I would like PHP to run the command line version of Ardunio to compile and upload it.
After an inital try with using exec() and system(), I switched to popen(). Running the following code I can get Arduino to open then it closes without uploading the sketch:
pclose(popen('"C:\Program Files\Arduino\arduino.exe" --port COM3 --upload "C:\sketches\uploads\cube\a\a.ino"));
Running that code and its variations through the Windows Command Line works so I know the input string is not the issue. Also, looking at the Windows Task Manager shows it opening for a second or so then closing. Could someone point me in the right direction?
For popen (or any of the other process functions) to work correctly on Windows you need to escape backslashes like this:
pclose(popen('"C:\\Program Files\\Arduino\\arduino.exe" --port COM3 --upload "C:\\sketches\\uploads\\cube\\a\\a.ino"'));
alternatively try replacing the backslashes with forward slashes. The following should also work on recent versions of Windows:
pclose(popen('"C:/Program Files/Arduino/arduino.exe" --port COM3 --upload "C:/sketches/uploads/cube/a/a.ino"'));
(Your code snippet was also missing a trailing single quote, but I suspect that's a typo.)

PHP exec giving error while the command runs successfully in bat file

I am running a local WAMP on my Windows 7 with a PHP script that executes a windows command as follows:
`exec('"%CD%\files_for_redistribution\ppt2html5.exe" /i:"%CD%\test.ppt" /o:"%CD%\output.html" /title:title /desc:description /author:author /keywords:keywords',$output,$error);`
The command when run from a batch file does the job well but when run from PHP script, gives an error:Presentation opening error: PowerPoint could not open the file.
The intention of the command is to convert PowerPoint to HTML using a third party software called ppt2html5.exe where test.ppt has to be converted to output.html.
I have found lot of blogs discussing about exec function not working properly but nothing really helped me to deal with this error as it runs the command but cannot open the file.
It would be great if somebody could help me with this.
Check if safe mode is on, because that activates escapeshellcmd and some characters are escaped.
Assuming that the string that you are passing to exec(), including percentage signs, routes and parameters are right, your problem may be related to permission of files and user executing apache + php, check that.
Fixed by adding a folder named Desktop inside C:\Windows\System32\config\systemprofile.
Source:http://www.sitepoint.com/forums/showthread.php?956457-Windows-2008-PHP-new-COM%28powerpoint-application%29

Lame encoder command in php shell_exec doesn't work via http server

I have a problem with running lame encoder via shell_exec script in php.
And the broke my problem to just this script and noticed that the script works if I run it via php command in terminal, but doesn't work if I open it in a browser.
I have permissios of all files set to 777 just in case.
here's the code
< ?php
exec('lame -f -V 9 ../furniture/sound/107887.wav ../furniture/sound/107887.ogg');
The thing is that script works if I run it in terminal with php command, but doesn't via browser.
Oh and this scirpt takes really max two seconds, so it coundn't be a timeout or anything. I also get no php or nginx errors.
How can I solve this?
Is lame on php's execution PATH? Try replacing lame with /usr/bin/lame or whatever.
How about the relative pathnames to the file arguments? Is ../furniture/ etc. correct from the current directory as seen by PHP? If in doubt, replace these with full pathnames too and see if that fixes the problem.
Less likely but worth mentioning: You might want to check that lame itself has all its executable bits set. If you own the file and it's only executable by the owner, apache won't be able to run it.
There may be many reasons why it is not working from the webserver, perhaps resource or permission related. You can find this out by using the second optional parameter of exec:
<?php
$output = "";
exec('lame -f -V 9 ../furniture/sound/107887.wav ../furniture/sound/107887.ogg',$output);
echo $output;
?>

Problems with a PHP shell script: "Could not open input file"

Ok, I am trying to create an email logger, that uses a PHP shell script. I have set up CPanel to pipe emails to my script. I am sure this is all configured properly. However I am having problems with the script, well any script for that matter when running it from the shell.
here is an example.
#!/usr/local/bin/php –q
<?php
/* Read the message from STDIN */
$fd = fopen("php://stdin", "r");
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
/* Saves the data into a file */
$fdw = fopen("mail.txt", "w+");
fwrite($fdw, $email);
fclose($fdw);
/* Script End */
?>
Real simple, right? Read from STDIN and write to a file...I thought something was wrong, not able to read STDIN for some reason. Hosting provider allows it, allow_url_open and allow_url_include are both on.
When executing the script via SSH I get the following error:
Could not open input file: âq
So once again I thought that was the script telling me, that is could not read from STDIN
So I tried just a simple script.
#!/usr/local/bin/php –q
<?php
echo 'Hello World';
?>
Same thing:
Could not open input file: âq
So it appears that the PHP program is telling me it is unable to open the script? The script is located in $HOME/mail/forward (CHMOD 755) and the script itself is CHMOD 755, as well the file mail.txt is CHMOD 755
I am really stumped on this.
I just experienced this issue and it was because I was trying to run a script from the wrong directory.. doh! It happens to the best of us.
Have you tried:
#!/usr/local/bin/php
I.e. without the -q part? That's what the error message "Could not open input file: -q" means. The first argument to php if it doesn't look like an option is the name of the PHP file to execute, and -q is CGI only.
EDIT: A couple of (non-related) tips:
You don't need to terminate the last block of PHP with ?>. In fact, it is often better not to.
When executed on the command line, PHP defines the global constant STDIN to fopen("php://stdin", "r"). You can use that instead of opening "php://stdin" a second time: $fd = STDIN;
I landed up on this page when searching for a solution for “Could not open input file” error. Here's my 2 cents for this error.
I faced this same error while because I was using parameters in my php file path like this:
/usr/bin/php -q /home/**/public_html/cron/job.php?id=1234
But I found out that this is not the proper way to do it. The proper way of sending parameters is like this:
/usr/bin/php -q /home/**/public_html/cron/job.php id=1234
Just replace the "?" with a space " ".
Windows Character Encoding Issue
I was having the same issue. I was editing files in PDT Eclipse on Windows and WinSCPing them over. I just copied and pasted the contents into a nano window, saved, and now they worked. Definitely some Windows character encoding issue, and not a matter of Shebangs or interpreter flags.
When you use php CLI argument -q doesn't exist.
I had the same problem when I wrote script in the Windows (eclipse) and I tried run them on Linux. Every line in file from Windows is ended by \r\n. I had to delete \r in first line that contained parser path:
When \r was deleted from first line (mcedit shown \r as ^M) script ran correctly.
Due to windows encoding issue for me
I experienced this "Could not open input file" error. Then I obtained the file using wget from another linux system, and the error did not occur.
The error ws only occurring for me when the file transited through windows.
I know its stupid but in my case i was outside of my project folder i didn't have spark file.
For me the problem was I had to use /usr/bin/php-cgi command instead of just /usr/bin/php
php-cgi is the command run when accessed thru web browser.
php is the CLI command line command.
Not sure why php cli is not working, but running with php-cgi instead fixed the problem for me.
The actual root cause is that the hyphen before "q" on the first line is actually not a standard ASCII hyphen(U+002D), but an en-dash(U+2013). The "a" with the funny hat that PHP reported was the first clue, and then if you look closely the "dash" is too big. This is the kind of thing word processors do to code.

Categories