jiminikiz Posted March 28, 2010 Share Posted March 28, 2010 Okay firstly, I hate to start a new topic if the question is already answered somewhere (or post this in the wrong area), but I have been looking to solve this very simple question for quite some time now and have found no simple answer. I am trying to run an already compiled and working C++ program from the browser. It will take some string parameters and call a command similar to this: " ./Program name1 id1 name2 id2 0 " This works fine in a linux shell terminal, but not when called from the browser using any kind of PHP execution call (including shell_exec(), exec(), pipe(), etc.) I would always get a "Segmentation Fault". I figured it had to deal with the fact that the program is writing to a text file. Okay, so I wrote a super trivial program to see if I can write a string to a textfile when the program is called from the browser. #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; cout << "Creating File... (hello.txt)\n"; myfile.open ("hello.txt"); cout << "Writing to File...\n"; myfile << "Writing -crap- to this to a file.\n"; cout << "Closing File...\n"; myfile.close(); return 0; } I am calling this code from the browser using: $command = "./hello"; $output = shell_exec($command." 2>&1") ; echo(nl2br($output)); Only the print statements reach the browser window. No text file is created. No errors are generated (or at least not reported in the browser window) and this program definitely works when compiled and run natively. Now I understand I could easily create and write to text files using PHP, but I just need to figure out how to allow a C++ program to create/read/write/delete files when executed from the PHP execution functions. The program that I am trying to run (not the trivial one posted here) creates many text files, and again, works perfectly fine when run from the terminal. I have tried setting all the permissions to 755. I am trying to do this locally (localhost) using XAMMP in linux. Thank you in advance! Quote Link to comment https://forums.phpfreaks.com/topic/196804-c-trouble-with-writing-files/ Share on other sites More sharing options...
Daniel0 Posted March 29, 2010 Share Posted March 29, 2010 Works fine for me: daniel@daniel-laptop:~/test$ ls hello.cpp test.php daniel@daniel-laptop:~/test$ cat hello.cpp #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; cout << "Creating File... (hello.txt)\n"; myfile.open ("hello.txt"); cout << "Writing to File...\n"; myfile << "Writing -crap- to this to a file.\n"; cout << "Closing File...\n"; myfile.close(); return 0; } daniel@daniel-laptop:~/test$ g++ hello.cpp -o hello daniel@daniel-laptop:~/test$ cat test.php <?php $command = "./hello"; $output = shell_exec($command." 2>&1") ; echo $output; ?> daniel@daniel-laptop:~/test$ php -v; php test.php PHP 5.3.3-dev (cli) (built: Mar 23 2010 10:24:53) Copyright (c) 1997-2010 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies with Xdebug v2.0.5, Copyright (c) 2002-2008, by Derick Rethans Creating File... (hello.txt) Writing to File... Closing File... daniel@daniel-laptop:~/test$ ls hello hello.cpp hello.txt test.php daniel@daniel-laptop:~/test$ cat hello.txt Writing -crap- to this to a file. Quote Link to comment https://forums.phpfreaks.com/topic/196804-c-trouble-with-writing-files/#findComment-1033422 Share on other sites More sharing options...
jiminikiz Posted March 29, 2010 Author Share Posted March 29, 2010 I really appreciate your swift reply. I haven't tried that method, and you are right, running that command from the command line also works on my system... However, I guess what my problem really is lies in the attached code, when run in a web browser does not work. All I see are <<cout>> statements. To run the code, I am just clicking the search button for method1 in search.php Calls method1.php [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/196804-c-trouble-with-writing-files/#findComment-1033667 Share on other sites More sharing options...
jiminikiz Posted March 29, 2010 Author Share Posted March 29, 2010 NVM chmod 777 makes everything work. I fear great security risk. But I will worry about that later. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/196804-c-trouble-with-writing-files/#findComment-1033688 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.