I'm developing a Qt application where I need to execute a PHP script. Despite the PHP file being located in the same directory as the project files, I receive an error when trying to run the script using QProcess
. Here's the snippet causing the issue:
this->getMined.start("php", QStringList() << "get_cexio_BTC.php");
this->getMined.waitForFinished();
QByteArray output = getMined.readAll();
When I run the application, I get the following error:
Could not open input file: get_cexio_BTC.php
Is there something I'm overlooking in the QProcess
setup that could be causing this file path issue?
So if php
is not found in the path, or didn't get loaded into the environment variables that QProcess
is using, you could get an error like that.
Try printing out the environment variables:
qDebug() << "Process Environment" << myProcess.getProcessEnvironment().toStringList();
If under the PATH
variable it doesn't mention the location of php.exe
, it will fail.
You may need to specify php.exe
instead of just php
. If it is being found correctly, then you should be able to see the usage statement of the php command when it isn't ran with any input file.
http://www.php.net/manual/en/features.commandline.options.php
If that isn't the issue, then make sure that you are in the working directory that you think you are.
system("dir");
or
qDebug() << "current path" << QDir::currentPath();
You can change the current working directory in code, or specify it for a specific process or you can go and change it from Qt's Project Settings > Run > Working Directory.
And if you didn't already, be sure to check/print out the error string of your QProcess
instance when it isn't doing what you want it to do.
qDebug() << "error string?" << myProcess.errorString();
And make sure you know to use forward slashes and quotes where appropriate. If you have an unquoted command line argument/filename, it may see spaces in your path as another option to process.
And lastly if you are on Windows 8 and you are accessing something on a network drive, you have to run a net use command to be able to access those paths from the commandline, even if they are mapped and working in Windows explorer.
Hope that helps.