473,395 Members | 1,730 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.

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 1743
*** 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
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
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
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...

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.