Jump to content


Photo

Weird File Error


  • Please log in to reply
3 replies to this topic

#1 mikesena

mikesena
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 25 August 2006 - 10:50 AM

Hi,

if(!$mode){ $mode = "start"; }


if($mode == "start"){
    $handle = fopen($filename, "r") or die("Read: The file <i>'".$filename."'</i> could not be opened.");

    $data = fread($handle, filesize($filename)) or die("Read: The file <i>'".$filename."'</i> could not be read.");

    fclose($handle);
    

} elseif($mode == "save"){

    $handle = fopen($filename, "w") or die("Write: The file <i>'".$filename."'</i> could not be opened.");
    fwrite($handle, $data) or die("Write: The file <i>'".$filename."'</i> could not be writen.");

    fclose($handle);
    

    echo "File data saved OK!<br><br>";

    

    $handle = fopen($filename, "r") or die("Read: The file <i>'".$filename."'</i> could not be opened.");

    $data = fread($handle, filesize($filename)) or die("Read: The file <i>'".$filename."'</i> could not be read.");

    fclose($handle);

}

This is my php code for a basic file open / save (a section of my script).
the filename is in the style of "./" followed by the filename.
For some reason, whenever i save a file, extra slashes appear next to " and '.

eg. include('Hello.php');
    include("Hi.php");
  those would become:
    include(\'Hello.php\');
    include(\"Hi.php\");

The file contents are being displayed in a text area.  when a save button is pushed, the data is saved.

What is causing this error?

#2 Stuie_b

Stuie_b
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts
  • LocationNewcastle, UK

Posted 25 August 2006 - 11:40 AM

if i'm not mistaken it's something too do with the way html passes it's form values and how magic_quotes_gpc works, (could be wrong on that)

try too remove any slashes from the filenames before they are saved,

$handle = fopen(stripslashes($filename), "w")

if that works then i'd check too see what is actually being passed too $filename, making sure there are no whitespaces at the begining or end. use trim too remove any that may be there.


hope it helps

Stuie
It's all in the manual :D

#3 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 25 August 2006 - 11:44 AM

you need to use stripslashes()

EDIT: oops u had an _ in the function

Basicaly you need to use stripslashes($inputvariable);

and that will remove all \ from the variable.
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 August 2006 - 11:46 AM

Trimming doesn't remove backslashes, it removes characters (usually whitespace and newline) from the beginning and end of values. You need to use the functions stripslashes() on the value.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users