File creation from Text Value
Posted: Sat Oct 28, 2006 1:57 pm
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?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
yes. Most of us aren't clairvoyants.mdefelice wrote:Any suggestions?
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 93Code: 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( ); ?>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]The fopen probably fails.$fname = $_POST["poll_name"];
$fp = fopen($GLOBALS['fname'],'wb');//open file write only
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);
}
}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.