I need to open a directory on my php application.
My machine has Windows XP, the unit i need to access is a shared
folder that is of the server, and is mapped on my PC as the
"G:" unit.
I have already entered the user name and password and saved them, so
that when my PC boots I can enter the unit "G" without doing anything.
The server has Windows Server 2008.
My code:
$path = 'G:\LABORATORIO2\Repositorio';
$dir = opendir($path);
I get the next error :
Warning: opendir(G:\LABORATORIO2\Repositorio) [function.opendir]:
failed to open dir: No such file or directory in
C:\xampp\htdocs\REPOHCL\laboratProtocolos.php on line 160
If i paste the address G:\LABORATORIO2\Repositorio on my file explorer, it opens perfectly.
If I change the address for an address that points to my local drive, go perfectly.
Related
I am trying to upload files into office network share folder, I have permission to my user account access the shared folder, I used UNC path to directly to upload files by move_upload_file function, It was not working I got this error.
Warning: move_uploaded_file(\\17.105.103.8\History\TQ_Books\1.PDF): failed to open stream: Invalid argument in C:\xampp\htdocs\bookshelfs\app\controllers\Books.php on line 144
after some search I mounted that shared folder on my pc, I directly used that mounted drive name to upload the file then I am getting this error
Warning: move_uploaded_file(Z:\30.PDF): failed to open stream: No such file or directory in C:\xampp\htdocs\bookshelfs\app\controllers\Books.php on line 437
I am not getting what's wrong with it, how to fix this issue? do I need to do some config with apache service? I was trying SMP protocol but It is very hard to understand for me.
UPDATE
$bookExt = pathinfo($_FILES['select_book']['name'], PATHINFO_EXTENSION);
$filename = ltrim($_POST['book_no'], '0') . '.' . $bookExt;
$tmpFilename = $_FILES['select_book']['tmp_name'];
$targetDir = '\\\\17.105.103.8\\History\\TQ_Books\\'; //before mount'
$targetDir = 'Z:'; //after mount
$fileTarget = $targetDir . $filename;
if (move_uploaded_file($tmpFilename, $fileTarget)) {
die("success, uploaded");
} else {
die("not uploaded");
}
After days of research, I got the solution. PHP cannot talk directly to the network share folder in this case you have to mount the network drive to your system. after if you mount the drive still it won't work, because the problem is here Apache server logged in by the local system user account, so you have to change it to your system user account through Apache service. (you can find apache service on windows services) you have to open windows service by admin then only you can change the account.
Then you can run this code on your PHP file to mount. all set (you can see your mounted drive after restart your pc)
exec("net use Z: \\\\netowrk_ip\\folder /user:{sharefolder_domine}\{sharefolder_username} {sharefolder_password} /persistent:no 2>&1", $output, $return_var);
var_dump($output);
I hope this answer helps others.
I am trying to upload files into office network share folder, I have permission to my user account access the shared folder, I used UNC path to directly to upload files by move_upload_file function, It was not working I got this error.
Warning: move_uploaded_file(\\17.105.103.8\History\TQ_Books\1.PDF): failed to open stream: Invalid argument in C:\xampp\htdocs\bookshelfs\app\controllers\Books.php on line 144
after some search I mounted that shared folder on my pc, I directly used that mounted drive name to upload the file then I am getting this error
Warning: move_uploaded_file(Z:\30.PDF): failed to open stream: No such file or directory in C:\xampp\htdocs\bookshelfs\app\controllers\Books.php on line 437
I am not getting what's wrong with it, how to fix this issue? do I need to do some config with apache service? I was trying SMP protocol but It is very hard to understand for me.
UPDATE
$bookExt = pathinfo($_FILES['select_book']['name'], PATHINFO_EXTENSION);
$filename = ltrim($_POST['book_no'], '0') . '.' . $bookExt;
$tmpFilename = $_FILES['select_book']['tmp_name'];
$targetDir = '\\\\17.105.103.8\\History\\TQ_Books\\'; //before mount'
$targetDir = 'Z:'; //after mount
$fileTarget = $targetDir . $filename;
if (move_uploaded_file($tmpFilename, $fileTarget)) {
die("success, uploaded");
} else {
die("not uploaded");
}
After days of research, I got the solution. PHP cannot talk directly to the network share folder in this case you have to mount the network drive to your system. after if you mount the drive still it won't work, because the problem is here Apache server logged in by the local system user account, so you have to change it to your system user account through Apache service. (you can find apache service on windows services) you have to open windows service by admin then only you can change the account.
Then you can run this code on your PHP file to mount. all set (you can see your mounted drive after restart your pc)
exec("net use Z: \\\\netowrk_ip\\folder /user:{sharefolder_domine}\{sharefolder_username} {sharefolder_password} /persistent:no 2>&1", $output, $return_var);
var_dump($output);
I hope this answer helps others.
I'm running Xampp on a Windows Server ; Apache is running as a service with a local account.
On this server, a network share is mounted as X: with specific credentials.
I want to access files located on X: and run the following code
<?php
echo shell_exec("whoami");
fopen('X:\\text.txt',"r");
?>
and get
theservername\thelocaluser
Warning: fopen(X:\text.txt) [function.fopen]: failed to open stream: No such file or directory
I tried to run Apache, not as a service but directly by launching httpd.exe ...
and the code worked.
I can't see what causes the difference between the service and the application and how to make it works.
You're not able to do this using a drive letter, as network mapped drives are for a single user only and so can't be used by services (even if you were to mount it for that user).
What you can do instead is use the UNC path directly, for example:
fopen('\\\\server\\share\\text.txt', 'r');
Note, however, that there are a few issues with PHP's filesystem access for UNC paths. One example is a bug I filed for imagettftext, but there are also issues with file_exists and is_writeable. I haven't reported the latter because as you can see from my long-outstanding bug with imagettftext, what's the point.
For network shares you should use UNC names: "//server/share/dir/file.ext"
If you use the IP or hostname it should work fine:
$isFolder = is_dir("\\\\NAS\\Main Disk");
var_dump($isFolder); //TRUE
$isFolder = is_dir("//NAS/Main Disk");
var_dump($isFolder); //TRUE
$isFolder = is_dir("N:/Main Disk");
var_dump($isFolder); //FALSE
If it helps for the future, what i did was this:
Create a local account with the same username and password as the account which you access to the network drive.
Go to Windows Services > Apache2 > Properties > Login > Log in as this account ... and specify the username and password that you created.
About the syntax, the one that worked for me was:
$fileName = '//192.168.1.10/Folder/file.txt';
I'm using php 8.
I'm running Xampp on a Windows Server ; Apache is running as a service with a local account.
On this server, a network share is mounted as X: with specific credentials.
I want to access files located on X: and run the following code
<?php
echo shell_exec("whoami");
fopen('X:\\text.txt',"r");
?>
and get
theservername\thelocaluser
Warning: fopen(X:\text.txt) [function.fopen]: failed to open stream: No such file or directory
I tried to run Apache, not as a service but directly by launching httpd.exe ...
and the code worked.
I can't see what causes the difference between the service and the application and how to make it works.
You're not able to do this using a drive letter, as network mapped drives are for a single user only and so can't be used by services (even if you were to mount it for that user).
What you can do instead is use the UNC path directly, for example:
fopen('\\\\server\\share\\text.txt', 'r');
Note, however, that there are a few issues with PHP's filesystem access for UNC paths. One example is a bug I filed for imagettftext, but there are also issues with file_exists and is_writeable. I haven't reported the latter because as you can see from my long-outstanding bug with imagettftext, what's the point.
For network shares you should use UNC names: "//server/share/dir/file.ext"
If you use the IP or hostname it should work fine:
$isFolder = is_dir("\\\\NAS\\Main Disk");
var_dump($isFolder); //TRUE
$isFolder = is_dir("//NAS/Main Disk");
var_dump($isFolder); //TRUE
$isFolder = is_dir("N:/Main Disk");
var_dump($isFolder); //FALSE
If it helps for the future, what i did was this:
Create a local account with the same username and password as the account which you access to the network drive.
Go to Windows Services > Apache2 > Properties > Login > Log in as this account ... and specify the username and password that you created.
About the syntax, the one that worked for me was:
$fileName = '//192.168.1.10/Folder/file.txt';
I'm using php 8.
I've been battling this problem for days now. I'm 100% sure is a user configuration thing somewhere but, i don't know where to look or what to change. Here is the problem. I have a file i want to read. It just contains 5 lines like "this is line 1, this is line 2 etc. It's for debugging purposes. I have 2 copies of this files. one lives locally and one lives on a network drive. I can access the file locally no problem. I can also access the file on the network drive if I specify the address as in \\192.168.0.16\geo\junk.txt. What I cannot do is to access the file via a mapped drive. as in U:\junk.txt where the U: is mapped to the 192.168... above. Below is my code. Again, after A LOT!!! of reading I've come to the conclusion that its a user perminsion thing between the machine the code lives in and the apache server that runs the code. I don't think the two are talking to each other. Just in case this is on a windows 7 machine apache 2.2.
<?php
#$theFile="\\\\192.168.0.16\\geo\\junk.txt"; #works fine
#$theFile="junk.txt"; #local file works fine
$theFile="U:\\junk.txt"; #mapped drive DOESN'T WORK AAARRRGGG!!!!!
$handle = fopen($theFile, "r");
if($handle){
while (!feof($handle)){
$buffer = fgets($handle);
echo $buffer."<br />";
}
}
?>