473,469 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Use file() function to read a variable name, not a literal name.

I know I can use the file() function to read a file into an array by
providing a literal file name between single quotes:

$lines = file('literal_file_name');

But, what if I want file() to use a variable name?

$variable_file_name = "literal_file_name";
$lines = file($variable_file_name);

Of course, that example won't work. But what will?

Aug 2 '06 #1
3 1747
*** walterbyrd escribió/wrote (2 Aug 2006 10:49:28 -0700):
I know I can use the file() function to read a file into an array by
providing a literal file name between single quotes:

$lines = file('literal_file_name');

But, what if I want file() to use a variable name?
From manual:

array file ( string filename [, int use_include_path [, resource context]]
)

You need to provide a string. Period. PHP doesn't know/care how you create
the string.

$variable_file_name = "literal_file_name";
$lines = file($variable_file_name);

Of course, that example won't work. But what will?
What error are you getting?
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Aug 2 '06 #2

Alvaro G. Vicario wrote:
What error are you getting?
This is what I'm using. It is copied almost verbatim from the php.net
examples for using the file() function. One difference: I'm using a
variable ($userfile), instead of a literal. The variable does contain a
file name, I've checked. If I replace the variable, with a literal,
this will work.

// Get a file into an array. In this example we'll go through HTTP to
get
// the HTML source of a URL.
$lines = file($userfile);

// Loop through our array, show HTML source as HTML source; and line
numbers too.
foreach ($lines as $line_num =$line) {
echo "Line #<b>{$line_num}</b: " . htmlspecialchars($line) . "<br
/>\n";
};

Here is the error that I'm getting:

Warning: Invalid argument supplied for foreach() . . .

Aug 2 '06 #3

Alvaro G. Vicario wrote:
What error are you getting?
Opps. I'm sorry to have wasted your time. It was just a syntax error.
I've fixed it.

*sigh* I've been away from programming for too long.

Aug 2 '06 #4

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
4
by: Frank | last post by:
Could someone tell me how to open a file at run time that I didn't know the name of at compile time? I know how to open a file at compile time when I know what the name is going to be. FILE...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
0
by: Fabrice | last post by:
Hello, (Alain) Tis is a part of my code to retrieve text from hastable in memory cache, by reading (befor) a resources file. Thanks for your help. /1/ The resources file * I have create a...
1
by: walterbyrd | last post by:
I know I can use the file() function to read a file into an array by providing a literal file name between single quotes: $lines = file('literal_file_name'); But, what if I want file() to use...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
3
by: sam_cit | last post by:
Hi Everyone, I have the following function, char* sample1() { char *p = "india"; return(p); }
15
by: arnuld | last post by:
This is the partial-program i wrote, as usual, i ran into problems halfway: /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a function to open a file for input and then read...
2
by: Fabian Braennstroem | last post by:
Hi, me again :-) I would like to parse a small batch file: file/read-case kepstop.cas file/read-data keps1500.dat solve/monitors/residual/plot no solve/monitors/residual/print yes
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...
1
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.