File creation from Text Value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mdefelice
Forum Newbie
Posts: 3
Joined: Sat Oct 28, 2006 1:53 pm

File creation from Text Value

Post by mdefelice »

I am trying to create a file from a form text box but I get errors. I tried casting it but it doesn't work. Any suggestions?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: File creation from Text Value

Post by volka »

mdefelice wrote:Any suggestions?
yes. Most of us aren't clairvoyants.
a) show us the code
b) show us the error message
mdefelice
Forum Newbie
Posts: 3
Joined: Sat Oct 28, 2006 1:53 pm

Post by mdefelice »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Sorry, yes that would help. 

I get the following errors

Warning: flock() expects parameter 1 to be resource, boolean given on line 90

Warning: flock() expects parameter 1 to be resource, boolean given on line 92

Warning: fclose(): supplied argument is not a valid stream resource  on line 93

Code: Select all

<?php 
$fname;

/* prints out a text box */
function print_name_option($text,$name)
{
	//prints option values
	print '<tr>';
	print '<td align = "right">' . $text . ':</td>';
	print '<td align = "left"> <input type = "textbox" name = "' . $name . '" width = "20">';
	print '</tr>';
}

/* checks for file existence */
function file_exist($name)
{
		if(file_exists($name))
		{
			print '<br><font color=FF0000><B>The file "' . $name . '" already exists!</B></font>';
		}
}


function write_file( )
{
	$fname = $_POST["poll_name"];
	$fp = fopen($GLOBALS['fname'],'wb');//open file write only
		flock($fp,1);//lock file
			echo 'worked'
		flock($fp,3);//unlock file
	fclose($fp);

}


/* prints the installation */
function print_install( )
{
	print '<form method="post" action=' . $_SERVER['PHP_SELF'] . '>'; 
	print '<table width = "50%">';
		print_name_option('Poll Name', 'poll_name');//print poll box
	print '</table>';
	print '<input type="submit" value="submit">';
	print '</form>';

	if(!file_exist($_POST['poll_name']))
	{
		write_file( );

	}
}

?>


<?php print_install( ); ?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$fname = $_POST["poll_name"];
$fp = fopen($GLOBALS['fname'],'wb');//open file write only
The fopen probably fails.
This $fname = $_POST["poll_name"] doesn't create a global variable, it's in the functions scope =>$fp = fopen($fname ...
are you sure you want to use the POST parameter unfiltered as filename? What if someone passes a value like /etc/passwd ? (ok, this will probably fail but I'm sure you get the point)

Code: Select all

function write_file( )
{
	$fname = basename($_POST["poll_name"]);
	$fp = fopen($fname, 'wb'); //open file write only
	if ( false===$fp) {
		print '<br><font color=FF0000><B>The file "' . $fname . '" cannot be oppend!</B></font>';
	}
	else {
		flock($fp,1);//lock file
		echo 'worked'
		flock($fp,3);//unlock file
		fclose($fp);
	}
}
p.s.: please use

Code: Select all

[url=http://forums.devnetwork.net/faq.php?mode=bbcode]bbcode tags[/url] for your php code. It's much more readable  ...and the highlighter adds links to the php manual for built-in functions.
mdefelice
Forum Newbie
Posts: 3
Joined: Sat Oct 28, 2006 1:53 pm

Post by mdefelice »

Thank you very much for your help. Yes I will be adding some checking routines to control exactly what can be entered into the text box. I was just in the very early stages of that program.
Post Reply