Jump to content

[SOLVED] file function = includes php as plain text??


Recommended Posts

Is this normal? I have a file called 'hello.php' and in it's code I use the file function to include another file (hello2.php). This works ok, untill I want to add php code in hello2.php. That code is displayed as plain text, and it does not execute... ???

$file = file('hello2.php');

damn :-[

I can use 'include' and the php code executes, BUT then I can't use shuffle as I get errors. I use shuffle in hello.php to shuffle the content of hello2.php.

 

Warning: shuffle() expects parameter 1 to be array, integer given in blablabla
Warning: array_slice() [function.array-slice]: The first argument should be an array in blablabla
Warning: implode() [function.implode]: Invalid arguments passed in blablabla

:-\

If you use include with a file system path, variables are available. I'm going to guess that you used a URL instead. Post your code to get specific help with what it is doing.

 

This is the code 'with include'

<?php
$file = include('hello2.php');
shuffle($file);
echo implode("",array_slice($file,-10,11));
?>

If you have php code in hello2.php that you want to execute, there is no good reason to be using shuffle() on it.

 

The include statement includes the contents of the file at that point in the script. It does not return the contents so that it can be assigned to a variable.

 

What exactly is in hello2.php and what are you trying to accomplish.

What exactly is in hello2.php and what are you trying to accomplish.

The content of hello2.php is a thumbnail with info on the same line. But because there can be a broken link to an thumbnail I use if/else to load a default thumb if it's broken.

you can do shufffling logic in hello2.php ??

I don't understand what you mean...Do you ask if I can shuffle the content of hello2.php?

With the file function I can shuffle the content, but with include I can only make the default thumb work, and no shuffle.

as far as i understand in hello2.php you have some lines (thumbnails with php code), something like this:

 

 

if(file_exists('thumbnail1.jpg')) echo "<img src='thumbnail1.jpg'>"; else echo "<img src='defaultthumbnail.jpg'>";

if(file_exists('thumbnail2.jpg')) echo "<img src='thumbnail2.jpg'>"; else echo "<img src='defaultthumbnail.jpg'>";

if(file_exists('thumbnail3.jpg')) echo "<img src='thumbnail3.jpg'>"; else echo "<img src='defaultthumbnail.jpg'>";

 

and you want to shuffle images and show them randomly...

 

 

what u can do is shuffle images in hello2.php like this

 

$arrImages = array();

if(file_exists('thumbnail1.jpg')) $arrImages[] = "<img src='thumbnail1.jpg'>";  else $arrImages[] = "<img src='defaultthumbnail.jpg'>";

if(file_exists('thumbnail2.jpg')) $arrImages[] = "<img src='thumbnail2.jpg'>";  else $arrImages[] = "<img src='defaultthumbnail.jpg'>";

if(file_exists('thumbnail3.jpg')) $arrImages[] = "<img src='thumbnail3.jpg'>";  else $arrImages[] = "<img src='defaultthumbnail.jpg'>";

 

 

 

shuffle($arrImages);

echo implode("",array_slice($file,-10,11));

as far as i understand in hello2.php you have some lines (thumbnails with php code), something like this:

...

 

This is what I have in hello2.php and this on every line

 

<table border="0" width="100%" cellspacing="0"><tr><td valign="top" align="left" width="150"><a href="http://www.website.com/images.htm"><img src="<?php if (file_exists('thumb01.gif')) { echo "thumb01.gif"; } else { echo "nothumb.gif"; } ?>" border="0"></a></td><td valign="top" align="top">information about this image</td></tr></table><br>

 

 

put this in hello2.php

 

$arrImages = array();

$arrImages[] = '<table border="0" width="100%" cellspacing="0"><tr><td valign="top" align="left" width="150"><a href="http://www.website.com/images.htm"><img src="'.(file_exists('thumb01.gif')? "thumb01.gif": "nothumb.gif").'" border="0"></a></td><td valign="top" align="top">information about this image</td></tr></table><br>';

//more lines like this...

 

//put this at the end..of hello2.php

shuffle($arrImages);

echo implode("",array_slice($file,-10,11));

 

//in hello1.php just include hello2.php dont shuffke or anything else

include hello2.php

 

 

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.