Problem with file, file_get_contents and include in PHP 5

liunx

Guest
Hi,

My websites have just been moved to a PHP 5/MySql 5 server, which has caused some of my code to stop working.

With file and file_get_contents, I get a message saying that the file couldn't be found, using this code:

$aRet = file("http://myothersubdomain/path/to/file/file.php")

The file definitely exists and can be called from my browser, but file doesn't seem to be able to find it under php5.

With include, I have a file that I include into a script and the file also has an include within it. The file itself is included, but the include within the file is not. It seems to be executed, but nothing replaces it.

The first file is:

<html>
<head>
<title>Download Login</title>
<meta name="Keywords" content="download,login" />
<meta name="Description" content="Download Login" />
<meta http-equiv="robots" content="noindex, nofollow" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-us" />
<link href=http://www.phpbuilder.com/board/archive/index.php/"http://myothersubdomain/path/to/file/css.php" rel="stylesheet" type="text/css" />
<script language="JavaScript1.2" type="text/javascript" src=http://www.phpbuilder.com/board/archive/index.php/"js/constants.js.php"></script>
</head>
<body background="http://myothersubdomain/images/bg.jpg">
<table width="750" align="center" valign="top" cellpadding="0" cellspacing="0" class="page-border">
<tr><td align="center">
<img name="imghdr" src=http://www.phpbuilder.com/board/archive/index.php/"http://myothersubdomain/images/hdr.gif" width="750" height="100" border="0" />

<table width="100%" align="center" valign="top" border="0" cellpadding="10" cellspacing="0" class="bg-page">
<tr><td valign=top>
<h1>Download Login</h1>
<?php $booShowFieldset = TRUE; ?><?php include("http://myotherdomain/path/to/file/myfile.htm"); ?>
</td></tr></table>
<?php include("http://myothersubdomain/path/to/file/myfile2.htm"); ?>
</td></tr></table>
</body>
</html>

The code that executes it is:

$strEvalFilename = "/home/path/to/file/myincludefile.php";
ob_start();
include($strEvalFilename);
$strEvalText = ob_get_clean();
The result is:

<html>
<head>
<title>Download Login</title>
<meta name="Keywords" content="download,login" />
<meta name="Description" content="Download Login" />
<meta http-equiv="robots" content="noindex, nofollow" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-us" />
<link href=http://www.phpbuilder.com/board/archive/index.php/"http://myothersubdomain/path/to/file/css.php" rel="stylesheet" type="text/css" />
<script language="JavaScript1.2" type="text/javascript" src=http://www.phpbuilder.com/board/archive/index.php/"js/constants.js.php"></script>
</head>
<body background="http://myothersubdomain/images/dpp-bg.jpg">
<table width="750" align="center" valign="top" cellpadding="0" cellspacing="0" class="page-border">
<tr><td align="center">
<img name="imghdr" src=http://www.phpbuilder.com/board/archive/index.php/"http://myothersubdomain/images/hdr.gif" width="750" height="100" border="0" />

<table width="100%" align="center" valign="top" border="0" cellpadding="10" cellspacing="0" class="bg-page">
<tr><td valign=top>
<h1>Download Login</h1>
</td></tr></table>
</td></tr></table>
</body>
</html>

Could anyone tell me whether these statements have changed in the way they operate in php5 that would cause this behaviour and what I can do to fix it? All 3 statements have worked fine in php4 and nothing changed code-wise before or after the migration to php5.

DebbieThere is not change in the way those functions work. Most likely there is a change in configuration settings that affect how they can operate. You can help determine the exact problem by turning on some error reporting to find out what problems PHP is having with locating/opening those files. Try starting your script with:

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

Hopefully the script will then output some error message(s) explaining what is happening.Hi,

FYI, for anyone with a similar problem. I solved this problem by asking my hoster to switch allow_url_include on and everything is working again.

DebbieFor anyone else who has this problem...

Note that allow_url_include should be left off in most cases as it's unnecessary. In this case, for example, plain HTML files without PHP code shouldn't be read/outputted using include/require() but rather by readfile(), file_get_contents(), etc.

Now, allow_url_fopen, on the other hand, does need to be enabled if you're grabbing content from remote hosts (e.g. files that aren't on the same webserver as your script)....Now, allow_url_fopen, on the other hand, does need to be enabled if you're grabbing content from remote hosts (e.g. files that aren't on the same webserver as your script).
And even if it is not enabled, you can still use the cURL functions; so if maximum portability is desired, you may want to use cURL instead of PHP's file functions from accessing remote sites.Been following this thread with a lot of interest - just to clarify something - would you need allow_url_fopen if the include was for a file that only had php in it, which had the php extn and it was on the same server please ?

Joad.No, allow_url_fopen could be disabled because you would never use a path that began with "http://" to include a PHP file (or any other file on your server).would you need allow_url_fopen if the include was for a file that only had php in it, which had the php extnAnd also no because if you're using a URL the idea is that you're asking for a resource and it's no concern of yours what technology the server is using to generate it (having ".php" in a URL doesn't mean anything to someone requesting a resource).

Everyone repeat after me: URLs are not filenames.

Oh, and bradgrafelman: how long have you had the GPS receiver? ;)Many thanks for the useful info - it's appreciated.

Joad.What if I had a file I uploaded and wanted to import into a MySQL database. The db and webserver are NOT on the same machine which is causing me some difficulty.

The form has the following:

"<td><input type=\"file\" name=\"file_source\" id=\"file_source\" class=\"edt\" value=\"$file_source\"></td>"

Where after I submit I do the following:


//This is working
$fileName = basename($_FILES['file_source']['name']);
move_uploaded_file($_FILES['file_source']['tmp_name'], $uploadFolder.$fileName);

//Find the path
$uploadServer = $_SERVER["HTTP_HOST"];
$uploadFolder = $_SERVER['DOCUMENT_ROOT'] . "/atlas/modules/$modname/uploadcache/";



//Find the file
$csvFile = $uploadServer.$uploadFolder.$fileName;
$csv = file_get_contents($csvFile);

I then am trying to load into an array:

$breaker = "\n";
$fields = array('ReportNumber', 'Student_ID', 'Name_Last', 'Name_First', 'Total_RS', 'Total_Percent', 'Percentile', 'Subtest_RS', 'Subtest_Percent');
$lines = explode($breaker, $csv);

Afterwords I am inserting about 100 records into a db. It's not working yet.

Running PHP Version 5.2.4

This caught my eye:

allow_url_fopen, on the other hand, does need to be enabled if you're grabbing content from remote hosts

Which is turned on.
 
Back
Top