11Tami Posted July 19, 2007 Share Posted July 19, 2007 Hello, is there a way to include more than one web site file with PHP? For example: <?php $includefile = "file" . ".txt"; include ($includefile); ?> Includes a web site file named file.txt into a page. I know if I wanted to include an image it would be $includefile = "file" . ".jpg"; include ($includefile); etc. But is there a way to include both in one? I'm getting kind of tired of using separate scripts for each one. Some sort of code that will allow you to add both an image type and a file type, both in the same script? Please let me know if you know something that might work or if you have bumped into anything somewhere. Thanks a lot. Quote Link to comment Share on other sites More sharing options...
dooper3 Posted July 19, 2007 Share Posted July 19, 2007 You could use an array like so: //declare the array and add as many files as you want $files=array("file1.txt","file2.txt","image.jpg"); //count how many files there are in the array $count=count($files); //use a for loop to include them all for ($n=0;$n<$count,$n++) { include $files[$n]; } Hope that helps Quote Link to comment Share on other sites More sharing options...
11Tami Posted July 20, 2007 Author Share Posted July 20, 2007 Thanks very much, nothing is appearing and I have these uploaded file1.txt file1.txt file1.jpg and the code I'm using: <?php $files=array("file1.txt","file2.txt","file1.jpg"); $count=count($files); for ($n=0;$n<$count,$n++ {include $files[$n];} ?> The code seems to want something more that it doesn't have...anyone know? Quote Link to comment Share on other sites More sharing options...
benjaminbeazy Posted July 20, 2007 Share Posted July 20, 2007 what does this give you? <?php ini_set('error_reporting', 8191); ini_set('display_startup_errors', 1); ini_set('display_errors', 1); $files=array("file1.txt","file2.txt","file1.jpg"); $count=count($files); for ($n=0; $n < $count; $n++{ include($files[$n]); } ?> Quote Link to comment Share on other sites More sharing options...
11Tami Posted July 20, 2007 Author Share Posted July 20, 2007 Thanks that tool worked great. It is including each file in the array at the same time, instead of a different one in the array on each separate page load. Also the error I get says it won't include an image file along with the rest. Anyone know of a way that an image file can be included along with a .txt file? Thank you very much. <?php ini_set('error_reporting', 8191); ini_set('display_startup_errors', 1); ini_set('display_errors', 1); $files=array("file1.txt","file2.txt","file1.jpg"); $count=count($files); for ($n=0; $n < $count; $n++){ include($files[$n]); } ?> Quote Link to comment Share on other sites More sharing options...
benjaminbeazy Posted July 20, 2007 Share Posted July 20, 2007 try this... <?php ini_set('error_reporting', 8191); ini_set('display_startup_errors', 1); ini_set('display_errors', 1); $files=array("file1.txt","file2.txt","file1.jpg"); $count=count($files); for ($n=0; $n < $count; $n++{ $parts = explode(".", $files[$n]); $ext = array_pop($parts); switch ($ext){ case "jpg": echo "<img src=\"{$files[$n]}\">"; break; default: include($files[$n]); break; } } ?> Thanks that tool worked great. It is including each file in the array at the same time, instead of a different one in the array on each separate page load. do you want it to include just one file each time??? if so, just use a array_rand and pull out $files[0] for the include Quote Link to comment Share on other sites More sharing options...
11Tami Posted July 20, 2007 Author Share Posted July 20, 2007 Thank you, no problem, I can figure out how to get them one at a time later, right now I just need to get the images included. This is where I'm at, I moved the $n++ to another line, it didn't want it earlier. Now its saying it doesn't like the $ sign past this ?> But there is no $ anywhere past this ?>. So there is something else wrong but the errors aren't telling me where to look for it. Anyone see why this code isn't activating at all? Thanks. <?php ini_set('error_reporting', 8191); ini_set('display_startup_errors', 1); ini_set('display_errors', 1); $files=array("file1.txt","file2.txt","file1.jpg"); $count=count($files); for ($n=0; $n < $count;){ $n++; $parts = explode(".", $files[$n]); $ext = array_pop($parts); switch ($ext){ case "jpg": echo "<img src=\"{$files[$n]}\">"; break; default: include($files[$n]); break; ?> Quote Link to comment Share on other sites More sharing options...
benjaminbeazy Posted July 20, 2007 Share Posted July 20, 2007 i think you were missing a brace or two, but oh well, i changed it already to a foreach loop before i saw that so here, use this... <?php ini_set('error_reporting', 8191); ini_set('display_startup_errors', 1); ini_set('display_errors', 1); $files=array("file1.txt","file2.txt","file1.jpg"); $count=count($files); foreach($files as $var => $val){ $parts = explode(".", $val); $ext = array_pop($parts); switch ($ext){ case "jpg": echo "<img src=\"{$val}\">"; break; default: include($val); break; } } ?> Quote Link to comment Share on other sites More sharing options...
11Tami Posted July 20, 2007 Author Share Posted July 20, 2007 Great, I'll send you something. I'll see if I can pick each item on its own now. Quote Link to comment Share on other sites More sharing options...
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.