I haven't done PHP for a while so I apologize in advance if it's something simple.  I'm migrating a PHP-based site to a shared web hosting and there's an issue with the way it outputs HTML markup. Let's consider the following code snippet that reads HTML from a file on the server and outputs it to the web browser:  $handle = @fopen($file_name, "rb");  $str = @fread($handle, filesize($file_name)); fclose($handle);  print $str;   For some reason it replaces some characters on the page. Like it puts that character instead of an apostrophe:    while it looked normal when the exact same code ran from a previous web hosting server:    Any idea what am I missing here?
326
I'm setting up a new server and want to support UTF-8 fully in my web application. I have tried this in the past on existing servers and always seem to end up having to fall back to ISO-8859-1.  Where exactly do I need to set the encoding/charsets? I'm aware that I need to configure Apache, MySQL, and PHP to do this — is there some standard checklist I can follow, or perhaps troubleshoot where the mismatches occur?  This is for a new Linux server, running MySQL 5, PHP, 5 and Apache 2.
54053
Why do PHP regexes have the surrounding delimiters? It seems like it would be more clear if any pattern modifiers were passed in as a parameter to whatever function was being used.
54171
I have a stubborn folder that someone must have created accidentally and that has persisted all this time. I can see it with:  # find /var/www/html -maxdepth 1 -type d   which gives me:  /var/www/html /var/www/html/files /var/www/html/.?   unfortunately the question mark there is not the actual character ?, it merely seems to denote that there is no proper glyph and/or translation for that character. So something like rmdir is out of the question. My system has UTF-8 as the default locale as so many modern Ubuntu systems, but admittedly the folder in question may have originated on a RHEL or CentOS box (tared up and unpacked).  # env|grep LANG LANG=en_US.UTF-8   When trying to cd .\? (as mentioned before it is not actually ?) I get auto-corrected to . by bash and actually end up in /var/www/html/. for some reason. Now if I can't "name" this folder usefully, I cannot access it in any way. How can I get rid of it without putting the whole system in jeopardy?  Now I installed detox already but that only seems to act on files, not folders, according to its documentation and besides it still requires that I be able to name the object somehow to do something with it.
116031
I'm trying to write some PHP to upload a file to a folder on my webserver. Here's what I have:  &lt;?php     if ( !empty($_FILES['file']['tmp_name']) ) {         move_uploaded_file($_FILES['file']['tmp_name'], './' . $_FILES['file']['name']);         header('Location: http://www.mywebsite.com/dump/');         exit;     } ?&gt;  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"     "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt; &lt;html&gt;     &lt;head&gt;         &lt;title&gt;Dump Upload&lt;/title&gt;     &lt;/head&gt;     &lt;body&gt;         &lt;h1&gt;Upload a File&lt;/h1&gt;         &lt;form action="upload.php" enctype="multipart/form-data" method="post"&gt;             &lt;input type="hidden" name="MAX_FILE_SIZE" value="1000000000" /&gt;             Select the File:&lt;br /&gt;&lt;input type="file" name="file" /&gt;&lt;br /&gt;             &lt;input type="submit" value="Upload" /&gt;         &lt;/form&gt;     &lt;/body&gt; &lt;/html&gt;   I'm getting these errors:     Warning: move_uploaded_file(./test.txt) [function.move-uploaded-file]: failed to open stream: Permission denied in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 3      Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\Temp\phpA30E.tmp' to './test.txt' in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 3      Warning: Cannot modify header information - headers already sent by (output started at E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php:3) in E:\inetpub\vhosts\mywebsite.com\httpdocs\dump\upload.php on line 4   PHP version 4.4.7 Running IIS on a Windows box. This particular file/folder has 777 permissions.  Any ideas?
58463
Does anyone know of an easy way to escape HTML from strings in ?  I need to be able to pass an arbitrary string and have it properly escaped for display in an HTML page (preventing JavaScript/HTML injection attacks).  I'm sure it's possible to extend jQuery to do this, but I don't know enough about the framework at the moment to accomplish this.
83249
Very similar to , except for Java.  What is the recommended way of encoding strings for an XML output in Java. The strings might contain characters like "&amp;", "&lt;", etc.
89834
I have:  &lt;?php  $file=fopen(date("Y-m-d").".txt","r+") or exit("Unable to open file!");  if ($_POST["lastname"] &lt;&gt; "") {    fwrite($file,$_POST["lastname"]."\n"); }  fclose($file);  ?&gt;   but it overwrites the beginning of the file.  How do I make it insert?
82526
Some compilers (especially C or C++ ones) give you warnings about:  No new line at end of file   I thought this would be a C-programmers-only problem, but github displays a message in the commit view:  \ No newline at end of file   for a PHP file.  I understand the preprocessor thing explained in , but what has this to do with PHP? Is it the same include() thing or is it related to the \r\n vs \n topic?   What is the point in having a new line at the end of a file?
4791
Say i have this PHP code:  $FooBar = "a string";   i then need a function like this:  print_var_name($FooBar);   which prints:  FooBar   Any Ideas how to achieve this? Is this even possible in PHP?
29690
I'd like to capture the output of  to a string.  The PHP documentation says;     As with anything that outputs its result directly to the browser, the  can be used to capture the output of this function, and save it in a string (for example).   What would be an example of how that might work?  print_r() isn't a valid possibility, because it's not going to give me the information that I need.
66288
Background  I am writing and using a very simple CGI-based (Perl) content management tool for two pro-bono websites. It provides the website administrator with HTML forms for events where they fill the fields (date, place, title, description, links, etc.) and save it. On that form I allow the administrator to upload an image related to the event. On the HTML page displaying the form, I am also showing a preview of the picture uploaded (HTML img tag).  The Problem  The problem happens when the administrator wants to change the picture. He would just have to hit the "browse" button, pick a new picture and press ok. And this works fine.  Once the image is uploaded, my back-end CGI handles the upload and reloads the form properly.  The problem is that the image shown does not get refreshed. The old image is still shown, even though the database holds the right image. I have narrowed it down to the fact that the IMAGE IS CACHED in the web browser. If the administrator hits the RELOAD button in Firefox/Explorer/Safari, everything gets refreshed fine and the new image just appears.  My Solution - Not Working  I am trying to control the cache by writing a HTTP Expires instruction with a date very far in the past.  Expires: Mon, 15 Sep 2003 1:00:00 GMT   Remember that I am on the administrative side and I don't really care if the pages takes a little longer to load because they are always expired.  But, this does not work either.  Notes  When uploading an image, its filename is not kept in the database. It is renamed as Image.jpg (to simply things out when using it). When replacing the existing image with a new one, the name doesn't change either. Just the content of the image file changes.  The webserver is provided by the hosting service/ISP. It uses Apache.  Question  Is there a way to force the web browser to NOT cache things from this page, not even images?  I am juggling with the option to actually "save the filename" with the database. This way, if the image is changed, the src of the IMG tag will also change. However, this requires a lot of changes throughout the site and I rather not do it if I have a better solution. Also, this will still not work if the new image uploaded has the same name (say the image is photoshopped a bit and re-uploaded).
96773
I was just browsing a forum and someone asked about a PHP file they had found on the web.  It has several spots like this in the code:  if ($REMOTE_ADDR == "") $ip = "no ip"; else $ip = getHostByAddr($REMOTE_ADDR);  I have always thought brackets are needed to enclose what you want to do if the condition is true.  Is there some other alternative, such as if it is on the same line you don't?  There is also another line like this: if ($action != ""): mail("$adminaddress","Visitor Comment from YOUR SITE",  My instinct is to say this wouldn't work, but I also don't know if it is an outdated PHP file and it used to work?
7977
I'm in the process of making my PHP site Unicode-aware. I'm wondering if anyone has experience with the mbstring.func_overload setting, which replaces the normal string functions (e.g. strlen) with their multi-byte equivalents (mb_strlen). There aren't any comments on the PHP manual page.  Are there any potential problems that I should be aware of? Any cases where calling the multi-byte version is a bad idea?  I suppose one example would be functions that deal with encryption, since they may expect to deal with strings of bytes, rather than strings of characters.  Also, the manual page includes a note: "It is not recommended to use the function overloading option in the per-directory context, because it's not confirmed yet to be stable enough in a production environment and may lead to undefined behaviour."  Does that mean that it's not stable in a per-directory context, or it's generally not stable? The wording is unclear.
57618
foreach((get_the_category()) as $category) {     echo $category-&gt;cat_name . ', ';  }   So I'm echoing it like this because I don't want the categories to appear as hyperlinks, I just want the cat names.  The problem is I can't get rid of the last separator so it ends up like : category, category, category,  I tried 'stripping' it (php seriously confuses me small design mind)  foreach((get_the_category()) as $category) {     echo $category-&gt;cat_name . ', ';     echo rtrim($category, ", "); }   That didn't trim it though.
75495
Apparently a lot of people complain that they only see random letters and characters:    My biggest problem is that I can't reproduce the problem on ANY of my devices! Not on my Windows XP laptop, not on my Windows 7 laptop, not on my Android phone or my iPod Touch. It doesn't matter which browser I'm using. The only time I see the problem myself is when I try to share one of my blog posts on Google+, because the blog description then appears in strange characters.  So, first of all I need to find a way to reproduce the problem somehow. Otherwise I won't be able to see if what I did fixed the problem or not.  I did everything that @toscho recommended, but nothing helped.  Did anybody ever run into a similar problem or would you know where I could start to look for the problem?  Edit: I contacted my webhost. They couldn't reproduce the problem, just like me. They said that they have enabled gzip compression on their servers. They suggested turning off one plugin after another to see what happens, but as I can't reproduce the problem, I can't do that. Please help!  More and more readers are complaining about this problem. Most of them just need to refresh the page or wait a few minutes and then the site loads normally, but I'm still using a lot of traffic because of that.  EDIT 2: The W3 Total Cache Plugin FAQ says: I see garbage characters instead of the normal web site, what's going on here?  If a theme or it's files use the call php_flush() or function flush() that will interfere with the plugins normal operation; making the plugin send cached files before essential operations have finished. The flush() call is no longer necessary and should be removed.  I am pretty sure that none of my code has this call anywhere, but I'm not 100% sure. Is there any way to find out other than searching through millions of characters in all of my files?
73904
When is it a good idea to use ?  I sometimes see this in code samples of PHP. Does this handle DOS/Mac/Unix endline issues?
38314
touch ~/deleted/$(echo "directory_"$(readlink -f foo)|tr '/' '\') 2&gt; /dev/null   where foo is a directory  It's reading the full pathname of foo and creating it as a file replacing all '/' with '\' and putting a directory_ in front so the directory  /home/test/foo would create a file directory_\home\test\foo  It does exactly what I want except a warning keeps printing tr: warning: an unescaped backslash at end of string is not portable   a. I don't know what that means b. I would like it to not show  I tried to redirect stderr to /dev/null but it doesn't work. How do I suppress it?