简体   繁体   English

在PHP中保存文件时出现问题(“无法打开流:没有此类文件或目录”)

[英]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代码:

<?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") (当我尝试保存文件名“ aaa”时)

that: " Warning: file_put_contents(/SavedForms/aaa) [function.file-put-contents] : failed to open stream: No such file or directory in" 那:“ 警告: file_put_contents(/ SavedForms / aaa) [function.file-put-contents] :无法打开流:在其中没有这样的文件或目录”

I already have created the directory /SavedForms/ in the file as seen in the image below... 我已经在文件中创建目录/SavedForms/ ,如下图所示。

在此处输入图片说明

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. 假设SavedForms正在执行的php脚本位于同一目录中。

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 这意味着它当前正在根目录中查找SavedForms ,当然,它找不到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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