Forum Moderators: coopster
$allfiles = Array();
$allfiles[1] = "afile.txt";
$allfiles[2] = "thatfile.html";
$allfiles[3] = "thatfile.gif";
The reason I'm trying to change it is because I need to be able to name the files anything, without numbers on them so putting the number in the array instead.
When I look up for each in the manual, [us.php.net...] it shows it working on arrays like the just above. But when I insert it below in the script, the script won't work anymore. Maybe it needs to call the numbers in the array somehow. Can someone tell me how to get my array above working in the following code? Thanks very much.
<?php
$currentFile = date('s');
$allfiles = array('this01.txt','test02.txt','test03.html','imagefile04.gif','image05.gif');
foreach($allfiles as $get):
if(preg_match("/".$currentFile."\.txt/","/".$get."\.txt/")):
$get_content = file_get_contents($get);
break;
elseif(preg_match("/".$currentFile."\.html/","/".$get."\.html/")):
$get_content = file_get_contents($get);
break;
elseif(preg_match("/".$currentFile."\.gif/","/".$get."\.gif/")):
echo "<img src=\"$get\">";
break;
endif;
endforeach;
echo $get_content; exit();?>
[1][edited by: Simone100 at 6:19 am (utc) on Aug. 23, 2007]
<?php
ini_set('error_reporting', 8191);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
$currentFile = date('s');
$allfiles = Array();
$allfiles[1] = "afile.txt";
$allfiles[2] = "thatfile.html";
$allfiles[3] = "thatfile.gif";
foreach($allfiles as $get):
if(preg_match("/".$currentFile."\.txt/","".$get.".txt")):
$get_content = file_get_contents($get);
break;
elseif(preg_match("/".$currentFile."\.html/","".$get.".html")):
$get_content = file_get_contents($get);
break;
elseif(preg_match("/".$currentFile."\.gif/","".$get.".gif")):
echo "<img src=\"$get\">";
break;
endif;
endforeach;
echo $get_content; exit();?>
If anyone thinks the way I'm doing this is too hard please let me know. For instance if there is a way to see if $allfiles is a gif any easier, then to continue if it is, something like?
if($allfiles && $currentfile == ".jpg"¦".gif")
echo "<img src=/'$allfiles[$currentFile]'>";
else etc.
Or if it can still be fixed as is... Thank you very much.
[1][edited by: Simone100 at 9:22 am (utc) on Aug. 23, 2007]
Your file_get_contents($get) should be file_get_contents($get.".txt") etc. for each case
Although, it does occur to me that the error might be in the preg_match, where the second .".txt" should not be there at all.