Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts

    php fopen with windows folder path

    I'm using the fopen function. However, when I supply a path, it does not recognize the file. If I put that same file in the location of the php script without a path name, it runs fine.

    I tried replacing "\" with "/" and that did not work either.

    PHP Code:
    $fname="S:\Dev\test\config.txt";
    $f fopen($fname"r");
    while ( 
    $line fgets($f1000) ) {
    echo 
    $line;
    // end while loop 

    Any thoughts?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    \{x} indicates a parsable escape character in double quotes. You need to use \\ instead of \.
    / should however work, even without an explicit call to realpath. This leads me to believe that the issue is either a cached version of the page, or a privilege problem to the file. Since it works in the local directory, this tells me that this is either an incorrect path or a cache. Error reporting will tell you if you have any problems with file opening and why:
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    You haven't trapped any errors here. fopen can easily fail, and technically fgets as well even with a valid file pointer. Fopen must be trapped to determine if it has failed as fgets is guaranteed to fail in the event that the fopen doesn't return a valid file resource.
    Don't forget to close the file when you are done with it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    I tried this:

    PHP Code:
    $fname=str_replace("\\","/",$fname); 
    That did not work. Thanks for the double back slash suggestion, as that fixed the page bombing out.

    The file now shows C:/Users/dons/Desktop/config.txt after the replace.

    My code is on a virtual host folder, could that be the problem?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Using a replacement is useless. As soon as you have a \t, it now becomes a tab. You cannot replace the \ within the \t since \t is a single char not two chars.
    If that filepath is valid and you can type it into your OS, then there will be no problem with the filepath.

    What are the results of the opened error reporting?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    Warning: fopen(C:\\Users\\mathceleb\\Desktop\\CUI05_02_09_20.csv): failed to open stream: No such file or directory in C:\vhosts\Dev\cohort\fileparse2.php on line 724 output_file_name = C:\\Users\\mathceleb\\Desktop\\CUI05_02_09_20.csv

    This is the error I got.

    C:\vhosts is actually on the S: drive.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Is that DFS map on the same server or a remote one?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    No, we are writing from the web server to the local users desktop based on a browse file input that the user specifies.

    The user presses browse, and picks a file in the folder they want to write to. The code shaves off the filename and just keeps the path.

    PHP Code:
    $extemp=trim($_REQUEST['export_location']);
    echo 
    "extemp = " $extemp "<br />";
    $extemp2=explode("\\",$extemp);
    array_pop($extemp2);
    $exloc=implode("/",$extemp2); 
    Then we tack on the filename we specify at the end.
    Last edited by mathceleb; 05-02-2011 at 03:51 PM.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by mathceleb View Post
    No, we are writing from the web server to the local users desktop based on a browse file input that the user specifies.

    The user presses browse, and picks a file in the folder they want to write to. The code shaves off the filename and just keeps the path.

    PHP Code:
    $extemp=trim($_REQUEST['export_location']);
    echo 
    "extemp = " $extemp "<br />";
    $extemp2=explode("\\",$extemp);
    array_pop($extemp2);
    $exloc=implode("/",$extemp2); 
    Then we tack on the filename we specify at the end.
    You can't do this.
    Once a file is uploaded, it now belongs to the server, and will remain in the temp directory until its been moved or pruned by the system.
    A local machine isn't related in any way to the server. Telling the server you want the 'C:' drive refers to itself. PHP can however write across network shares via UNC if you have that set up AND the user executing the PHP code has write privilege across the workstations. A server cannot otherwise write to a workstation; that would be a tremendous security hole if it were allowed. The only real potential option I can see with this is SSH, which would then require a common Local administrator that could be used on any and all workstations (and I'm not sure if windows comes with an SSH client or not :/).

    You will need to instead provide the user with a download instead. Otherwise, you need to script a client/server application so the two can communicate between each other (also requires PHP to be installed on the workstations, or use a different language).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    mathceleb (05-02-2011)

  • #9
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    Just to be clear, Trying to treat this like a windows "Save As" procedure won't work.

    I would have to create the file and then place it on a specified folder on the server after I move it from the temp directory and allow the user to download it instead. Or, I could just save to the directory where the code is at without a path which was working fine, and have the user go into this directory in Windows Explorer manually and move the outputted file themselves.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Those are your options. Your users have no choice but to either use a shared location that can be mapped to that local server (rendering the entire upload pointless since there is a now a known file), or allow them to download a local copy of the file. PHP cannot force a download to a client machine, and can't even browse a system.

    Alternatively, a client / server application (for which PHP is quite useless in comparison; use C# since .NET is pretty much native to windows) can do this for you.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #11
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Fou-Lu View Post
    Those are your options. Your users have no choice but to either use a shared location that can be mapped to that local server (rendering the entire upload pointless since there is a now a known file), or allow them to download a local copy of the file. PHP cannot force a download to a client machine, and can't even browse a system.

    Alternatively, a client / server application (for which PHP is quite useless in comparison; use C# since .NET is pretty much native to windows) can do this for you.
    Awesome, thank you!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •