Jump to content

create a php file using exec with variables


moosey_man1988
Go to solution Solved by Jacques1,

Recommended Posts

Hi Everyone

 

im currently creating an install script and i was hoping to touch and cat to a lot of files for them to be created, but im having troubles.

 

You will easily see in the code below what im trying to achieve.

//Lets create the mysqli_connection page for all other php pages to use with the new DB user
exec ('touch database/mysqli_connection.php');
exec ('echo "<?php
$mysqli_connection = mysqli_connect('.$servername.','. $dbnewuser.','. $newuserpass.','. $dbname.'); 
if (!$mysqli_connection){
die("Connection Failed: " . mysqli_connect_error());
}
?>" > database/mysqli_connection.php'); 

the issue I have is this is how the file comes out, its having trouble passing $mysqli_connection as a string.

 

see the mysqli_connection.php below

<?php
 = mysqli_connect(localhost,testuser,testPassword,testDB);
if (!){
die(Connection Failed:  . mysqli_connect_error());
}
?>

I did try using single quotes around the $mysqli_connection variable like so

 

'.$mysqli_connection.' but the file then comes out blank.

 

any help given would be greatly appreciated, and i thank everyone for their help in advance :)

Edited by moosey_man1988
Link to comment
Share on other sites

Well, nesting three languages just isn't a good idea: You have a PHP code generating shell code generating PHP code. No wonder you have syntax conflicts.

 

This can be made a lot simpler. PHP can in fact write files, there's no need to delegate this to the shell. There's also no need to generate an entire PHP script. Just define a bunch of constants and put the application logic into static scripts:

<?php

const DB_USER = <insert string here>;
const DB_PASSWORD = <insert string here>;
...

You should also consider using a simple data format like JSON, YAML or XML instead of a full-blown PHP script.

Link to comment
Share on other sites

those are yet languages on my list to learn :) this script will ever be improved upon with new langues as I learn more languages etc etc.

 

Thanks for the advice, im still not clear on ALL of a functions that PHP can do, but this is what forums and guides are for :D

 

I will test out the put_file_contents, Thanks for a swift response :)

Link to comment
Share on other sites

i created a test script, still no luck :(

<?php

$servername = "localhost";
$username = "root";
$DBpassword = "testPassword";
$dbname = "testDB";
$invoiceDB = "invoices";

$dbnewuser = "testUser";
$newuserpass = "testPass";

$file = "database/mysqli_connection.php";

$myString = "<?php 
$mysqli_connection = mysqli_connect($servername,$dbnewuser,$newuserpass,$dbname); 
if (!$mysqli_connection){
die('Connection Failed: ' . mysqli_connect_error());
}
?>";

file_put_contents($file, $myString);
$getData = file_get_contents($file);
echo $getData;

?>

Ignore the unused variables, this was just something quick to see if can be accomplished.

 

here's the error

 

PHP Notice:  Undefined variable: mysqli_connection in /var/www/html/test.php on line 14

PHP Notice:  Undefined variable: mysqli_connection in /var/www/html/test.php on line 19

 

I really do hope to make a connection script, as once the installation finished it, all the rest of my script call upon the mysqli_connection.php

Edited by moosey_man1988
Link to comment
Share on other sites

  • Solution

This makes no sense.

 

You want $servername, $dbnewuser etc. to be replaced with the corresponding values, but at the same time you want $mysqli_connection to show up as a literal variable in the generated script. How is PHP supposed to tell the difference? If you want a literal dollar sign within a double-quoted string, you need to escape it with a backslash:

echo "\$some_variable";

Secondly, you can't just insert strings into your code template, because those will end up as raw words. What you want are string literals, and those have quotes:

$string_content = 'yada yada yada';
echo "String literal: '".addslashes($string_content)."'";

You should escape the content with addslashes() so that a literal quote within the string content won't blow up your generated PHP script.

Edited by Jacques1
Link to comment
Share on other sites

Hey Jaques, Thank you for your response again your answer solved it for me VERY quickly, I will remember in future that magical backslash :)

 

Resulting Code below

//Lets create the mysqli_connection page for all other php pages to use with the new DB user

$mysqli_file = "database/mysqli_connection.php";

$mysqli_connection_setup = "<?php
\$mysqli_connection = mysqli_connect($servername, $dbnewuser, $newuserpass, $dbname); 
if (!\$mysqli_connection){
die('Connection Failed: ' . mysqli_connect_error());
}
?>";

file_put_contents($mysqli_file, $mysqli_connection_setup);

Thanks again you must grow tired of PHP newbs :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.