willdk Posted February 13, 2009 Share Posted February 13, 2009 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'); Quote Link to comment https://forums.phpfreaks.com/topic/145115-solved-file-function-includes-php-as-plain-text/ Share on other sites More sharing options...
The Little Guy Posted February 13, 2009 Share Posted February 13, 2009 why not like this? include 'hello.php'; Quote Link to comment https://forums.phpfreaks.com/topic/145115-solved-file-function-includes-php-as-plain-text/#findComment-761615 Share on other sites More sharing options...
samshel Posted February 13, 2009 Share Posted February 13, 2009 this is perfectly normal. file() just reads the file contents , ( which is PHP code in your case). If you want to execute the PHP code in hello2.php, you should be including it. Quote Link to comment https://forums.phpfreaks.com/topic/145115-solved-file-function-includes-php-as-plain-text/#findComment-761616 Share on other sites More sharing options...
willdk Posted February 13, 2009 Author Share Posted February 13, 2009 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 :-\ Quote Link to comment https://forums.phpfreaks.com/topic/145115-solved-file-function-includes-php-as-plain-text/#findComment-761626 Share on other sites More sharing options...
PFMaBiSmAd Posted February 13, 2009 Share Posted February 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/145115-solved-file-function-includes-php-as-plain-text/#findComment-761631 Share on other sites More sharing options...
willdk Posted February 13, 2009 Author Share Posted February 13, 2009 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)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/145115-solved-file-function-includes-php-as-plain-text/#findComment-761639 Share on other sites More sharing options...
PFMaBiSmAd Posted February 13, 2009 Share Posted February 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/145115-solved-file-function-includes-php-as-plain-text/#findComment-761663 Share on other sites More sharing options...
willdk Posted February 13, 2009 Author Share Posted February 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/145115-solved-file-function-includes-php-as-plain-text/#findComment-761676 Share on other sites More sharing options...
samshel Posted February 13, 2009 Share Posted February 13, 2009 you can do shufffling logic in hello2.php ?? Quote Link to comment https://forums.phpfreaks.com/topic/145115-solved-file-function-includes-php-as-plain-text/#findComment-761681 Share on other sites More sharing options...
willdk Posted February 13, 2009 Author Share Posted February 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/145115-solved-file-function-includes-php-as-plain-text/#findComment-761683 Share on other sites More sharing options...
samshel Posted February 13, 2009 Share Posted February 13, 2009 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)); Quote Link to comment https://forums.phpfreaks.com/topic/145115-solved-file-function-includes-php-as-plain-text/#findComment-761686 Share on other sites More sharing options...
willdk Posted February 13, 2009 Author Share Posted February 13, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/145115-solved-file-function-includes-php-as-plain-text/#findComment-761693 Share on other sites More sharing options...
samshel Posted February 13, 2009 Share Posted February 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/145115-solved-file-function-includes-php-as-plain-text/#findComment-761701 Share on other sites More sharing options...
willdk Posted February 13, 2009 Author Share Posted February 13, 2009 put this in hello2.php ... Your code helped me to make the script work thanks samshel & all posters! Quote Link to comment https://forums.phpfreaks.com/topic/145115-solved-file-function-includes-php-as-plain-text/#findComment-761727 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.