Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this PHP code overwrite file contents while opening?
    primarykey
    data
    text
    <p>I've written some pretty complex PHP apps using this feature, so I'm not sure what's happening here.</p> <p>I'm currently writing an app that uses an online history to save content (via ajax get). The API I wrote to store the user's history to a file is extremely simple:</p> <pre><code>$myFile = "./snhistory/".$_GET["uid"]; $stringData = urldecode($_GET["name"])."\n"; $file = fopen($myFile,"a"); fwrite($file,$stringData); fclose($file); </code></pre> <p>This looks like the kind of code that adds the data found in <code>name</code> plus a new line to the end of a file, right? Well, that's not how PHP sees it. It adds one name, then when I run the code with a different name, it overwrites the first and only the second one appears. I've tried anything I could think of:</p> <pre><code> file_put_contents($myFile,file_get_contents($myFile).$stringData); </code></pre> <p>And</p> <pre><code> file_put_contents($myFile,$stringData,FILE_APPEND); </code></pre> <p>And </p> <pre><code>fopen($myFile,"w"); fseek($file,0,SEEK_END); </code></pre> <p>And all produce the same behaviour. Is it something wrong with PHP or am I missing something here? I feel like if there was a problem in the code, the second thing I tried would have fixed it. But I'm not sure, which is why I'm asking everyone here.</p> <p>I would really appreciate any help that could be provided. </p> <p><strong>Edit:</strong> As per the answer, the problem with the strings was that they contained punctuation like <code>'</code> and, for some reason, the url encoding and escaping doesn't work. Read the answer's edit for the final answer.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload