Jump to content

Page will not successfully include()


stickynote427

Recommended Posts

I have a tutorials page on my site. A parameter in the URL determines the tutorial displayed, and if a "code" parameter is set, then that example for the tutorial will be displayed instead.

 

However, when I try to include() the example file, my entire page does not load; I'm assuming because include() is obviously run in PHP, as well as the file starting out with some PHP code.

 

Here's what I have at the top of my tutorials page:

 

<?php

if (isset($_GET['t'])) { //if a tutorial is specified
if (isset($_GET['code'])) { //check if the example should be shown
include('howto/'.$_GET['t'].'/code'.$_GET['t'].'.txt'); //include the example file
}
else { //if the example file should not be shown
include('howto/'.$_GET['t'].'/tut'.$_GET['t'].'.txt'); //include the tutorial file
}
echo("<a href=\"http://blakechesney.com/p/howto\">Return to the main how-to page</a>"); //echo navigation back to the main page
}

else {
// perform actions related to the main page; probably irrelevant to my problem
?>

 

When the "code" parameter is set, though, the page only loads up to the point until I want to include() the example file.

 

Any ideas?

 

Thanks.

stickynote427

Link to comment
https://forums.phpfreaks.com/topic/191936-page-will-not-successfully-include/
Share on other sites

Try using the require() function as it has better error reporting. Also if you are planning to store the file data in a variable you can alternatively use the file_get_contents() function. The cause - the file does not exist. Be sure the path is relative of the php script and try to follow the path via ftp yourself.

Thanks.

 

This is what I get:

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: NO) in /var/www/html/mysql/test.php  on line 4

 

I have tried different usernames and passwords as well and those do not work, either.

I'm confused. This is all of the code I have in my file:

 


<?php ini_set('display_errors','1') ; error_reporting(E_ALL) ;?>

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

if (mysql_query("CREATE DATABASE my_db",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

mysql_close($con);
?>

Oh, lord. I apologize. I'm also having problems with MySQL and did not realize that this was not where I was trying to get help with MySQL.

 

I added the PHP code that thorpe provided earlier to the CORRECT file this time. Here is the error I receive:

 

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /var/www/html/pages/howto/2/code2.txt  on line 6

 

Again, I'm sorry about my mistake.

Isn't it obvious, the file does not exist. So I would suggest that you make sure the specified path is NOT a fake url and to make sure it exists. If you need a fake url you must specify the full 'http://domain.com/folderetc'.

Okay, now I receive:

 

Warning: file_get_contents(howto/2/tut2.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /var/www/html/pages/howto.txt  on line 8

Alright, well, I tried this:

 

echo file_get_contents('http://example.com/pages/howto/'.$_GET['t'].'/tut'.$_GET['t'].'.txt');

 

And I get this error message:

 

Warning: file_get_contents(http://example.com/pages/howto/2/tut2.txt) [function.file-get-contents]: failed to open stream: HTTP request failed! in /var/www/html/pages/howto.txt  on line 8

 

And went to http://example.com/pages/howto/2/tut2.txt and I can see the file fine.

Replace the example.com with your real domain.

Alright, well, I tried this:

 

echo file_get_contents('http://example.com/pages/howto/'.$_GET['t'].'/tut'.$_GET['t'].'.txt');

 

And I get this error message:

 

Warning: file_get_contents(http://example.com/pages/howto/2/tut2.txt) [function.file-get-contents]: failed to open stream: HTTP request failed! in /var/www/html/pages/howto.txt  on line 8

 

And went to http://example.com/pages/howto/2/tut2.txt and I can see the file fine.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.