Hi all,

I am trying to write a php file using fopen/fwrite. However, it is apparently being ignored. Not even getting any error messages.

Right now, I've got a flat file, which stores information that is used to specify which variables. At this point, they are hardcoded. I would like to be able to dynamically write that file. At the top of the first file, I am assigning values to some variables. There is an include file, which should be using those variables, along with some hard coded text to write a new file.

Here is the first file:

<!-- This is the MySQL login POST page -->

<html>
<head>
<style type="text/css">
    @import url(resources/css/global.css);
</style>
</head>
<body> 
<?php

session_start();
//include('app_layout.php');
include('welcome_layout1.php');

// Report all PHP errors
 // ini_set('error_reporting', E_ALL);

// Set the display_errors directive to On
// ini_set('display_errors', 1);

// print "<pre>" . print_r($vsf->config) . "</pre>";
// if this is the first time the form has been loaded (submit_flag won't exist),
// then prefill the` form values from the config file


$host = $_POST['host'];
$database_name = $_POST['database_name'];
$username = $_POST['username'];
$password = $_POST['password'];

$_SESSION['host'] = $_POST['host'];
$_SESSION['database_name'] = $_POST['database_name'];
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];

$error_msg = "<div class=\"message\"><span class=\"red\">Cannot Connect</span></div></br>";
$again = "<div class=\"message\"><span class=\"black\">Please <a href=\"welcome.php\">try</a> again</span></div>";

$con = mysql_connect($host, $username, $password);
$db_selected = mysql_select_db($database_name, $con);

// SECTION THAT FORMATS SUCCESSFUL LOGIN MESSAGE

if ($db_selected) {
	echo "<div class=\"message\" >";
	echo "<span class=\"green\">".$username."</span><br>";
	echo "<span class=\"black\">has succesfully logged into the</span><br>";
	echo "<span class=\"green\">".$database_name."</span><span class=\"black\"> database!!</span><br>";
	echo "</div>";

// added to try and combine functionality of test.php with welcome2.php - rjm 040712	

$query="SELECT feed_title,id FROM feeds";
$result = mysql_query ($query);

echo "<h2>";
//echo "<a href=\"index.php?action=saveconfig\">Create New Feed</a><br>";

echo "<a href=\"feed_config1.php\">Create New Feed</a><br>";
echo "<a href=\"index.php?action=feedlist\">Modify Existing Feed</a><br>";
include('A_dropdown.php'); //pull-down menu of feeds from feeds table. Intent is to populate with variables based on which feed selected - 040712 rjm
	}
	else
	{
// SECTION THAT FORMATS INVALID LOGINS ATTEMPTS
if (empty($host)) {
	echo "<div class=\"message\" >";
    echo "<span class=\"red\">host</span>". ' <span class=\"black\"> not specified'."</br></span></div>";
}
if (empty($username)) {
	echo "<div class=\"message\" >";
    echo "<span class=\"red\">username</span>". ' <span class=\"black\">not specified'."</br></span></div>";
}
if (empty($password)) {
	echo "<div class=\"message\" >";
    echo "<span class=\"red\">password</span>". '  <span class=\"black\">not specified'."</br></span></div>";
}
if (empty($database_name)) {
	echo "<div class=\"message\" >";
    echo "<span class=\"red\">database</span>". '  <span class=\"black\">not specified'."</br></span></div>";
}
if (!$db_selected) {
	echo $error_msg;
}
	echo $again;
	}

mysql_close($con);

// adding section below to try and rewrite the TEMP.config.xml.php file on the fly - 6/12/12 rjm

include('gen_config.xml.php');

?>
</body>
</html>

Here is the content of the include file:

<!-- This file puts everything together and generates the podcast's RSS xml file -->

<?php

$output = <<<EOT

<config>
	<database>
    	<host>$host</host>
        <database_name>$database_name</database_name>
		<username>$username</username>
		<password>$password</password>
	</database>        
