opening a file from a path stored in a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
retrospec
Forum Newbie
Posts: 3
Joined: Wed Mar 19, 2008 11:40 am

opening a file from a path stored in a variable

Post by retrospec »

Hi,

I'm having a bit of problems trying to remember how to go about doing this, but here's my problem:

I need to make a quick process to read files from a directory, open each one up and run through another script that outputs the content of each text file into a particular, readable format. I know that this can be done via making a form. As an ultra simple and quick solution, I'm trying to open a defined list of paths of text files ("essaysrclist.txt"), place each line into an array and run each element through a loop (running the data processing code) until the array list's index is maxed. It's simple enough, but I'm having problems with assigning a path to a variable via a variable. Everything only works when I initialize the path variable with the path as a string.

ex. @$array = "./filepath/file.txt"; // works

vs.

$arraytemp = strval($arraylist[0]); // arraylist[0] is
"./filepath/file.txt"
@ $arraysrc = file($arraytemp); // doesn't work


Can someone please help lead me in the right direction? :)
Thanks ahead of time.


larger example of code below:
*****

//path of essay list stored in variable

$anttest = "essaysrclist.txt";
$anttest = strval($anttest);


//open file list
@ $essaylist = file($anttest);
if (!$essaylist)
{
echo "<p><strong>The Essay file list could not be

opened at this time. Please contact the administrator.

FOR ESSAYLIST</strong></p></body></html>";
exit;
}

//count number of files
$essaycount = count($essaylist);
print "**************************<br />";
print "Number of Essays in List: $essaycount <br />";
print "**************************<br /><br />";

//list essays

print "**************************<br />";
print "Essays to be Checked:<br />";

for ($i=0; $i<$essaycount; $i++)
{

print "$essaylist[$i] <br />";


}

print "**************************<br /><br />";



//open the essay file from list

$essaytemp = strval($essaylist[0]);
$test = strval($test);

//$test = "./testfiles/essaysrc.txt";

@ $essaysrc = file($essaytemp);
//@ $essaysrc = file("./testfiles/essaysrc.txt");



print "+++++++++test variable: $test +++++++++++++ <br

/>";
print "+++++++++essaysrc variable: $essaysrc +++++++++++++

<br />";
print "+++++++++essaytemp variable: $essaytemp

+++++++++++++ <br />";
print "+++++++++essaylist @ index 0 variable:

$essaylist[0] +++++++++++++ <br />";

print "<br><br>";
print "===============<br>";

if (!$essaysrc)
{
echo "<p><strong>The Essay file could not be opened at

this time. Please contact the administrator. FOR

ESSAYSRC</strong></p></body></html>";
exit;
}
www.WeAnswer.IT
Forum Newbie
Posts: 24
Joined: Wed Mar 19, 2008 6:33 pm

Re: opening a file from a path stored in a variable

Post by www.WeAnswer.IT »

Here is some code that should do what you want. I hope I commented it enough for you.

Code: Select all

<html>
    <head>
    </head>
    <body>
<?php
 
// path of essay list stored in variable
$anttest = "essaysrclist.txt";
 
// make sure these vars are initialized
$essayindex = "";
$essaycontent = "";
 
if(!file_exists($anttest))
{
    // the file does not exist
    echo "File not found";  
}
else
{
    // split the contents of $anttest into an array
    $essaylist = file($anttest);
    if(!$essaylist)
    {
        ?>
                <p><strong>The Essay file list could not be opened at this time. Please contact the administrator.</strong></p>
            </body>
        </html>
        <?php
        exit;
    }
}
 
// count number of files
$essaycount = count($essaylist);
 
// in PHP, we use foreach loops to go through arrays
foreach($essaylist as $num => $essay);
{
    // periods are concatenators in PHP
    // they add to a variable, instead of just replacing it
    $essayindex .= $essay."<br />";
    
    $essaycontents .= "<br /><hr />Now showing content in {$essay}:<br /><br /><pre>";
 
    $essay = trim($essay); // get rid of whitespace
 
    // you can use file() instead of file_get_contents
    // to split the lines of the file into an array
    $essaycontents .= file_get_contents($essay);
 
    $essaycontents .= "</pre><br /><br />";
}
?>
 
    <hr />
    Number of Essays in List: $essaycount <br />
    <hr />
 
    <?= $essayindex ?>
 
    <hr />
    <br />
    <br />
    <strong>Essays to be Checked:</strong>
    <br />
    <br />
 
    <?= $essaycontent ?>
 
 
    </body>
</html>
Last edited by www.WeAnswer.IT on Mon Mar 24, 2008 2:33 pm, edited 3 times in total.
retrospec
Forum Newbie
Posts: 3
Joined: Wed Mar 19, 2008 11:40 am

Re: opening a file from a path stored in a variable

Post by retrospec »

Thanks a ton for the reply. However, this solution doesn't directly answer one of my questions (i don't think that I asked it clearly at first). How can you open a file from a variable if the variable is initialized with content from an array or non-direct means?

For example (w/the code I presented):

$arraytemp = strval($arraylist[0]); // arraylist[0] is
"./filepath/file.txt"
@ $arraysrc = file($arraytemp); // doesn't work

"./filepath/file.txt" is a value pulled from a text file with file paths listed in it. When I run print statements to check the value of variables, I get an output of:

**************************
Essays to be Checked: // contents of file paths txt
./testfiles/essaysrc.txt
./testfiles/essaysrc2.txt
./testfiles/essaysrc3.txt
**************************

+++++++++test variable: ./testfiles/essaysrc.txt +++++++++++++
+++++++++essaysrc variable: +++++++++++++
+++++++++essaytemp variable: ./testfiles/essaysrc.txt +++++++++++++
+++++++++essaylist @ index 0 variable: ./testfiles/essaysrc.txt +++++++++++++

This means that $essaylist[0] has the same value as the literal string of the file path I want it to have. Yet, when I try to open a file using that variable (ex. @$anttest = file($essaylist[0]); vs. @$anttest = file('./testfiles/essaysrc.txt');) -- I'm unable to open the file. Please help!!! :)
www.WeAnswer.IT
Forum Newbie
Posts: 24
Joined: Wed Mar 19, 2008 6:33 pm

Re: opening a file from a path stored in a variable

Post by www.WeAnswer.IT »

If you read my code above, I did exactly what you're suggesting. Here's what that code does, in English instead of PHP:

1. Open essaysrclist.txt

2. Split each line of essaysrclist.txt into an array
(At this point, we will have an array, where each element is one line of essaysrclist.txt, which means each element is a file name.)

3. For each element of essaysrclist.txt, open up the file and output the contents of the file.


Read the code I gave you, especially the comments, and try to understand what it's doing. If you have any questions, feel free to ask. Then run my code and see if it gives you the output that you want. If not, let me know what's wrong with it.

(Note: I did notice a logic error in my code, which is now fixed, so try running it again).
retrospec
Forum Newbie
Posts: 3
Joined: Wed Mar 19, 2008 11:40 am

Re: opening a file from a path stored in a variable

Post by retrospec »

Ok. I think that I misread the initial code. Will check and test asap then. Thanks a lot!
Post Reply