karmaB Posted May 1, 2005 Posted May 1, 2005 Hello. I am newly registered here and have been a customer of TCH for a couple years now... I hope someone can help me with my problem. I'm kind of a PHP dunce and what seems to be simple isn't turning out to be so. Here's what I've got: I am trying to use this script I found to include a certain file based on the current day of the month. So, on the 1st it will include file1.txt, the 2nd will include file2.txt, etc. I'm including the script on one of my pages so the designated text file will be shown (ideally) on the page for each day of the month. Here's the code...if you see that I've formatted something incorrectly, please help! --------The Files--------- 1- File that calculates: todaysFile.php 2- Result Page - test.php: <? include('todaysFile.php');?> 3- Files to be included: file1.txt, file2.txt, file3.txt, file4.txt, file5.txt, file6.txt, file7.txt ------------------------------- Here is everything inside todaysFile.php: <?php $totalFiles="7"; // Total files to pull from $todaysFile = Array ( // Up to 31 '1' => 'file1.txt' , // Name of or Path to file '2' => 'file2.txt' , // Name of or Path to file '3' => 'file3.txt' , // Name of or Path to file '4' => 'file4.txt' , // Name of or Path to file '5' => 'file5.txt' , // Name of or Path to file '6' => 'file6.txt' , // Name of or Path to file '7' => 'file7.txt' , // Name of or Path to file ); // Place day of month in variable 'x' $x=date("d"); // If value of date exceeds the amount of files then pick a random file to display if($x > $totalFiles) { $x=rand(1,$totalFiles); } // Display file include("$todaysFile[$x]"); ?> And here is the error on test.php when all is said and done: Warning: main(): Failed opening '' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/blah/public_html/todaysFile.php on line 24 It looks to me like it isn't actually requesting the file to be included...like it can't open a file that isn't named. I know this has got to be something simple but I don't know enough to figure it out. Please help if you can! Thank you so much. L Quote
TCH-Thomas Posted May 1, 2005 Posted May 1, 2005 Welcome to the forum, karmaB. Sorry, don´t know the answer to this but hold on, there are plenty of php gurus here. Quote
surefire Posted May 1, 2005 Posted May 1, 2005 >include("$todaysFile[$x]"); At first glance it appears that you're telling the server to look for a file named $todaysFile[$x] instead of the value of $todaysFile[$x]. I would guess this would work better include($todaysFile[$x]); or include $_SERVER['DOCUMENT_ROOT'].'/'.$todaysFile[$x]; Quote
karmaB Posted May 1, 2005 Author Posted May 1, 2005 (edited) >include("$todaysFile[$x]"); At first glance it appears that you're telling the server to look for a file named $todaysFile[$x] instead of the value of $todaysFile[$x]. I would guess this would work better include($todaysFile[$x]); or include $_SERVER['DOCUMENT_ROOT'].'/'.$todaysFile[$x]; <{POST_SNAPBACK}> Thank you for helping! When I tried include($todaysFile[$x]); the error is the same. When I tried include $_SERVER['DOCUMENT_ROOT'].'/'.$todaysFile[$x]; the error is: Warning: main(/home/blah/public_html/): failed to open stream: Success in /home/blah/public_html/todaysFile.php on line 24 Warning: main(/home/blah/public_html/): failed to open stream: Permission denied in /home/blah/public_html/todaysFile.php on line 24 Warning: main(): Failed opening '/home/blah/public_html/' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/blah/public_html/todaysFile.php on line 24 Edited May 1, 2005 by karmaB Quote
TCH-Don Posted May 1, 2005 Posted May 1, 2005 Why don't you change the include($todaysFile[$x]); to echo($todaysFile[$x]); to see what the variable really is. Quote
karmaB Posted May 1, 2005 Author Posted May 1, 2005 Why don't you change the include($todaysFile[$x]); to echo($todaysFile[$x]); to see what the variable really is. <{POST_SNAPBACK}> Just tried that and it echos nothing. Just a blank space. It's like no file is actually being plugged in there...even in the original error, it said "Failed opening '' for inclusion" like its not actually including anything at all. ?? Mind you, I don't honestly know what I'm talking about. Quote
TCH-Bruce Posted May 1, 2005 Posted May 1, 2005 Welcome to the forums karmaB These guys will help sort you out. Quote
TCH-Tim Posted May 1, 2005 Posted May 1, 2005 date("d") will put a leading '0' on your single digit dates, so today would be 01, not 1. Your array currently looks like this: >$todaysFile = Array ( // Up to 31 '1' => 'file1.txt' , // Name of or Path to file '2' => 'file2.txt' , // Name of or Path to file '3' => 'file3.txt' , // Name of or Path to file '4' => 'file4.txt' , // Name of or Path to file '5' => 'file5.txt' , // Name of or Path to file '6' => 'file6.txt' , // Name of or Path to file '7' => 'file7.txt' , // Name of or Path to file ); Try this: >$todaysFile = Array ( // Up to 31 '01' => 'file1.txt' , // Name of or Path to file '02' => 'file2.txt' , // Name of or Path to file '03' => 'file3.txt' , // Name of or Path to file '04' => 'file4.txt' , // Name of or Path to file '05' => 'file5.txt' , // Name of or Path to file '06' => 'file6.txt' , // Name of or Path to file '07' => 'file7.txt' , // Name of or Path to file ); Or to get the day of the month without the leading 0, use date("j"). More here on PHP date functions. Quote
karmaB Posted May 1, 2005 Author Posted May 1, 2005 date("d") will put a leading '0' on your single digit dates, so today would be 01, not 1. Your array currently looks like this: >$todaysFile = Array ( // Up to 31 '1' => 'file1.txt' , // Name of or Path to file '2' => 'file2.txt' , // Name of or Path to file '3' => 'file3.txt' , // Name of or Path to file '4' => 'file4.txt' , // Name of or Path to file '5' => 'file5.txt' , // Name of or Path to file '6' => 'file6.txt' , // Name of or Path to file '7' => 'file7.txt' , // Name of or Path to file ); Try this: >$todaysFile = Array ( // Up to 31 '01' => 'file1.txt' , // Name of or Path to file '02' => 'file2.txt' , // Name of or Path to file '03' => 'file3.txt' , // Name of or Path to file '04' => 'file4.txt' , // Name of or Path to file '05' => 'file5.txt' , // Name of or Path to file '06' => 'file6.txt' , // Name of or Path to file '07' => 'file7.txt' , // Name of or Path to file ); Or to get the day of the month without the leading 0, use date("j"). More here on PHP date functions. <{POST_SNAPBACK}> YEEEHAAAWWWWW! Thank you guys soooooo much!!! The 0 did the trick. The stupid thing is I was going to try that last night when I was so danged frustrated but got totally sidetracked with another thought. Thank you thank you thank you! L Quote
TCH-Don Posted May 1, 2005 Posted May 1, 2005 Good catch Tim oh yes Welcome to the family KarmaB Don't be a stranger Quote
karmaB Posted May 1, 2005 Author Posted May 1, 2005 ...and thank you for the friendly welcomes too... Quote
Head Guru Posted May 1, 2005 Posted May 1, 2005 This forum amazes me, time and time again. Thank you to everyone for making the forums so special. Bill Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.