-2

I have a website, where the user can write PHP/HTML code and save it to his computer. Everything is fine until the user types a slash (/).

The file saves into the client computer, but instead of saving the client code, it exports an PHP error (the file in the computer has a PHP code of an error). The file-saving code is the following:

$content = $_REQUEST['code'];   //Get the code

$file = "file.php";
file_put_contents($file, $content); //Writes the content into a file

header('Content-type: text/plain');
header('Content-Disposition: attachment; filename=$file');
readfile(dirname(dirname($con)) . '/'.$file);

The error only happens when the client uses slashes. Any idea on why is this happening? )-:

EDIT:

This is one of those errors:

enter image description here

The code that i tried to export was the following:

/:

The thing that worries me, is that if I type exactly the same characters in different order (:/) then they export to my computer with no errors.

Jerome
  • 115
  • 2
  • 2
  • 8
  • 9
    Is that your actual code? You have a typo on your second header. Look at your quotes. – BIOS Jun 06 '13 at 02:01
  • Use a function to replace `/` with an `_`, right at the gate. Plus that missing quote as `BIOS` stated in `header('Content-Disposition: attachment; filename=$file);` – Funk Forty Niner Jun 06 '13 at 02:15
  • Sorry, you're right, i was missing the quote. That's the actual code, but accidentally I erase the quote mark posting the code. Error posted!!! help me please. – Jerome Jun 06 '13 at 02:57
  • Your error suggest that you have problem with `readfile(\/105.2.php)` in line 24 (path with '\' and '/' ?!). Is `readfile(dirname(dirname($con)) . '/'.$file);` in line 24 ? Put `print_r($con);` before `readfile()` and show us result. – furas Jun 06 '13 at 03:31
  • Since the client is working with HTML/PHP code, if I print_r the code it displays the HTML result of the same code. But, if I print_r the example I mentioned before (/:) the result is /: ... it prints how it is supposed to export. And yes, the readfile() is in line 24 – Jerome Jun 06 '13 at 03:46

1 Answers1

1

I see only one error - problem with readfile(\/105.2.php) in line 24 - so I tested it.

$file = 'file.php';
$con = '/:';
readfile(dirname(dirname($con)) . '/'.$file);

It gives me incorrect path \/file.php as in error message.

If I use $con=':/' it gives me correct path ./file.php

I only don't know what $con is. Maybe you have dirname(dirname($content)) in your oryginal code and $content = $_REQUEST['code']; => dirname(dirname($_REQUEST['code'])) => dirname(dirname("/:")) => "\"

furas
  • 134,197
  • 12
  • 106
  • 148
  • $con was the client's PHP/HTML code. But, hey bro, I solved the problem... but I really don't know how I did it. I just add an str_replace to my code before the headers, and suddenly everything went all right. – Jerome Jun 06 '13 at 23:32
  • $con = str_replace("/", "!@%slash@%!", $con); and Poof! all the problems were gone. The thing that worries me is that i wrote that str_replace thing after the file_put_contents... which means that the code was already written in the file. – Jerome Jun 06 '13 at 23:35