Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Robert Walker
Robert Walker
16,514 Points

fopen problem

$my_file = "$Uid.".txt;
$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);
$string_data = serialize($lastResult);
fwrite($handle, $string_data);


     $url = $_SERVER["REQUEST_URI"];
         $keys = parse_url($url); // parse the url
         $path = explode("/", $keys['path']); // splitting the path
         $last = $path[2]; // get the value of the last element 
     $my_file = "$last.txt";

$str = file_get_contents('$my_file');
$lastResult = unserialize($str);

The above is my code but I am having problems with the last section. I am trying to open the file but keep getting bool false, however if I replace the my_file with the full file name it works perfectly even though my_file is the same string.

$my_file = "12345.txt"
$str = file_get_contents('$my_file'); // wont work
$str = file_get_contents('12345.txt'); // will work

I am so confused on why this is not working for me.

1 Answer

Aaron Graham
Aaron Graham
18,033 Points

remove the quotes around $my_file.

$str = file_get_contents($my_file);

The way you have it, you are passing the string '$my_file' to file_get_contents().

Robert Walker
Robert Walker
16,514 Points

Thanks for the quick reply Aaron, far too late here and I need to get some sleep silly mistakes again. =(