Can't get fopen to open file - php

I can't tell you how stupid I feel having to ask this question, but I've been working on the most simple of commands (for two days) and can't find the problem. I 'borrowed' some code for a non repeating hit counter. I have tried to get it to work and I finally determined I'm not getting access to the simple txt files that store the hits or the one that stores the ip addresses. I've read the problems here, looked at the command in a 'Dummies' book and even watched YouTube videos and I'm blind to the problem. I've tried using a string for the file name and using the filename directly. I had the files in a sub folder on the server and thought that might be the issue so I moved them to the root with the same error. If someone can see why this isn't working I'd be eternally grateful.
This is only part of the whole code but it's where I determined that it fails.
$filename = 'countfix.txt';
$handle = fopen('$filename', 'r');
fread($handle, $current_inc)
or die ("Can't open file");
echo $current_inc;
fclose($handle);
Thanks.

This is wrong:
$handle = fopen('$filename', 'r'); // tries to open a file named $filename
It should be written this way:
$handle = fopen($filename, 'r'); // no quotes, opens countfix.txt
You might have meant to write this instead:
$handle = fopen("$filename", 'r');
wherein the double quotes will cause the real value of $filename to be substituted into the string (thus making the code work), but there is no point in doing that. Lose the quotes.
Additionally, this code doesn't do what it says:
fread($handle, $current_inc) or die ("Can't open file");
Here the error message is printed if you cannot read from the file, not when you fail to open it. You should check the return value of fopen instead or modify the message to be more accurate.

This is the right way to do it:
$handle = fopen("$filename", 'r');
You must enclose variables with doubble quotes, or not enclose them at all! This is how PHP is.
You might want to read this: what is the difference between single quoted and double quoted strings in php

Related

How insert a string before last character in a large file

I have a very large file that consists of a single string. Because of the size of the file, I do not want to read the entire string into memory.
The last character will always be a closing bracket ] as this string is a json array. I want to insert a small json object (represented as a string) immediately before that closing bracket. I have seen a few ideas, but cannot get anything to work.
As you can see, I am trying to open the file and use fseek to move the file pointer to just in front of the ]. Then I try to write the new string into the existing string at that position.
However, the effect of this is simply to append the new string to the end of the existing string, which is not what I want.
As a simplified example, let's say the file starts out containing this string:
[{"name":"alice","city":"london"}]
And then I want to add a second person to this list using this code:
$new_person = ",{\"name\":\"bob\",\"city\":\"paris\"}";
$filename = "people.json";
$fh = fopen($filename, "a+");
$filesize = filesize($filename);
$stat = fstat($fh);
fseek($fh, $stat[$filesize]-1);
fwrite($fh, $new_person);
fclose($fh);
But what I wind up with is a file that contains this string:
[{"name":"alice","city":"london"}],{"name":"bob","city":"paris"}
My PHP skills are terrible. I can't tell if my fseek is pointing to the wrong spot or if the issue is elsewhere. Thanks for any help.
From the docs (emphasis mine):
a+: Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.
Use r+ mode instead, and instead of fstat you can do:
fseek($fh, -1, SEEK_END);
Please try the following code to get your solution, i have tested and its work fine...
try{
$new_person = ",{\"name\":\"bob\",\"city\":\"paris\"}]";
$filename = "people.json";
$fh = fopen($filename, "a+");
$stat = fstat($fh);
ftruncate($fh, $stat['size'] - 2);
fwrite($fh, $new_person);
fclose($fh);
}catch(Exception $exc){
echo($exc->getMessage());
}

fopen mode w+ and reading the file?

I'm going through all the modes and trying to understand where can you apply some of them, and I stumbled upon w. Now I understand how that mode works, but what's the purpose of w+? Obviously if we use fopen right after opening the file, it won't work since the file will be truncated, so I assume it's good to write something in the file and then read from it later on, so I decided to test it like so:
$handle = fopen('new.txt', 'w+');
fwrite($handle, '123');
$file = fread($handle, filesize('new.txt'));
var_dump($file); // string '' (length=0)
fclose($handle);
For some reason it doesn't read the contains of the file later.
Could someone explain to me please where could it applicable, and what is wrong with my code? filesize returns 3, so it doesn't delete the contents of the file, I assume that the $handle might be used from the moment we opened the file and it doesn't update? Then it confuses me even more why would we use mode w+ and doing reading from a file at the same time?
Edit: I can understand even how I can write something in the file with r+ and then check what I've written with using fopen again, like so:
$handle = fopen('new.txt', 'r+');
fwrite($handle, '123');
$handle = fopen('new.txt', 'r+');
$file = fread($handle, filesize('new.txt'));
var_dump($file);
fclose($handle);
If I would do the same with w+, it would just delete the contents obviously, and I would not be able to read anything?
fopen() with w+ option opens the file for read/write at the beginning and truncates the length to 0. It's essentially the same as writing to a new empty file.
See fopen() usage here: http://php.net/manual/en/function.fopen.php
w+ is mainly used to "read back" what you've written. In your case you are using fread() function which reads "forward" from your pointer which is currently sitting at the end of the file after doing fwrite(), which would explain why you are getting empty contents.
I hope this answers your question.
#Ye. is right about the pointer. Once you have executed fwrite, then the pointer will be at the end of the file. What you need is fseek() to go back to the beginning of the file.
$handle = fopen('new.txt', 'w+');
fwrite($handle, '123');
fseek($handle, 0);
$file = fread($handle, filesize('new.txt'));
var_dump($file); // string '123' (length=3)
fclose($handle);

