VERY simple routine that pulls the name of each PDF file in a folder and creates a list of each file found with a link to it. But I cannot get a line break between the items
Nothing seems to give me each file name and link on a new line. I have no idea why.
Neither of the \n has any affect at all.
<?php
$directory = "./images/";
$phpfiles = glob($directory . "*.pdf");
foreach($phpfiles as $phpfile)
{
echo "\n","<a href=$phpfile>".basename($phpfile)."</a>";
echo "\n";
}
?>
All I get for output is this....all on one line.
CAD_REPEQUEST.pdf JP_CADS.pdf JP_Reports.pdf
What I want is this....
CAD_REPEQUEST.pdf
JP_CADS.pdf
JP_Reports.pdf
If you're outputting to a Web page you'll need to replace \n with <br>.
If you're outputting to a text or other type of file, you may wish to replace \n with the constant PHP_EOL.
If you're outputting this to an email that's getting sent in text format, you'll have trouble with some clients, like Outlook, that will automatically attempt to remove unnecessary line breaks. Sometimes using a double line break helps, or you could convert your email to HTML and use the <br> approach.
Related
I'm trying to use file_put_contents to manage bans in a .txt
However, I'm having trouble adding text or a new line amidst the text I'm adding.
I'm using $_GET to grab the reason and information of the banned person, i.e "loser,127.0.0.1" (simple example) and then add them to the txt. Thing is I can't figure out how to add a new line. When I try adding text
<?php
file_put_contents("banned.txt", $_GET["r"], + "for example here", FILE_APPEND);
The code fails to run, I'm not sure whether or not to actually have a comma either.
This is the code I'm trying to use as of now, and it does add a line, but it doesn't go to the next line.
<?php
file_put_contents("banned.txt", $_GET["r"], FILE_APPEND);
What I'm trying to achieve is that it adds a new line, so if I said "loser,127.0.0.1" it adds that text to the txt, and goes to the next line for the next ban.
Try this
get your ban data and explicitly add the new line:
$banData = $_GET["r"] . PHP_EOL;
If you want to write csv data in the file (in a very simple way) you can do so like this:
$banData = $_GET["ban_data"] . ";" . $_GET["ban_reason"] . PHP_EOL;
then simply write to the file
file_put_contents("banned.txt", $banData, FILE_APPEND);
instead of "banned.txt" save to "banned.csv" and you're set
First code is invalid as you should not have , between your GET reference and concatenated string. Once that is fixed, just add \n (\r\n on Windows) at the end of your string that you append and you should have new lines (or to stay platform agnostic, use PHP_EOL instead).
I have trawled online looking for a working example, but I must be missing something, as it has been answered numerous times but I can't get it working.
I'm trying to send data ($file) to an html file. I can echo /print no bother but can't break a new line when sending to a html file via file_put_contents. I've tried "\r\n" with no success.
I'm using Chrome. This is the code I thought should work:
$someData = $_POST['someData'];
$file = 'file.html';
file_put_contents($file, $someData . PHP_EOL, FILE_APPEND);
In HTML neither CR, LF nor CRLF matters really. That's why <br /> tag exists and you need to use it instead or display your content in <pre> block or similar which is one of a few exceptions when CR/LF thing actually works as elsewhere.
file_put_contents($file, $someData . PUT THE BREAK HERE IN INVERTED COMMAS . PHP_EOL, FILE_APPEND);
It was as simple as that on Chrome. I think I might have (gulp) forgotten to upload a file when i was trying this earlier.
I am trying to write a function in which a string of text is written with timestamps to the file "text2.txt", I want each entry to be on a new line, but PHP_EOL does not seem to work for me.The strings simply write on the same line and does not write to a new line for each string.
Could anyone give me some pointers or ideas as to how to force the script to write to a new line every time the function is activated?
Some sort of example would be highly appreciated.
Thank you in advance.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['sendmsg']))
{
writemsg();
}
function writemsg()
{
$txt = $_POST['tbox'];
$file = 'text2.txt';
$str = date("Y/m/d H:i:s",time()) . ":" . $txt;
file_put_contents($file, $str . PHP_EOL , FILE_APPEND );
header("Refresh:0");
}
?>
Also, I want to get rid of the character count on the end of the string when using the below code :
<?php
echo readfile("text2.txt");
?>
Is there any way for the character count to be disabled or another way to read the text file so it does not show the character count?
Could anyone give me some pointers or ideas as to how to force the script to write to a new line every time the function is activated? Some sort of example would be highly appreciated.
Given the code you posted I'm pretty sure newlines are properly appended to the text lines you are writing to the file.
Try opening the file text2.txt on a text editor to have a definitive confirmation.
Note that if you insert text2.txt as part of a HTML document newlines won't cause a line break in the rendered HTML by the browser.
You have to turn them into line break tags <br/>.
In order to do that simply
<?php
echo nl2br( file_get_contents( "text2.txt" ) );
?>
Using file_get_contents will also solve your issue with the characters count display.
A note about readfile you (mis)used in the code in your answer.
Accordind to the documentation
Reads a file and writes it to the output buffer.
[...]
Returns the number of bytes read from the file. If an error occurs, FALSE is returned and unless the function was called as #readfile(), an error message is printed.
As readfile reads a file and sends the contents to the output buffer you would have:
$bytes_read = readfile( "text2.txt" );
Without the echo.
But in your case you need to operate on the contents of the file (replacing line breaks with their equivalent html tags) so using file_get_contents is more suitable.
To put new line in text simply put "\r\n" (must be in double quotes).
Please note that if you try to read this file and output to HTML, all new line (no matter what combination) will be replaced to simple space, because new line in HTML is <br/>. Use nl2br($text) to convert new lines to <br/>'s.
For reading file use file_get_contents($file);
ive got the following fwrite code, with , separating the data and it ending in ))
$shapeType = $_POST['shapeType'].','.$_POST['triangleSide1'].','.$_POST['triangleSide2']
.','.$_POST['triangleSide3'].','.$_POST['triangleColour'].'))';
fwrite($handle, $shapeType);
but this is how it saves in the text file...
,,,,))Triangle,180,120,80,Red))
why have the first set of
,,,,,))
appeared in front of what it should look like?
You need to add a new line character to the end of each line. Otherwise your lines will all run into each other.
Use PHP_EOL for this as it will automatically use the Operating System appropriate new line character sequence.
PHP_EOL (string)
The correct 'End Of Line' symbol for this platform.
Available since PHP 4.3.10 and PHP 5.0.2
$shapeType = $_POST['shapeType'].','.$_POST['triangleSide1'].','.$_POST['triangleSide2']
.','.$_POST['triangleSide3'].','.$_POST['triangleColour'].'))'.PHP_EOL;
FYI, this might be a little cleaner to do using sprintf():
$shapeType = sprintf("%s,%s,%s,%s,%s))%s",
$_POST['shapeType'],
$_POST['triangleSide1'],
$_POST['triangleSide2'],
$_POST['triangleSide3'],
$_POST['triangleColour'],
PHP_EOL
);
Without seeing more of the code I would guess that you post to the same file and you do not check if a POST request was made before you write your file. So probably you write to your file on a GET request as well, causing empty entries to appear.
You would need something like:
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// ...
$shapeType = $_POST['shapeType'].','.$_POST['triangleSide1'].','.$_POST['triangleSide2']
.','.$_POST['triangleSide3'].','.$_POST['triangleColour'].'))';
fwrite($handle, $shapeType);
// ...
}
Edit: By the way, you should probably use fputcsv as that takes care of escaping quotes, should you change something in the future that adds for example a description field.
I am having trouble with "\n" character not working. I realized that it wasn't work while testing output of variables using a simple echo statement. I have tried approaching the new line character a few different ways to see if it was just me, but nothing I have tried is working. Here is an example of some attempts I have made:
<?php
// Establish Connection to Taskaro DB
require "../_connections/connection_taskDB.php";
// Start Session
session_start();
// Create Session Variables
$_SESSION['userID'];
$_SESSION['companyID'];
$_SESSION['usernameDB'];
// Convert Session Variables to page variables
$userID = $_SESSION['userID'];
$currentUser = $_SESSION['usernameDB'];
$editType = $_REQUEST['editType'];
$projectID = $_REQUEST['projectID'];
// Testing if new line character is working
echo "hello, Mr. New Line!\n\r";
echo "This line should be below 'hello, Mr. New Line!'";
// Testing variable and session connection
echo "SESSION VARIABLES:"."\\n\n"."userID = {$userID}";
echo "userID = {$userID}"."/n";
echo "currentUser = {$currentUser}"."\r";
echo "companyID = {$companyID}\n\r";
echo "\nPOST VARIABLES:\n";
echo "editType = {$editType}\n";
echo "projectID = {$projectID}\n";
?>
I read up on some other overflow questions that had similar problems and none of them fixed my problem. The project is on a remote server (GoDaddy) in which php has been installed. The document has the correct file extension (.php). I am coding in dreamweaver and uploading my script for testing. From the code you can see I've tried "\n","\n\r","\r". I've also tested in both Firefox and Google Chrome.
I also tried to concatenate the "\n" character, and took a shot in the dark and even tried using the forward slash rather than the backslash (I knew it wouldn't work, but I'm getting pretty frustrated at this point). I bet it's something simple but I don't see what else is could be. Thanks in advanced.
If you view the source of the page, you will see all of those values output on separate lines.
If you are viewing the file in the browser, you need to use line breaks (<br />) if you want your text to show up on different lines. HTML ignores newlines in regards to presentation.
echo "hello, Mr. New Line!\n";
echo "This line should be below 'hello, Mr. New Line!'";
When viewing source, the above two text strings will be on separate lines. When viewed in the browser they will appear to be on the same line.
echo "hello, Mr. New Line!\n<br />";
echo "This line should be below 'hello, Mr. New Line!'";
When viewing source, the above two text strings will be on separate lines because of the \n. When viewed in the browser they will also be on separate lines because of the HTML break <br />.
Use the PHP_EOL constant instead of \n and call it a day.
Also, it's \r\n, not the other way around.
If you are expecting the browser to render new line characters as new lines in HTML, that won't happen. You need to use the <br> tag.