【问题标题】:Edit file on server with PHP and C#使用 PHP 和 C# 在服务器上编辑文件
【发布时间】:2015-02-01 18:28:47
【问题描述】:

这是我的 C# 代码:

WebClient myClient = new WebClient();
NameValueCollection inputs = new NameValueCollection();
inputs.Add("decrement", "true");
System.Uri uri = new System.Uri ("http://myserver/myPHP.php");
myClient.UploadValuesAsync (uri, "POST", inputs);

这是我在服务器上的myPHP.php 文件:

if($_POST['decrement'] == "true") {
    $file = './myTextFile.txt';
    // Open the file to get existing content
    $current = file_get_contents($file);
    // Append a new person to the file
    $current .= "John Smith\n";
    // Write the contents back to the file
    file_put_contents($file, $current);
}

txt 文件没有被写入,为什么不呢?

注意:myPHP.phpmyTextFile.txt 在服务器上的同一目录中

【问题讨论】:

  • 可能是因为 $_POST['decrement'] 不 == "true"
  • 但我将它设置在 C# 的键值对中。请与我平起平坐,我没有php经验。
  • 或者因为使用./ 文件路径不起作用。您需要使用一个函数来为您提供服务器的 htdocs 路径。

标签: c# php text-files server


【解决方案1】:

如果您的文件直接在 htdocs 下:

 $file = $_SERVER['DOCUMENT_ROOT'] . '/myTextFile.txt'; 

【讨论】:

  • 如果不是直接在httpdocs下怎么办。它位于同一级别的文件夹下。
  • @Ogen,然后是 $file = $_SERVER['DOCUMENT_ROOT'] . '/folder/myTextFile.txt';
  • @Ogen,基本上重点是,./folder/ 行不通。您需要完整路径,例如c:/whatever/whatever。使用$_SERVER['DOCUMENT_ROOT'] 只会为您提供c:/prog files/apache/htdocs 或您安装它的任何位置。
猜你喜欢
  • 1970-01-01
  • 2011-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-20
  • 1970-01-01
  • 2019-12-21
相关资源
最近更新 更多