PHP shell_exec not operating as expected with sqlite3 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

PHP shell_exec not operating as expected with sqlite3

I am trying to use php shell_exec() to use sqlite3 to _create_ a new sqlite db file. If I do: sqlite3 dB.sqlite .schema on the CLI, it will create a file called dB.sqlite on the disk filesystem, no output on the CLI. If I exec this same command with PHP using backticks or shell_exec() I get no output returned, but no file created. Why? And how can I make php do this?

20th Feb 2023, 9:38 AM
Nathan Stanley
Nathan Stanley - avatar
2 Answers
+ 1
The issue you are encountering is likely due to differences in the environment and permissions when running the command through PHP's shell_exec() function versus running it directly on the command line. When you run the command on the command line, you are likely doing so as a user with sufficient permissions to create the new file. However, when running the command through PHP, it is likely running as a different user or with different permissions, which may not have the necessary privileges to create the file. To address this, you can try specifying the full path for the new database file when running the sqlite3 command, and ensuring that the user running the PHP script has the necessary write permissions to create the file in that location. For example, you can try running the following command through shell_exec() in PHP:shell_exec('sqlite3 /path/to/new/dB.sqlite .schema');
22nd Feb 2023, 8:59 AM
Last
Last - avatar
+ 1
Replace "/path/to/new" with the actual path where you want to create the new file. You can also try adding the "-bail" flag to the sqlite3 command to see if any error messages are being generated. shell_exec('sqlite3 /path/to/new/dB.sqlite .schema -bail'); If you still encounter issues, you may need to check the file system permissions for the path where you are trying to create the new file and ensure that the user running the PHP script has the necessary write permissions.
22nd Feb 2023, 8:59 AM
Last
Last - avatar