kucing Posted July 1, 2008 Share Posted July 1, 2008 Hello there, I want a function or a command which let me read a php file but should not run it while reading. like example. I have a file name read.php read.php <?php echo ("hello"); ?> When I get the content of this file using file_get_contents which first run the file and then I get the content of the file I only want to have the content of this file without running this file. Thanks. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 So you there to be a php file, we'll call it test.php. You want to be able to do a function that will read its php code(not the resulting html it outputs) and display it in some form? Ummm... if there's a way, then facebook and other big sites that run in php would be easily hacked. I don't know of a way to do that.. unless there are some hacker tools out there. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 1, 2008 Share Posted July 1, 2008 <?php $file = 'somefile.txt'; $handler = fopen($file, 'r'); $contents = fread($handler, filesize($file)); print $contents; fclose($handler); ?> I think that's what you're looking for. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 <?php $file = 'somefile.txt'; $handler = fopen($file, 'r'); $contents = fread($handler, filesize($file)); print $contents; fclose($handler); ?> I think that's what you're looking for. he's looking to read a .php file. not a .txt(or any other) file. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted July 1, 2008 Share Posted July 1, 2008 its correct code. just change file name to read.php. dats all. <?php $file = 'read.php'; $handler = fopen($file, 'r'); $contents = fread($handler, filesize($file)); print $contents; fclose($handler); ?> Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 1, 2008 Share Posted July 1, 2008 its correct code. just change file name to read.php. dats all. <?php $file = 'read.php'; $handler = fopen($file, 'r'); $contents = fread($handler, filesize($file)); print $contents; fclose($handler); ?> Precisely, fopen() can read any kind of file. I use it often to read .torrent files and use my BEncode library I wrote to get the Meta-Info from it. However, you won't be able to write to this file without the necessary permissions on the server. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 its correct code. just change file name to read.php. dats all. <?php $file = 'read.php'; $handler = fopen($file, 'r'); $contents = fread($handler, filesize($file)); print $contents; fclose($handler); ?> uhhh... have you tested your code? He's saying he wants the code(I believe), not the output. Or I may have misread. He wasn't clear anyways when he said he wants the content of the file. Since php has code, and then changes to show html, if you to that with .php, if will give you the html that would otherwise be shown if you viewed the file Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 1, 2008 Share Posted July 1, 2008 No I haven't tested my code. And I do not need to because I know it works. It will write the contents of any file you specify into the browser in plain text. However, it will NOT be formatted like it would when opening the file in a text editor. You will need to write additional code to accept the line breaks. It won't display the output of the PHP file which is being read because that particular code isn't being parsed by the PHP Engine, the read code is. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted July 1, 2008 Share Posted July 1, 2008 may be i misinterpret him,but if you want to show the code, of course not in html form. then Wolphie code is correct. try it yourself. just tried: $sql="SELECT * FROM xyz where active ='0'"; $q=mysql_query($sql); $num=mysql_num_rows($q); while ($row = mysql_fetch_object($q)) { $id=$row->id; $ch1=$row->ch1; $ch2=$row->ch2; $ch3=$row->ch3; $chp1_2_3=$row->chp1_2_3; $active=$row->active; echo $id; $sql1="update xyz set active='1' where id='$id'"; $q2=mysql_query($sql1); } ?> Quote Link to comment Share on other sites More sharing options...
kucing Posted July 1, 2008 Author Share Posted July 1, 2008 Thanks all for your time and concern. Well.. I am still getting the same result as if using file_get_contents function. What if we change the first line <?php to ?php while getting the data from the file or rename the file to .txt ext before getting the content? any easy way to do that? Thanks Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 may be i misinterpret him,but if you want to show the code, of course not in html form. then Wolphie code is correct. try it yourself. just tried: $sql="SELECT * FROM xyz where active ='0'"; $q=mysql_query($sql); $num=mysql_num_rows($q); while ($row = mysql_fetch_object($q)) { $id=$row->id; $ch1=$row->ch1; $ch2=$row->ch2; $ch3=$row->ch3; $chp1_2_3=$row->chp1_2_3; $active=$row->active; echo $id; $sql1="update xyz set active='1' where id='$id'"; $q2=mysql_query($sql1); } ?> did you have a beginning <?php in that code? And did you upload it to a server in .php format? And when you ran the script, did it read the php code or the output in html format? I know what the answer is. Neither of us are wrong in this thread, we are just both interpreting the guy's question differently. There is no [easy] way to get pure php code from a .php file(if that's what he's asking) Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 1, 2008 Share Posted July 1, 2008 Just out of curiosity, have you checked out the highlight_file() function? It's a built-in function that highlights PHP code and formats it according to how you have written it. http://uk2.php.net/manual/en/function.highlight-file.php Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted July 1, 2008 Share Posted July 1, 2008 Except showing <?php i upload to server. read another file in that file. and works. may be he want to show all the code and with breaks.so has to do extra coding for that. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 Except showing <?php i upload to server. read another file in that file. and works. may be he want to show all the code and with breaks.so has to do extra coding for that. you didn't answer my question. Is the file a valid .php file, and you are using fopen to read the php code? Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted July 1, 2008 Share Posted July 1, 2008 Yup,it is. cron_vibration.php Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 Yup,it is. cron_vibration.php ok, do me a favor and gather the php code for facebook's /home.php Quote Link to comment Share on other sites More sharing options...
kucing Posted July 1, 2008 Author Share Posted July 1, 2008 It worked prefectly. Thank you all Just out of curiosity, have you checked out the highlight_file() function? It's a built-in function that highlights PHP code and formats it according to how you have written it. http://uk2.php.net/manual/en/function.highlight-file.php Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 1, 2008 Share Posted July 1, 2008 No offense dannyb785, but you're criticizing peoples code and their efforts to help this person. Yet what exactly have you contributed? Apart from idiotic criticism. If you're going to do that, make sure it's constructive criticism which will help people better their programming skills. Not everybody has an extensive experience. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 No offense dannyb785, but you're criticizing peoples code and their efforts to help this person. Yet what exactly have you contributed? Apart from idiotic criticism. If you're going to do that, make sure it's constructive criticism which will help people better their programming skills. Not everybody has an extensive experience. Hey wolphie, wanna show me where I was idiotic? Also, show me where I told him his code was wrong. His code performs a task. I didn't think, based on the initial question, that the code provided performed the task requested. I was the first person to reply and post help! That's what I contributed. I also contributed the fact that you can't gather php code from a php file uploaded to a server just by running a fopen script. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 1, 2008 Share Posted July 1, 2008 How does posting first justify it? And if you read, correctly. I wasn't suggesting his code. You didn't provide any evident help which could prove useful to the person asking the question, it's not exactly considered as a contribution. And by idiotic I meant your attitude, alright yes, you made a point which could have been put across better. But you didn't provide a solution. All in all, he didn't use the original method I proposed so therefore, discussion closed. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted July 2, 2008 Share Posted July 2, 2008 How does posting first justify it? And if you read, correctly. I wasn't suggesting his code. You didn't provide any evident help which could prove useful to the person asking the question, it's not exactly considered as a contribution. And by idiotic I meant your attitude, alright yes, you made a point which could have been put across better. But you didn't provide a solution. All in all, he didn't use the original method I proposed so therefore, discussion closed. How about my input being ignored? I said that if you're trying to grab pure php code from a php file off of a server, you can't do it. How much better does it need to be put across? I didn't provide a solution bc there wasn't one. 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.