简体   繁体   中英

Problems saving file in PHP (“failed to open stream: No such file or directory”)

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...

在此处输入图片说明

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM