I have a confusing problem with fopen and would greatly appreciate some expert advice please! I am using the below function to write serialized MySQL results to text files to prevent the need to re-query the database frequently. I pass the serialized data to the function below, along with the name for the cache file. If the file exists it should be overwritten with the new data. If it does not exist, the file should be created and the data written to it.
function cacheWriteSerializedData($data,$path_to_cache_file){
if(!$handle = fopen($path_to_cache_file,"w")){
"Error could not be created";
}
else{
fwrite($handle,serialize($data));
fclose($handle);
}
}
Now weirdly, this seems to work correctly on my Linux development machine. However, on OSX it fails and PHP reports: Warning: fopen(/Users/admin/Sites/cms/cache/cache1.txt) [function.fopen]: failed to open stream: No such file or directory in /Users/admin/Sites/cms/includes/functions_caching.php on line 15.
The cache directory exists and is writable. The file cache1.txt does not yet exist, but I believed that the w flag on fopen should create that if it doesn't exist, as it does on Linux.
Any ideas what I've done wrong, please?
Cheers,
Matt