<misc> <datadir>/media/CHATEAU/robertjm/www/podcasts</datadir> <dataurl>http://www.recastweb.com/media/</dataurl> <rssoutfile maxitems="500">/media/CHATEAU/robertjm/www/podcasts/Weekly.xml</rssoutfile> <showDBsetupMenuItem>Yes</showDBsetupMenuItem> </misc> <channel> <title>Regeneration - Sunday Weekly Podcasts</title> <link>http://www.recastweb.com/home.php</link> <webmaster>robertjm@hockeyhockeyhockey.com</webmaster> <language>en-us</language> <copyright>Copyright 2012</copyright> <description>The Weekly teaching of Pastor Albert Lee of ReGeneration Church, in Oakland, California, from the Sunday services. Come join us at 238 East 15th Street, at 9:30am or 6pm every Sunday.</description> <generator>XMLerator - The Free Web-based Podcast Management Software</generator> <managinEditor>robertjm@hockeyhockeyhockey.com</managinEditor><managingEditor>robertjm@hockeyhockeyhockey.com</managingEditor><podcastImageFile>http://www.recastweb.com/images/RegenNeonCross427.png</podcastImageFile><podcastURL>http://www.recastweb.com/home.php</podcastURL><podcastAltText>Regeneration - Sunday Weekly Podcasts</podcastAltText><new_feed_url>jabba the hutt</new_feed_url></channel> <itunes> <enable>Yes</enable> <keywords>regeneration, Albert Lee, Holy Bible, New Testiment, Jesus, Gospel, Luke</keywords> <subtitle>Regeneration - Sunday Weekly Podcasts</subtitle> <author>Albert Lee @ Regeneration</author> <owner_name></owner_name> <owner_email></owner_email> <image>http://www.recastweb.com/images/RegenNeonCross427.png</image> <categories><category>Religion_&amp;_Spirituality/Christianity</category></categories> <explicit_channel>No</explicit_channel><block_channel></block_channel> <complete></complete></itunes> <new_feed><enable>Yes</enable><new_feed_url/></new_feed></config> EOT; // now write the file $fh = fopen("TEST.config.xml.php", "w") or dieWithError("Can't open " . "TEST.config.xml.php for writing! Make sure this file is writable by the webserver user, " . " or is chmod'ed 666."); fwrite($fh, $output); fclose($fh); ?>

What am I missing? I was unsure about the second spec in the fopen() statement. However, trying different settings doesn't change the result. Nothing generated or warned about.

Thanks for looking!!

Robert

    If you have reporting turned on and aren't getting any errors or warnings, then your script is dying with a fatal error.

    I'd start troubleshooting by ditching the first file and working on just the fwrite operation... Just set some dummy vars to populate your heredoc... speaking of which, while testing, I'd change the heredoc tags to double quotes... you don't have any enclosed double quotes, so there's really no reason to use the heredoc tags anyway.

    Regardless, work backwards from the file write... once that's working, reintroduce small portions of your first script until you find the error.

      When you say heredoc tags you're talking about the <<< stuff, right?

      I'm still pretty neophyte when it comes to code so hadn't heard that name before.

      I'm using someone else's original code, and massaging it to do what I want it to do, which is why there might be stuff that's not necessarily the correct way of doing things.

      Amen, and working backwards. That's how I figured out the file I need to modify.

      Thanks again. Will post back if I still cannot get it, or it gets solved.

      Later,

      Robert

      antmeeks;11006091 wrote:

      If you have reporting turned on and aren't getting any errors or warnings, then your script is dying with a fatal error.

      I'd start troubleshooting by ditching the first file and working on just the fwrite operation... Just set some dummy vars to populate your heredoc... speaking of which, while testing, I'd change the heredoc tags to double quotes... you don't have any enclosed double quotes, so there's really no reason to use the heredoc tags anyway.

      Regardless, work backwards from the file write... once that's working, reintroduce small portions of your first script until you find the error.

        OK, so it seems like it all boils down to a permissions issue with the directory I'm trying to write out of.

        Here's the scenario:

        Within my www folder are:
        "Folder-X" = the php program folder for this particular program
        "Folder-Y" = The program in FolderX writes an XML file and stores it here.

        If I put a basic fopen/fwrite script within Folder-X, it squawks at me with:

        Warning: fopen(../test.txt): failed to open stream: Permission denied in /media/CHATEAU/robertjm/www/Folder-X/TESTCREATEFILE.PHP on line 10
        Warning: fwrite() expects parameter 1 to be resource, boolean given in /media/CHATEAU/robertjm/www/Folder-X/TESTCREATEFILE.PHP on line 11

        However, if I execute the PHP program from within Folder-Y, it writes the new file just fine.

        Here's what I'm testing with:

        <?PHP
        
        // Report all PHP errors
        ini_set('error_reporting', E_ALL);
        
        // Set the display_errors directive to On
         ini_set('display_errors', 1);
        
        
        $file = fopen("../test.txt","w+");
        fwrite($file, "Here is some text!");
        
        ?>
        

        Again, what I would like to do, is execute the fopen/fwrite as part of another page so that I can write a new version of "Page-Z", which is located WITHIN the SAME folder (Folder-X), not within Folder-Y.

        I looked at my permissions on my folder structure.
        Robertjm is the owner of Folder-X
        ROOT is the owner of Folder-Y.

        Eventually, I want to have this available for anyone to use. Is the above a security issue, in an of by itself?

        Thanks for looking,

        Robert

          Write a Reply...