gmaster
gmaster

Reputation: 692

PHP create new file problemk

I am working on a form that will allow a user to submit text to the website and save it as a file on the server that I can retrieve later. I am using the following form:

    <p>
      <label for="Name">First Name</label>
      <input type="text" name="Name" id="Name" />
    </p>
    <p>
      <label for="Last Name">Last Name</label>
      <input type="text" name="LastName" id="LastName" />
    </p>
    <p>
      <label for="GuideName">Guide Name</label>
      <input type="text" name="GuideName" id="GuideName" />
    </p>
    <p>Copy and paste your study guide into this box.</p>
    <p>
      <textarea name="Guide" id="Guide" cols="100" rows="30"></textarea>
    </p>
    <p>Submit
      <input type="submit" name="Submit" id="Submit" value="Submit" />
      </p>
    </form>

and the following PHP code:

<?php
if (isset($_POST['Submit']))
{
    $firstName = $_POST['Name'];
    $lastName = $_POST['LastName'];
    $guideName = $_POST['GuideName'];
    $guide = $_POST['Guide'];
    $finalGuideName = $guideName."(".$firstName." ".$lastName.").txt";

    $fh = fopen($finalGuideName, 'w') or die("can't open file");
    fwrite($fh, $guide);
    fclose($fh);
}  
?>

I've used this code (at least I think it's this code) in the past to take the user's information and create a text file on the server with it. However, when I got to check to see if the form worked, nothing appears on the server. What am I doing wrong? Thanks in advance.

Thanks for your help everyone, as I was trying to fix it, it started to work again. I have absolutely no idea why, I'm pretty sure I didn't change anything, but thanks again for all your help.

Upvotes: 0

Views: 473

Answers (3)

holographix
holographix

Reputation: 2557

have you tried using $_REQUEST instead of _POST or _GET?

to discover furher informations I think you shall also put an else to your

if(isset($_POST['Submit'])) {

just to make sure that the statement is reached. and as a last resort, check you paths and their permissions, like begin with a 0777, and if it works correct it to 755 or 644 (it depends by the cofiguration of the owning user of the directory vs the apache user, they shall be all in the same group, but that's another story )

Upvotes: 0

Josh Toth
Josh Toth

Reputation: 764

You need to make sure the directory you're writing in allows write permissions (change to 0755 or 0644*). Also, I wouldn't recommend writing to the ROOT of the site, make a folder and change the permissions on THAT. It's not a good idea to let anyone write to the root, haha

*Thanks to Lawrence for pointing out my mistake

Upvotes: 0

Kashif Khan
Kashif Khan

Reputation: 2645

your sumbit button name is Submit2, so use this code

if (isset($_POST['Submit2'])) 

Upvotes: 1

Related Questions