Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Robert Walker
Robert Walker
17,146 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);




if(isset($_GET['id'])){

     $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
17,146 Points

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