我正在寻找一种方法来替换 php 文件中的变量,然后通过 FTP 发送修改后的文件。
我希望为我的每个客户按关键字删除项目...所以我不必每次都登录他们的网站。我只需要将值更改为 php 文件中的变量。我在转义 $ 或其他东西时遇到了麻烦......有没有更简单的说法(将 $variable 更改为 $file 中的 $value)?或覆盖特定文件?
这是我要修改的文件的示例
<?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%'");
?>
我想在我的主服务器上运行类似于此的脚本
<?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);
?>
先谢谢你:)
最佳答案
除了评论之外,它就像...一样简单
$keyword = file_get_contents("keyword.txt");
并让 FTP 在您更改单词时覆盖 keyword.txt。
nb keyword.txt 只包含关键字,没有其他内容。
关于php - 替换php文件中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23483507/