-2

I am encountering difficulties with writing to a new line in a basic text file. All the other text (including the comma) appears just fine, but no new line (no space or anything either). I would greatly appreciate any direction.

<?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);


?>
KellyM
  • 2,472
  • 6
  • 46
  • 90
  • `\r\n` is what you are looking for – cmorrissey May 06 '16 at 18:47
  • Unless I am misunderstanding, isn't that what I did here? $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']); – KellyM May 06 '16 at 18:47
  • *Hm...*, I think that `format()` function of yours may be stripping those out, I can't be 100% sure about it though but it tingles my spidey sense here for some reason. – Funk Forty Niner May 06 '16 at 18:48
  • However, there should be a final `"\n"` in your last line though and you don't have one which I think is the reason here. So do `format($_POST['fourthFreqMessage']) . "\n";` – Funk Forty Niner May 06 '16 at 18:50
  • /r/n worked, thanks to everyone! – KellyM May 06 '16 at 18:51
  • ah so you're in Windows I see. That's why. Had we known right off the bat... ;-) – Funk Forty Niner May 06 '16 at 18:51
  • @KellyMarchewa check my answer – Robert May 06 '16 at 18:54

1 Answers1

1

Instead of using \n use constant called PHP_EOL which represents end of line according to your enviroment.

PHP_EOL (string) The correct 'End Of Line' symbol for this platform. Available since PHP 5.0.2

$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']);

PHP_EOL in windows will be CR+LF and in unix system LF

  • CR - Carriage Return, U+000D
  • LF - Line Feed, U+000A
Robert
  • 19,800
  • 5
  • 55
  • 85