Vishal Sharma
Vishal Sharma

Reputation: 13

Opening file on web server using PHP

I am newbie in web development. I have LAMP installed on Ubuntu 12. I have a php file "register.php" running on LAMP server which receives data from a html form and then saves it on server in a temp file. I am using below code. It does not seem to work. I've checked that it has required access and permission to execute the code but still it could not create the "temp.c"

$temp=fopen("temp.c","w");
$line = fgets($temp)
fclose($temp);

Additional Note(s):- I am able to receive other data from the form fields but only creation of file does not work.

Thanks for your help!

Upvotes: 1

Views: 354

Answers (1)

Umair Aziz
Umair Aziz

Reputation: 1528

Put Semicolon after fgets function like

$temp=fopen("temp.c","w");
$line = fgets($temp);
fclose($temp);

I worked as i verified on my local server....

My advice to you is to use NETBEANS IDE for PHP development as it will display Syntax errors automatically, when you are writing the code.....

To insert form data in file use following code

    $entry=$_POST["myvar"];
    $file="temp.c";
    $open=fopen($file,"a");

    if($open){        
      fwrite($open,$entry);
        fclose($open);
    } 

'a' in fopen($file,"a") Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

'a+' in fopen($file,"a+") Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

'w' in fopen($file,"a+") Open for writing only; place the file pointer at the beginning of the file and ==>truncate the file to zero length<==. If the file does not exist, attempt to create it.

Consult this for more info

Upvotes: 1

Related Questions