Problems reading a text file with PHP

Posted on 2011-02-11
Last Modified: 2013-12-13
I'm new to PHP and keep running into issues when I expect it to respond like C/C++ and it doesn't.

In this case I want to read a text file from the server and display to a web page while maintaining the formatting. Here's my code:

    if (file_exists($reqDescrFile)) {
      if ($fp = fopen($reqDescrFile, 'r')) {
        $reqDescr = fread($fp, filesize($reqDescrFile));
        $reqDescr2 = nl2br (htmlspecialchars ($reqDescr));
        echo $reqDescr2;

The problem is that fread doesn't appear to read ascii chars as I'm used to. When encountering quotes (') or (") it displays a black-diamond-? to the web page. The htmlspecialchars has no affect.

Text file:  It's more than
Web page output:   It¿s more than
Question by:jimdgar2
  • 4
  • 3
LVL 83

Accepted Solution

Dave Baldwin earned 500 total points
ID: 34873229
That usually means it's not a plain ASCII character.  The Windows single and double quote pairs are not ASCII but I think part of Windows 1252 character set.  What character set is your page and PHP file using?

Author Comment

ID: 34873255
I don't know. Is this where it's set?

    <meta charset="utf-8">

LVL 83

Expert Comment

by:Dave Baldwin
ID: 34873359
Yes, that's for the web page but it's an incomplete declaration.  The correct line (taken from this page that you're reading) is below.  You also have to set it separately for PHP sometimes.  Here's a page about character encoding in PHP:
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

Open in new window

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center


Author Comment

ID: 34873902
Nope, neither the full line nor the page on encoding help.
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34873956
I would bring the text file into a text editor that can change the character set like PsPAD to see what's going on.

Author Comment

ID: 34877079
Took me quite a while to figure this out. I created the .txt file using Notepad, which saves the text file in ansi_x3.110-1983 format, which is apparently done for backward compatibility issues. By opening the file with Wordpad and then saving as a text document in MS-DOS format, the PHP code in the browser is then able to read the file correctly. Still don't quite get it but it works.
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34877090
Thanks for the points.  I run into this kind of thing all the time with 'text' that people copy and paste from Microsoft Word which includes some characters only found in the standard Windows character set.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Installer 5 37
Eloquent ORM manual paginator defaults to simple 2 31
What's wrong with this PDO query? 5 27
PHP 5.6 and 7.x 4 18
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question