Jump to content

Question about a get file.


11Tami

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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]);
}
?>

Link to comment
Share on other sites

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]);
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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;
    }
}
?>

Link to comment
Share on other sites

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.