...

View Full Version : php fopen with windows folder path



mathceleb
04-12-2011, 08:33 PM
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.


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


Any thoughts?

Fou-Lu
04-12-2011, 08:58 PM
\{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:


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.

mathceleb
04-12-2011, 10:00 PM
I tried this:


$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?

Fou-Lu
04-12-2011, 10:21 PM
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?

mathceleb
05-02-2011, 03:22 PM
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.

Fou-Lu
05-02-2011, 03:45 PM
Is that DFS map on the same server or a remote one?

mathceleb
05-02-2011, 03:49 PM
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.


$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.

Fou-Lu
05-02-2011, 04:04 PM
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.


$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).

mathceleb
05-02-2011, 04:15 PM
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.

Fou-Lu
05-02-2011, 04:22 PM
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.

mathceleb
05-02-2011, 04:25 PM
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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum