PHP File Save Removes Backslashes - How to save PHP with Backslashes

138 Views Asked by At

I have a basic PHP script that I use to edit files on my server. I was working on creating a PHP contact form using the script, and while using the \n function to start a new line - I noticed on saving the file that it keeps removing the backslashes. Is this because of the stripslashes function? And how do I fix it? I will need to be able to save the file with \ (backslashes) for code. Here is the code snippet giving me the problem:

if (isset($_POST["filename"]) && $_SESSION['onefilecms_valid'] = "1" && $_POST["sessionid"] == session_id()) {
    $filename = $_POST["filename"];
    $content = stripslashes($_POST["content"]);
    $fp = @fopen($filename, "w");
    if ($fp) {
        fwrite($fp, $content);
        fclose($fp);
    }
    $message = $filename." saved successfully.";
}
if (isset($_GET["f"])) {
    $filename = stripslashes($_GET["f"]);
    if (file_exists($filename)) {
        $page = "edit";
        $pagetitle = "Edit “".$filename."”";
        $fp = @fopen($filename, "r");
        if (filesize($filename) !== 0) {
            $loadcontent = fread($fp, filesize($filename));
            $loadcontent = htmlspecialchars($loadcontent);
        }
        fclose($fp);
    } else {
        $page = "error";
        unset ($filename);
        $message = "File does not exist.";
    }
}

What can I do to fix this? Any help would be greatly appreciated! Thank you to everyone in advance.

0

There are 0 best solutions below