0

I am simply trying to save a file containing the serialized values of a form.

The Simple Form

    <h2>PHP Form Validation Example</h2>
    <form form method="post"> 
       First Name: <input type="text" name="fname" value="
       
      <?PHP 
      
      echo ($_POST['fname']) 
      
      ?>">
       
<br><br>
   <input type="submit" name="Save" value="Save Current Form"> 
   Save File Name As: <input type="text" name="saveFile">
   <br><br>
   <input type="submit" name="Load" value="Load Old Form">      
       Load File: <input type="text" name="loadFile">

    </form>

When i save/load the file i use the PHP code:

<?php

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

$PostArray = $_POST;
$s = base64_encode(serialize($PostArray)); 
$file = sprintf('/SavedForms/%s',$_POST['saveFile']);
file_put_contents($file, $s);

}

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

$file = sprintf("/SavedForms/%s",$_POST['loadFile']);
$_POST = unserialize(base64_decode(file_get_contents($file)));

}
?>

But it simply tells me

(when i try the save the file name "aaa")

that: "Warning: file_put_contents(/SavedForms/aaa) [function.file-put-contents]: failed to open stream: No such file or directory in"

I already have created the directory /SavedForms/ in the file as seen in the image below...

enter image description here

Could someone please let me know what i am doing wrong here!?!?

0

1 Answer 1

3

Remove the leading forward slash from the directory:

$file = sprintf('SavedForms/%s',$_POST['saveFile']);

Assuming that SavedForms is in the same directory as the executing php script.

Paths are relative to the script. By putting a leading slash, you are declaring an absolute path from the very root directory of your machine. This means it is currently looking for SavedForms in the root directory, and of course, it fails to find it

1
  • No problem. Good luck with your project.
    – Alex
    Jul 21, 2014 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.