473,414 Members | 1,703 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,414 software developers and data experts.

fread()

Ken
I am trying to read a JavaScript file.
Permissions have been set to 755.

Do I have to open the file first?
fopen($filename, "rb");

Then read it?

My script:

$filename = "http://domainname.com/javascript.js";
echo "File name = ".$filename; This echoes the correct name and path.

$fileopen = fopen($filename, "rb");
echo "File open = ".$fileopen; This echoes Resource id #4
What is resource id #4?
Where do I find a definition of resource id #4?

$contents = fread($fileopen, filesize($fileopen));
echo "File size = ".filesize($filename); This echo is empty.
echo "Contents = ".$contents; This echo is empty.

fclose($handle);

Ken
Jul 17 '05 #1
2 2765

AFAIK you can't request the filesize of a remote file (perhaps in PHP
5+).

Try something like this:

$strURL = 'http://domainname.com/javascript.js';
$strText = '';
$fh = fopen( $strURL, 'r') or die( $php_errormsg );
while( !feof( $fh ) )
{
$strText .= fread( $fh, 1024 );
}
fclose( $fh );
header( 'Content-type: text/plain' );
print $strText;

---
Steve

Jul 17 '05 #2
.oO(Ken)
I am trying to read a JavaScript file.
Permissions have been set to 755.

Do I have to open the file first?
Yes.
$fileopen = fopen($filename, "rb");
echo "File open = ".$fileopen; This echoes Resource id #4
What is resource id #4?
fopen() returns an ID that denotes the currently opened file. It's a
handle for all further actions performed on that file.
Where do I find a definition of resource id #4?
Resource-IDs are a special type in PHP.

http://www.php.net/manual/en/languag...s.resource.php
$contents = fread($fileopen, filesize($fileopen));
You can't use filesize() on remote files in PHP4.
fclose($handle);


This should be

fclose($fileopen);
You could also try this simple code instead:

$filename = 'http://...';
$content = file_get_contents($filename);

http://www.php.net/file_get_contents

HTH
Micha
Jul 17 '05 #3

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

Similar topics

2
by: Luc Holland | last post by:
Hey, I'm working on a program that reads a binary file. It's opened with ==== if ((f1=fopen(argv,"rb"))==NULL) { fprintf(stderr,"Error opening %s for reading . . .\n",argv); exit(2); } ====...
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...
6
by: Patrice Kadionik | last post by:
Hi all, I want to make a brief comparison between read() and fread() (under a Linux OS). 1. Family read and Co: open, close, read, write, ioctl... 2. Family fread and Co: fopen, fclose,...
2
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
8
by: M. Ĺhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)?...
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...
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...
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
3
by: juleigha27 | last post by:
Hi, First off, I want to apologize if this already got posted it seemed like something happened when I tried to post it previously and it didn't work. I am new to file manipulation with c. I...
30
by: empriser | last post by:
How to use fread/fwrite copy a file. When reach file's end, fread return 0, I don't konw how many bytes in buf.
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.