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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Try making your connect line the following

$con = mysql_connect("localhost","root","",true);

 

Firstly, what does that have to do with the original question?

 

Secondly, your advice would do NOTHING at all in relation to an access denied error either.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.