how in the world to I specify a path in fopen()? I have tried

"SuperiMacHD:Library:WebServer😃ocuments😃ispatchReports:CallsToDispatch😛layAgain:Testing"

"/localhost/dispatchreports/callstodispatch/playagain/testing/"

"/testing/"

&

"./testing"
and various and sundry combinations.

What I need to happen is for PHP to look for a folder in the home directory. If it does exists, save a file to it. If the folder doesn't exist, create it and save the file in it.

Please steer me in the right direction. I am on a Mac--there ain't no C:\ drive. Please reference the path accordingly.

tanksalot

    timstring;11023709 wrote:

    What I need to happen is for PHP to look for a folder in the home directory.

    That leaves two pretty important questions unanswered:

    1. Whose home directory?

    2. Where are all of the home directories stored?

      Details, details. The home directory is the root folder of my website.

        Also, using fopen to check for a directory makes no sense as its for opening FILES. You might want to try [man]is_dir[/man] instead. Also for making a directory, you want [man]mkdir[/man].

          At this present time, the folder I want to save to already exists and I can't get fopen to find it.

            timstring;11023713 wrote:

            Details, details. The home directory is the root folder of my website.

            So, you mean it's probably at $_SERVER['DOCUMENT_ROOT']?

            timstring;11023717 wrote:

            At this present time, the folder I want to save to already exists and I can't get fopen to find it.

            That's a good sign, since using [man]fopen/man with the path to a directory makes no sense.

              fopen with a path makes all the difference in the world. Here is an excerpt from php.net:

              http://www.php.net/manual/en/function.fopen.php

              toward the middle of the page, you will see this:

              $handle = fopen("c:\folder\resource.txt", "r");

              You see the c:\folder . . . ? that's called a path. I need to figure out how to get to my folder on a Mac.

                timstring;11023737 wrote:

                that's called a path.

                Yes, it's a path to a file, not a path to a directory. All of the example paths (assuming they all made sense, anyway) in your original post were paths to directories - these are the paths that make no sense to use in conjunction with fopen().

                  Ok, Ok, I am trying to create or open a file in fopen(). The file is at the end of the path. As I said, I'm looking for the path to the file. So, if this doesn't make any sense, I have a script that dumps 67 '.csv' files every week and once a month. I need to put them in a folder so that I don't end up with 67 files in the same folder with my files for my website

                    timstring, it sounds like you need to try a little harder. I think bradgrafelman is, as usual, offering helpful suggestions. This, for instance, sounds a bit surly:

                    timstring wrote:

                    You see the c:\folder . . . ? that's called a path. I need to figure out how to get to my folder on a Mac.

                    If you are so sure that this path exists, then perhaps you could try to first make sure that you have specified it correctly in your code by doing something like this:

                    // timstring, please note that this is a path to DIRECTORY, not a FILE
                    $tims_path = "/localhost/dispatchreports/callstodispatch/playagain/testing/";
                    if (is_dir($tims_path)) {
                      die( "timstring, you cannot use fopen on a directory path." );
                    }
                    
                    // if the path is a directory and you want to put a new file there, you'll need to first check if it is writable:
                    if (!is_writable($tims_path)) {
                      die("timstring, your path is not writable so you will not be able to write anything to it");
                    }
                    
                    // you might consider providing some example code of the file you are trying to write
                    $tims_file = $tims_path . "some_file_name.txt";
                    if (!file_exists($tims_file)) {
                      echo "timstring, your file does not exist. If you want to write a file using fopen and fwrite, you should probably use w, w+, a or a+ for your mode";
                    } elseif (!is_writable($tims_file)) {
                      die("timstring, you cannot write to " . $tims_file);
                    }
                    
                    echo "I think you should be good to go here to write " . $tims_file;
                    

                    You might also consider reading some documentation. It seems to work quite well for other people.

                      Thanks much for the routine.

                      The routine is dying at the 'if (!is_writable($tims_path))' statement. I know $tims_path is valid because the script passes the 'is_dir($tims_path)', and I dropped an index file in the folder at the end of the path and copied and pasted it into the script. (Or, you are trying to trick me with the 'if' statement being backwards.) I have tried various and sundry designations for '$tims_path', and the path is always unwritable. I have been having permission problems in my 'localhost' directory, so I reset them, and that didn't help. I can save files to the folder and drop files into it, but nada from inside the PHP script.

                      So, where am I going wrong? The answer is probably obvious--it usually is.

                        If the path is not writable, then this means that whoever is running the script does not have permission to write it. If this is a linux machine and you have CLI access, you could log in and list the path you are trying to write with this command:

                        ls -dl /path/tim/wants/to/write

                        Depending on how you are running the script (I'm guessing you are using Apache), and depending on how your machine is conifgured, it may be running as a different user like apache or www-data or nobody. If those users don't have write permission for the path, the script will not be able to write the file path. You can find out which user the script is running under by checking the output of this script:

                        passthru("whoami");

                          I put your line into Terminal and didn't get any errors. However, I still can't write to the folder. I even went so far as working your magic on the localhost folder. Still won't let me write. The owner of the folder is '_www' and I set his privileges to read and write, and the script still says that the target folder is unwritable. I've been goggling the issue and have found several possibilities, but none of them agree and nothing I've tried works.

                            It doesn't really seem like you are paying any attention.

                            The command doesn't do anything but display the file or folder's ownership properties and who can write it. Like I said:

                            sneakyimp wrote:

                            you could log in and list the path you are trying to write with this command

                            The PHP script tells you who the PHP script owner is.

                            If the script owner doesn't have permission to write the directory or file you are trying to write, then you are S.O.L.

                              4 days later

                              Here's the solution: I used 'chdir($tims_path)' with $tims_path = 'DispatchReportsFolder/' and fopen() put my files in $tims_path. Now, the $1,000,000 question is: why wouldn't the script write to $tims_path without the chdir?

                              Give me another duck.

                                Write a Reply...