473,395 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem with fread()

Hey all,

I'm a relative newbie to PHP, and am getting some strange results with
the fread() function with PHP (win32) 5.1.2. Here is some code:

<?php
echo $_SERVER['PHP_SELF']."<br>";
echo $_SERVER['DOCUMENT_ROOT'];
$myFile = $_SERVER['DOCUMENT_ROOT']."/Dave/blah.txt";
echo $myfile."<br>";
if (file_exists($myFile)) {
$fh = fopen($myFile, 'r');
// $theData = fread($fh, filesize($my_file));
$theData = fread($fh, 282);
fclose($fh);
echo $theData;
} else {
echo "<br>"." The file";
echo $_SERVER['DOCUMENT_ROOT'];
echo "$myFile does not exist";
}
?>

Now when I run the script using the commented out line for setting
$theData variable, i get a file cannot be 0 bytes message (see error
message at bottom). The file however is not empty. When I replace that
commented line with the line below, I am able to echo $theData
successfuly. I have heard that perhaps there is a bug with fread() in
5.1.x (not sure which rev), but this is causing major pain.

Am I doing something wrong? If this is a bug, is there a simple
alternate I can use?

Thanks heaps!
Warning: fread() [function.fread]: Length parameter must be greater
than 0 in C:\server\wamp\htdocs\dodger\blah.php on line 9

Feb 2 '06 #1
4 2618
Just to add to that. The error message at the bottom says
\dodger\blah.php and the top says \Dave\blah.php but in actual fact
this was a manual edit of the error output. Both are set to
\Dave\blah.php in reality.

Feb 2 '06 #2
Fixed it finally. I found using relative path in setting $myFile
variable was breaking it. An absolute path fixed it.

Feb 2 '06 #3
d
<re*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hey all,

I'm a relative newbie to PHP, and am getting some strange results with
the fread() function with PHP (win32) 5.1.2. Here is some code:

<?php
echo $_SERVER['PHP_SELF']."<br>";
echo $_SERVER['DOCUMENT_ROOT'];
$myFile = $_SERVER['DOCUMENT_ROOT']."/Dave/blah.txt";
echo $myfile."<br>";
if (file_exists($myFile)) {
$fh = fopen($myFile, 'r');
// $theData = fread($fh, filesize($my_file));
$theData = fread($fh, 282);
fclose($fh);
echo $theData;
} else {
echo "<br>"." The file";
echo $_SERVER['DOCUMENT_ROOT'];
echo "$myFile does not exist";
}
?>

Now when I run the script using the commented out line for setting
$theData variable, i get a file cannot be 0 bytes message (see error
message at bottom). The file however is not empty. When I replace that
commented line with the line below, I am able to echo $theData
successfuly. I have heard that perhaps there is a bug with fread() in
5.1.x (not sure which rev), but this is causing major pain.

Am I doing something wrong? If this is a bug, is there a simple
alternate I can use?

Thanks heaps!
Warning: fread() [function.fread]: Length parameter must be greater
than 0 in C:\server\wamp\htdocs\dodger\blah.php on line 9


You're inter-changing $myfile, $myFile and $my_file - that might not help
the situation ;)
Feb 2 '06 #4

<re*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hey all,

I'm a relative newbie to PHP, and am getting some strange results with
the fread() function with PHP (win32) 5.1.2. Here is some code:

<?php
echo $_SERVER['PHP_SELF']."<br>";
echo $_SERVER['DOCUMENT_ROOT'];
$myFile = $_SERVER['DOCUMENT_ROOT']."/Dave/blah.txt";
echo $myfile."<br>";
you might get farther if you concatenate instead of assign. (below) You
will also need to initialize the buffer if you do so. I also suggest using
a slightly larger buffer for performance. between 2K-8K is good, but no more
or you get truncation
..
$theData="";
if (file_exists($myFile)) {
$fh = fopen($myFile, 'r');
// $theData = fread($fh, filesize($my_file));
$theData = fread($fh, 282);
$theData .= fread($fh, 8192);

fclose($fh);
echo $theData;
} else {
echo "<br>"." The file";
echo $_SERVER['DOCUMENT_ROOT'];
echo "$myFile does not exist";
}
?>

Now when I run the script using the commented out line for setting
$theData variable, i get a file cannot be 0 bytes message (see error
message at bottom). The file however is not empty. When I replace that
commented line with the line below, I am able to echo $theData
successfuly. I have heard that perhaps there is a bug with fread() in
5.1.x (not sure which rev), but this is causing major pain.

Am I doing something wrong? If this is a bug, is there a simple
alternate I can use?

Thanks heaps!
Warning: fread() [function.fread]: Length parameter must be greater
than 0 in C:\server\wamp\htdocs\dodger\blah.php on line 9

Feb 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same...
10
by: Alain Lafon | last post by:
Helas, I got something that should be a minor problem, but anyhow it isn't to me right now. A little code fragment: fread(&file_qn, x, 1, fp_q); The corresponding text file looks like...
22
by: Rajshekhar | last post by:
Hi , i am writing a simple prgm to read a .txt file then store the contents into the array... program as follows: -------------------------- #include<stdio.h> int main() { FILE *fp1;
13
by: 010 010 | last post by:
I found this very odd and maybe someone can explain it to me. I was using fread to scan through a binary file and pull bytes out. In the middle of a while loop, for no reason that i could...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
2
by: elisa | last post by:
Dear all, I have problems in writeing and reading a block of data (long array) with fread and fwrite. If I write and read an integer array, everything looks fine, but when I try long array, sth...
3
by: Harry | last post by:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> void scramble(void); struct bmp_header { short int sig; int size_bmp;
24
by: kindrain | last post by:
the code checks whether a.txt has exact the same lines, then write different lines into b.txt Here it is: (before that ,you should creat a.txt) ---------------------- #include<stdio.h>...
9
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.