Here is my situation.Supposing I have a PHP file named as:
myfunctions.php
Is it possible to read the contents of that PHP file (myfunctions.php) in another script using available PHP functions?
I'm using the following functions without success in reading this PHP file:
file_get_contents()
file()
It simply returned blank. One of the possible successful method was to read this PHP file as text file but I do not know how to do this..If someone has some other methods, please share. Ideally I want the output to be an array so the data I would like to obtain will be easily manipulated for use.
Thanks.
If I were you, I will change the extension on the "myfunctions.php" to "myfunctions.txt" and use the theses functions
file_get_contents()
file()
But I don't know if you are allow to change the name of the document.
file() - Reads entire file into an array
fgets() - Gets line from file pointer
fread() - Binary-safe file read
readfile() - Outputs a file
file_put_contents() - Write a string to a file
stream_get_contents() - Reads remainder of a stream into a string
stream_context_create() - Creates a stream context
You need fread. To put it into an array, you might look at explode
Related
This question already has answers here:
Difference between file, file_get_contents, and fopen in PHP
(2 answers)
Closed 5 years ago.
What's the difference between fopen('file.txt', 'r') and file('file.txt')? They both appear to be the same...
Here's some info. Quote on file(), file_get_contents(), and fopen():
The first two, file and file_get_contents are very
similar. They both read an entire file, but file reads the file into
an array, while file_get_contents reads it into a string. The array
returned by file will be separated by newline, but each element will
still have the terminating newline attached, so you will still need to
watch out for that.
The fopen function does something entirely different—it opens a
file descriptor, which functions as a stream to read or write the
file. It is a much lower-level function, a simple wrapper around the C
fopen function, and simply calling fopen won't do anything but
open a stream.
Once you've open a handle to the file, you can use other functions
like fread and fwrite to manipulate the data the handle
refers to, and once you're done, you will need to close the stream by
using fclose. These give you much finer control over the file
you are reading, and if you need raw binary data, you may need to use
them, but usually you can stick with the higher-level functions.
So, to recap:
file — Reads entire file contents into an array of lines.
file_get_contents — Reads entire file contents into a string.
fopen — Opens a file handle that can be manipulated with other library functions, but does no reading or writing itself.
Credit goes to Alexis King.
I am new to PHP, and I am not quite sure: what is the difference between the file(), file_get_contents(), and fopen() functions, and when should I use one over the other?
The first two, file and file_get_contents are very similar. They both read an entire file, but file reads the file into an array, while file_get_contents reads it into a string. The array returned by file will be separated by newline, but each element will still have the terminating newline attached, so you will still need to watch out for that.
The fopen function does something entirely different—it opens a file descriptor, which functions as a stream to read or write the file. It is a much lower-level function, a simple wrapper around the C fopen function, and simply calling fopen won't do anything but open a stream.
Once you've open a handle to the file, you can use other functions like fread and fwrite to manipulate the data the handle refers to, and once you're done, you will need to close the stream by using fclose. These give you much finer control over the file you are reading, and if you need raw binary data, you may need to use them, but usually you can stick with the higher-level functions.
So, to recap:
file — Reads entire file contents into an array of lines.
file_get_contents — Reads entire file contents into a string.
fopen — Opens a file handle that can be manipulated with other library functions, but does no reading or writing itself.
file — Reads entire file into an array
file_get_contents — Reads entire file into a string
fopen — Opens file or URL
I'm planning to run a php program from Mac Terminal. I have a folder on my desktop with around 800 .csv files and I need to write a php program that reads through and reads each one so that I can run some transformations on the data it's storing. I know how to parse the .csv once it's loaded but I'm wondering if there is a way to load each file without having to name it explicitly? I don't have a list of the 800 file names but I feel like there has to be a way to just read in all the files in a folder in a loop or something without having the title of each file listed -- I don't have much coding experience, so forgive me if there's an obvious answer of which I'm oblivious.
Thank you!
There are a few way todo this but glob'ing is very straightforward:
<?php
foreach (glob("*.csv") as $filename) {
//do somthing
}
?>
You can loop through all files in a directory using readdir() :http://php.net/manual/en/function.readdir.php.
Once you get the file name using readdir() you can parse it by either breaking the file content into an array and working with the cells by looping through the array using str_getcsv() (requires at least phpv5.3) or the old fashion fgetcsv() to read through the file one line at a time. For each file create a string variable, and after line you read through and transform, simply append the modified line to this string with an end-of-line character appended as well. After reading through the entire file, simply replace the file contents of the original with file_put_contents()
I have a PHP script that generates a dynamic PHP sitemap from my site's database to an xml file using fopen() and fwrite().
How can I compress this file using GZ compression dynamically as I write it?
I tried fwrite()-ing strings that I ran through gzcompress() into the file and renaming it ".xml.gz", but it doesn't seem the file it;s creating is a well-fromed archive.
Not using fopen and fwrite but gzopen() and gzwrite() should do the trick for you.
From the manual:
# Sample #1 gzwrite() example
<?php
$string = 'Some information to compress';
$gz = gzopen('somefile.gz','w9');
gzwrite($gz, $string);
gzclose($gz);
?>
If i understood correctly
this is a quote from the php site that we all should keep in mind.
Take Heed 07-Nov-2010 08:50 Read the description of gzwrite() very
carefully. If the 'length' option is not specified, then the input
data will have slashes stripped on systems where magic quotes are
enabled. This is important information to know when compressing
files.
http://php.net/manual/en/function.gzwrite.php
I have a file named "connection.php". I want to read the contents of this file to a string. I use fopen, and read functions for reading. But when I am reading I just got only last 2-3 lines on that file. That means no PHP scripts can read like echo, functions etc. How can I read the whole contents on that file?
<?php
$str = file_get_contents('connection.php');
var_dump($str);
?>
note that if 'connection.php' contains '<?php' at the beginning, and you try to output it to a browser, you likely won't see anything unless you perform a "View Source".
Quoting the manual page of fread :
fread() reads up to length bytes from
the file pointer referenced by handle
. Reading stops as soon as one of the
following conditions is met:
length bytes have been read
EOF (end of file) is reached
a packet becomes available (for network streams)
8192 bytes have been read (after opening userspace stream)
If you want to read a whole file, you'll need to use some kind of loop, to read data until you reach the end of the file.
Or, as an alternate (probably easier), you can use file_get_contents, which will get you the whole content of the file with only one function call.
Which means no need for fopen + multiple fread + fclose ;-)
Perhaps your browser is hiding the content because it starts with '<?php'. You can try View Source in your web browser, or echo the contents in the following way:
<?php
$contents = file_get_contents('connection.php');
echo "<pre>";
echo htmlentities($contents);
echo "</pre>";