0

Im looking to figure out a way to replace variable inside a php file then send that modified file thru FTP.

I wish to delete items by keyword for each of my clients... so i dont have to log into their websites everytime. I just need to change the value to a variable inside a php file. I'm having trouble escaping out the $ or something... Is there an eaiser way to say (change $variable to $value in $file)? or overwrite a file a specific?

Here is an example of the file i want to modify

<?php 
mysql_connect(host,user,pass);
mysql_select_db(dbName);

$keyword = ""; //I want to replace this line

mysql_query("delete from products where product_name like '%$keyword%'");

?>

I want to run a script on my main servers similar to this

<?php

 $handle = fopen("keyword_remover.php", "w");
            if ($handle) {
                while (($line = fgets($handle)) !== false) {
                  $write_line .=  str_replace("\$keyword=\"\";", "\$keyword=\"".mysql_real_escape_string($_POST['keyword'])."\"", $line);
                }
            } else {
                // error opening the file.
            }
            fputs($handle,$write_line);
            fclose($handle);

//make sure that FTP connection is valid
            $connection_url = "ftp.".$client_domain;

            // Set up a connection
            $ftp_conn = ftp_connect($connection_url);

            // Login
            if (ftp_login($ftp_conn, $client_user, $client_pass))
            {
                $connected = "1";
            }else{
                $connected = "0";
            }

            if($connected == "1")
            {
                //FTP connection worked, lets push the script and run it!
                $local_dir = "keyword_remover.php";
                $remote_dir = "remote_path/keyword_remover.php";
                ftp_put($ftp_conn, $remote_dir, $local_dir, FTP_ASCII);
                $script_url = "http://".$client_domain."//"keyword_remover.php";
                scriptHit($script_url); // Hit the script with curl here
                sleep(1);
                ftp_delete($ftp_conn, $remote_dir);


                echo $script." ran on " .$client_domain."<br>";


            }else{
                echo "Could not connect to FTP: ". $connection_url."<br>".$client_user."<br>".$client_pass;
            }
            ftp_close($ftp_conn);

?>

Thank you in advanced :)

2
  • 2
    Wow, that's over-complicating the matter. Just have PHP read a config file or something and upload the changes to those. Commented May 5, 2014 at 23:38
  • 1
    In addition to what Phil mentions, you should see an anti-pattern in what you are doing. Writing PHP scripts to modify variable (or is this REALLY a constant) values in a PHP file is just lunacy. This is what database, config files, content management systems, admin panels, etc. are meant to do. Commented May 5, 2014 at 23:45

1 Answer 1

2

Further to the comments, its as simple as...

 $keyword = file_get_contents("keyword.txt");   

and let FTP to overwrite keyword.txt when you change your word.

nb keyword.txt contains the key word and nothing else.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much.. Didn't really consider the option of having my keyword as a single file.. Good idea mortimerCat ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.