使用 fopen 打开 .txt 文件会创建额外的空行
Opening a .txt file with fopen creates extra blank lines
我正在尝试在 vb.net 中生成一个字符串,然后我想将其发送到我的服务器,一个 .php 文件,该文件应将该字符串附加到 .txt 文件。字符串中有多行。字符串正确到达 php 文件。但是,在写入 .txt 文件期间,php 会创建额外的空行,这是我不想要的。
我想要在我的 .txt 文件中的内容:
line1
line2
line3
我得到的:
line1
line2
line3
我尝试更改 fopen 调用的参数(在参数中添加 b 以强制使用二进制模式),但没有帮助。我还尝试用 vb.net 代码中的“\n”和“\r\n”替换 vbNewLine 以分隔字符串中的行,但不幸的是,它无法识别,并被写入 .txt 文件喜欢
line1 \r\n line2 \r\n line3
这是我的 vb.net 申请代码:
function sendStringToServer()
Dim stringToSend as String
stringToSend = "line1" & vbNewline
stringToSend &= "line2" & vbNewline
stringToSend &= "line3"
Dim wc As New WebClient
wc.Headers("content-type") = "application/x-www-form-urlencoded"
Dim response As String = wc.UploadString("http://www.example.com/uploaddata.php", "data=" & data)
MessageBox.Show(response)
(注意回答正确,没有多余的空行。)
还有 upploaddata.php 代码:
<?php
if (isset($_POST["data"]))
{
echo($_POST["data"]);
$data=$_POST['data'];
$fp = fopen('data.txt', 'ab');
fwrite($fp, $data);
fclose($fp);
}
else
{
echo "ERROR: No data!";
}
?>
我做错了什么?
编辑:在被要求做一个最小的、完整的和可验证的例子之后,我粘贴了代码和结果:
在vb.net应用中,向uploaddata.php发送字符串的Sub:
function sendStringToServer()
Dim stringToSend as String
stringToSend = "line1" & vbNewline & "line2" & vbNewline & "line3"
Dim wc As New WebClient
wc.Headers("content-type") = "application/x-www-form-urlencoded"
Dim response As String = wc.UploadString("http://www.example.com/uploaddata.php", "data=" & data)
MessageBox.Show(response)
整个uploaddata.php内容:
<?php
if (isset($_POST["data"]))
{
var_dump($_POST['data']);
$data=$_POST['data'];
file_put_contents('data.txt', $data);
}
else
{
echo "ERROR: No data!";
}
?>
在 vb.net 应用程序中调用 sendStringToServer() 后 data.txt 的内容:
line1
line2
line3
一些建议,其中之一有望解决您的问题。而不是使用 vbNewLine
(在作为 POST 数据发送之前是 deprecated BTW) try using vbLf
instead. Ensure your values are trimmed of leading and trailing whitespace before using them (I assume your actual code will be using variables.) Also your string needs to be properly URL encoded。
function sendStringToServer()
Dim stringToSend as String
stringToSend = Trim("line1") & vbLf
stringToSend &= Trim("line2") & vbLf
stringToSend &= Trim("line3")
stringToSend = HttpUtility.UrlEncode(stringToSend)
Dim wc As New WebClient
wc.Headers("content-type") = "application/x-www-form-urlencoded"
Dim response As String = wc.UploadString("http://www.example.com/uploaddata.php", "data=" & stringToSend)
MessageBox.Show(response)
在 PHP 方面,您可以使用 file_put_contents
来简化您的代码,而不是旧的 fopen
、fwrite
、fclose
三重奏。
<?php
if (isset($_POST["data"])) {
echo($_POST["data"]);
file_put_contents("data.txt", $_POST["data"], FILE_APPEND);
} else {
echo "ERROR: No data!";
}
我正在尝试在 vb.net 中生成一个字符串,然后我想将其发送到我的服务器,一个 .php 文件,该文件应将该字符串附加到 .txt 文件。字符串中有多行。字符串正确到达 php 文件。但是,在写入 .txt 文件期间,php 会创建额外的空行,这是我不想要的。
我想要在我的 .txt 文件中的内容:
line1
line2
line3
我得到的:
line1
line2
line3
我尝试更改 fopen 调用的参数(在参数中添加 b 以强制使用二进制模式),但没有帮助。我还尝试用 vb.net 代码中的“\n”和“\r\n”替换 vbNewLine 以分隔字符串中的行,但不幸的是,它无法识别,并被写入 .txt 文件喜欢
line1 \r\n line2 \r\n line3
这是我的 vb.net 申请代码:
function sendStringToServer()
Dim stringToSend as String
stringToSend = "line1" & vbNewline
stringToSend &= "line2" & vbNewline
stringToSend &= "line3"
Dim wc As New WebClient
wc.Headers("content-type") = "application/x-www-form-urlencoded"
Dim response As String = wc.UploadString("http://www.example.com/uploaddata.php", "data=" & data)
MessageBox.Show(response)
(注意回答正确,没有多余的空行。) 还有 upploaddata.php 代码:
<?php
if (isset($_POST["data"]))
{
echo($_POST["data"]);
$data=$_POST['data'];
$fp = fopen('data.txt', 'ab');
fwrite($fp, $data);
fclose($fp);
}
else
{
echo "ERROR: No data!";
}
?>
我做错了什么?
编辑:在被要求做一个最小的、完整的和可验证的例子之后,我粘贴了代码和结果:
在vb.net应用中,向uploaddata.php发送字符串的Sub:
function sendStringToServer()
Dim stringToSend as String
stringToSend = "line1" & vbNewline & "line2" & vbNewline & "line3"
Dim wc As New WebClient
wc.Headers("content-type") = "application/x-www-form-urlencoded"
Dim response As String = wc.UploadString("http://www.example.com/uploaddata.php", "data=" & data)
MessageBox.Show(response)
整个uploaddata.php内容:
<?php
if (isset($_POST["data"]))
{
var_dump($_POST['data']);
$data=$_POST['data'];
file_put_contents('data.txt', $data);
}
else
{
echo "ERROR: No data!";
}
?>
在 vb.net 应用程序中调用 sendStringToServer() 后 data.txt 的内容:
line1
line2
line3
一些建议,其中之一有望解决您的问题。而不是使用 vbNewLine
(在作为 POST 数据发送之前是 deprecated BTW) try using vbLf
instead. Ensure your values are trimmed of leading and trailing whitespace before using them (I assume your actual code will be using variables.) Also your string needs to be properly URL encoded。
function sendStringToServer()
Dim stringToSend as String
stringToSend = Trim("line1") & vbLf
stringToSend &= Trim("line2") & vbLf
stringToSend &= Trim("line3")
stringToSend = HttpUtility.UrlEncode(stringToSend)
Dim wc As New WebClient
wc.Headers("content-type") = "application/x-www-form-urlencoded"
Dim response As String = wc.UploadString("http://www.example.com/uploaddata.php", "data=" & stringToSend)
MessageBox.Show(response)
在 PHP 方面,您可以使用 file_put_contents
来简化您的代码,而不是旧的 fopen
、fwrite
、fclose
三重奏。
<?php
if (isset($_POST["data"])) {
echo($_POST["data"]);
file_put_contents("data.txt", $_POST["data"], FILE_APPEND);
} else {
echo "ERROR: No data!";
}