Writing large string to text file

A trivial use of PHP and frwite() to create/write to a text file.
However, is there a way to write a very large text string to a file using fwrite?()? I assume there is, and that it involves some form of buffer management. The PHP docs don't seem to have this covered.
Sample code:
$p = "Some really large string ~ 100-250K in size"
$myFile = "testp.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
set_file_buffer($fh, 1000000);
fwrite($fh, $p);
fclose($fh);
Believe it or not, this simply gets a file with the name of the file inside the file.
Using a much smaller text string, it works as expected. Pointers to what I should do would be useful.
UPDATE:
Some of you are missing that I did try the above with a string of ~100K, and it didn't work. All I got in the output file was the name of the file!!!
thanks
::: 2ND UPDATE....
never mind.. the whole thing was user error... god i need a drink... or sleep!
thanks
php/fwrite works as i thought it would/should.. nothing to see here..!
There is no limit on how much data can be written to a stream (a file handle) in PHP and you do not need to fiddle with any buffers. Just write the data to the stream, done.

fgetcsv doesn't validate whether or not this is a csv file

This question has been asked several times, but as it turns out all of the answers I have come across have been wrong.
I'm having a problem validating whether a file is a CSV or not. Users upload a file, and the application checks to see if fgetcsv works in order to make sure it's a CSV and not an Excel file or something else. That has been the traditional answer I'm finding via Google.
e.g.:
if ($form->file->receive()) {
$fileName = $form->file->getFileName();
$handle = fopen($fileName, 'r'); // or 'w', 'a'
if (fgetcsv($handle)) {
die('sos yer face');
}
if ($PHPExcelReader->canRead($fileName)) {
die('that\'s what she said');
}
}
What happens with the above is 'sos yer face' because fgetcsv validates as true no matter what you give it if your handle comes from fopen($fileName, 'r') as long as there is a file to read; and fgetcsv always false when using fopen($fileName, 'w') or 'a' because the pointer will be initiated at the EOF. This is according to the php.net documentation.
Maybe what I'm saying is ridiculous and I just don't realize it. Can anyone please fix my brain.
The problem with validating whether or not a file is a CSV file is that CSV is not a well-defined format. In fact, by most definitions of CSV, just about any file would be a valid CSV, treating each line as a row with a single column.
You'll need to come up with your own validation routine specific to the domain you are using it in.

Inexplicable linebreaks in xml file that cannot be removed

So I have a sql database that generates an xml file. This xml file gets put on a webserver and then parsed by a php file. Unfortunately randomly sometimes there will be a line break in the xml file that causes it to be unable to be parsed.
http://imgur.com/jNgiE
As you can see from line 12-14. There is a linebreak. But I have no idea why. And I even wrote a script to remove carriage returns and newline characters but the linebreaks still remain. Anyone have any ideas?
$inputXML = file_get_contents("ukso.xml");
$fixedXML = str_replace("\r","",$inputXML);
$fixedXML = str_replace("\n","",$inputXML);
$fixedXML = str_replace(" ","",$inputXML);
$myFile = "ukso.xml";
print $fixedXML;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $fixedXML);
fclose($fh);
Your code to remove linebreaks is broken. make the following changes:
$inputXML = file_get_contents("ukso.xml");
$fixedXML = str_replace("\r","",$inputXML);
$fixedXML = str_replace("\n","",$fixedXML); // note: reference the correct variable here
$fixedXML = str_replace(" ","",$fixedXML); // and here.
$myFile = "ukso.xml";
print $fixedXML;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $fixedXML);
fclose($fh);
The code as you have it is only trimming out double spaces.
I see you're using notepad++
Go into view-show symbol-show all characters. That will tell you exactly what characters there are in the file.
It's possible that the line breaks are also due to the line being too long otherwise. XML files are usually designed to have line breaks at the end of tags, and you may be running into a situation where the line length is too long somewhere in one of the programs. (Often they create a fixed buffer and read in as much as possible, and if it's too long the program will chop the line)

Categories