Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-写新行的困难_Php_File - Fatal编程技术网

PHP-写新行的困难

PHP-写新行的困难,php,file,Php,File,我在写基本文本文件中的新行时遇到困难。所有其他文本(包括逗号)看起来都很好,但没有新行(也没有空格或任何内容)。我将非常感谢您的指导 <?php session_start(); $user = $_SESSION['store']; function format($input) { $input = trim($input); $input = stripcslashes($input); $input = htmlspecialchars($input); return $inp

我在写基本文本文件中的新行时遇到困难。所有其他文本(包括逗号)看起来都很好,但没有新行(也没有空格或任何内容)。我将非常感谢您的指导

<?php 
session_start();
$user = $_SESSION['store'];

function format($input) {
$input = trim($input);
$input = stripcslashes($input);
$input = htmlspecialchars($input);
return $input;

}

$file = "messages/reminder.txt";
$image;
$secFreqLabel;
$freqLabel;
$uploadDirectory = "corpImages/";

if(!file_exists($file)) {
$temp = fopen($file, "w"); // create file
fclose($temp);
}

 $con = new PDO("sqlite:bk.db");
if($_SERVER['REQUEST_METHOD'] == "POST") {
$secFreqLabel =  'reminder.jpg';
$freqLabel =  'reminderHourly.jpg';
$thirdFreqLabel =  'reminderThree.jpg';
$fourthFreqLabel =  'reminderFour.jpg';

$freqUploadFile = $uploadDirectory . $freqLabel;
$secFreqUploadFile = $uploadDirectory . $secFreqLabel;
$thirdFreqUploadFile = $uploadDirectory . $thirdFreqLabel;
$fourthFreqUploadFile = $uploadDirectory . $fourthFreqLabel;

$statement = $con->prepare("INSERT INTO slides (image, label) VALUES (:image, :label)");

if(!empty($_FILES['freqImage'])) {
$con->exec("DELETE FROM slides where label = 'reminder.jpg'");
}

if(!empty($_FILES['secFreqImage'])) {
$con->exec("DELETE FROM slides where label = 'reminderHourly.jpg'");
}

if(!empty($_FILES['thirdFreqImage'])) {
$con->exec("DELETE FROM slides where label = 'reminderThree.jpg'");
}

if(!empty($_FILES['fourthFreqImage'])) {
$con->exec("DELETE FROM slides where label = 'reminderFour.jpg'");
}

$image = file_get_contents($_FILES['freqImage']['tmp_name']);
move_uploaded_file($_FILES['freqImage']['tmp_name'], $freqUploadFile);
$statement->bindParam(":label", $freqLabel);
$statement->bindParam(":image", $image);
$statement->execute();

$image = file_get_contents($_FILES['secFreqImage']['tmp_name']);
move_uploaded_file($_FILES['secFreqImage']['tmp_name'], $secFreqUploadFile);
$statement->bindParam(":label", $secFreqLabel);
$statement->bindParam(":image", $image);
$statement->execute();

$image = file_get_contents($_FILES['thirdFreqImage']['tmp_name']);
move_uploaded_file($_FILES['thirdFreqImage']['tmp_name'], $thirdFreqUploadFile);
$statement->bindParam(":label", $thirdFreqLabel);
$statement->bindParam(":image", $image);
$statement->execute();

$image = file_get_contents($_FILES['fourthFreqImage']['tmp_name']);
move_uploaded_file($_FILES['fourthFreqImage']['tmp_name'], $fourthFreqUploadFile);
$statement->bindParam(":label", $fourthFreqLabel);
$statement->bindParam(":image", $image);
$statement->execute();

$text = format($_POST['freq']) . "," . format($_POST['freqMessage']) . "\n" . format($_POST['secFreq']) . "," . format($_POST['secFreqMessage']) . "\n" . format($_POST['thirdFreq']) . "," . format($_POST['thirdFreqMessage']) . "\n" . format($_POST['fourthFreq']) . "," . format($_POST['fourthFreqMessage']);

$writer = fopen($file, "w");
fwrite($writer, $text);
fclose($writer);

touch("update.txt");
}

$handler = fopen($file, "r");

$firstReminder = fgetcsv($handler, ",");
$secondReminder = fgetcsv($handler, ",");
$thirdReminder = fgetcsv($handler, ",");
$fourthReminder = fgetcsv($handler, ",");
fclose($handler);


?>

而不是使用
\n
使用名为
PHP\u EOL
的常量,该常量根据您的环境表示行尾

PHP_下线(字符串) 此平台的正确“线端”符号。从PHP5.0.2开始提供

windows中的
PHP\u EOL
将是
CR+LF
,unix系统中的
LF

  • CR
    -回车,U+000D
  • LF
    -线路馈电,U+000A

\r\n
是您想要的,除非我有误解,不是我在这里做的吗?$text=格式($\u POST['freq'])。"," . 格式($\u POST['freqMessage'])。“\n”。格式($\u POST['secFreq'])。"," . 格式($\u POST['secFreqMessage'])。“\n”。格式($\u POST['thirdFreq'])。"," . 格式($\u POST['thirdFreqMessage'])。“\n”。格式($_POST['fourthFreq'])。"," . 格式($_POST['fourthFreqMessage']);嗯…,我想你的
format()
函数可能会把它们去掉,虽然我不能100%确定,但出于某种原因,它会刺痛我的蜘蛛感觉。然而,在你的最后一行中应该有一个最后的
“\n”
,而你没有一个,我认为这就是原因。也可以使用
格式($\u POST['fourthFreqMessage'])。“\n”/r/n工作正常,谢谢大家!
$text = format($_POST['freq']) . "," . format($_POST['freqMessage']) . PHP_EOL . format($_POST['secFreq']) . "," . format($_POST['secFreqMessage']) . PHP_EOL . format($_POST['thirdFreq']) . "," . format($_POST['thirdFreqMessage']) . PHP_EOL . format($_POST['fourthFreq']) . "," . format($_POST['fourthFreqMessage']);