simple PHP filesystem path problem

I have been writing to a file in the same directory as the script with no problems. Then when it came to writing to a file in the subdirectory it failed. the code I used is here..

      $destinationfile = "./".$teacherid."/template.csv";
      echo $destinationfile;      

      $handle = fopen($destinationfile,"w+");

      echo $handle;

                      etc. etc....



echoing $destination file gives...       ./1/template.csv

and echoning the $handle gives nothing in the subdirectory case and a resource with number when working in the same directory..

I have also tried the path written as              /1/template.csv               but no luck either

Any tips anyone? Thanks
ARC_UMAsked:
Who is Participating?
 
WarpsehConnect With a Mentor Commented:
Have you tried without the ./ before your path? Also, i think that if it is a windows platform you should use the windows format ("path\\file")
0
 
Beverley PortlockConnect With a Mentor Commented:
Try this

      $dirName = dirname( $_SERVER['PHP_SELF'] );

      $destinationfile = "$dirName/".$teacherid."/template.csv";
      echo $destinationfile;      

      $handle = fopen($destinationfile,"w+");

      echo $handle;

There may be an extra '/' in there - I can't recall if dirname() leaves a trailing slash
0
 
ARC_UMAuthor Commented:
i thought that was going to fix it for sure..
now im fopening

/onlinealps/1/template.csv

but still no handle.. can anyone see what else is wrong?
0
 
Beverley PortlockCommented:
What is the full path name to the file in question?

Also try this

      $destinationfile = realpath( './1/template.csv' );

      echo $destinationfile;      

      $handle = fopen($destinationfile,"w+");

      echo $handle;
0
 
Beverley PortlockCommented:
One other thought that occurs - check permissions on both the file and the directory. If you are on Linux then PHP runs under Apache and uses the Linux username of wwwrun or www-data depending on the distro. Check these users have access to both the file and the directory.
0
All Courses

From novice to tech pro — start learning today.