Jump to content

Recommended Posts

I have a routine where a user can put text files into a folder and the code will display the name of each file (minus the .txt extension) with the contents of the file below it. The PHP page resides in one folder, while the text files sit in a subfolder below that:

 

<?php

echo "<center>";
echo "<font size=\"5\" color=\"#000000\" face=\"times new roman\"><b>Page Title</b></font>";
echo "</center>";
echo "<center>";
echo "<font size=\"8\" color=\"#000000\" face=\"times new roman\"><b>Bid Requests</b></font>";

echo "<BR>";
echo "<BR>";

$extension = '.txt';
$blank = '';

echo "<font size=\"3\" color=\"#000000\" face=\"arial\">The following bid requests are open at this time:</font>";
echo "<BR>";
echo "<BR>";
echo "<font size=\"3\" color=\"#000000\" face=\"arial\">";
if ($handle = opendir('./bidfolder/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") 

{
echo "<center>";
echo "<b>";
echo htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file)));

echo "</b>";
echo "<BR><BR>";

readfile($file);

echo "<BR><BR>";
echo "<img src='http://www.website/images/bidbar.jpg'>";
echo "<BR>";
echo "<BR>";
echo "</center>";
}
}
closedir($handle);
echo "</font>";
}
?>

 

The opendir function is pulling filenames from the folder, "bidfolder," as it should, BUT....the readfile function is looking in the root for file contents. I have to also have each text file in the root folder (mmbids) or it errors.

 

In other words, how do I make the readfile function point to the "bidfolder" subfolder?

 

Link to comment
https://forums.phpfreaks.com/topic/57399-please-help-me-specify-a-folder/
Share on other sites

try this:

readfile($handle.$file);

 

No go. It gives me....

 

[function.readfile]: failed to open stream: No such file or directory in C:\user\WWWRoot\MMBids\bidrequest.php on line 45

 

It's working like it is, but I don't want the user to have to save the text files in both the mmbids folder and the bidfolder folder. It's pulling the file list from bidfolder but trying to read the text of the file from mmbids, where it doesn't exist. I need to redirect the "readfile" part to the bidflder folder.

 

Make sense?

 

sorry I think I did that wrong ::)

readfile('./bidfolder/' . $file);

That should work, although you need to change this line as well

echo htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file)));

to

echo htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', './bidfolder/' . $file)));

sorry I think I did that wrong ::)

readfile('./bidfolder/' . $file);

That should work, although you need to change this line as well

echo htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file)));

to

echo htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', './bidfolder/' . $file)));

 

1. It works

 

2. You're awesome!    ;D

 

I don't suppose you would also know an easy way to make it display them sorted by file date/time (newest on top) instead of alphabetically?

 

:)

 

hmm.. not really.

To get the last modified date of a file though use this:

$modified = filemtime('./bidfolder/' . $file);

That will give you a time you can easily use with the date() function.

 

I see. Yeah, that lists the date/time code for each file when I echo it.

 

Anybody know how I can make it sort by this date/time code with newest on top?

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.