unable to move files to mounted network share folder - php

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.

Related

How to correctly upload a file to a directory [duplicate]

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.

Write to file on shared drive from apache server PHP [duplicate]

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.

Cannot write create file using PHP file_put_contents

I'm new at the whole php thing, and I need to write a file in the server (my own computer where I am developing a web app) with some config info. Here is the code I'm using:
$filename = "config.conf";
$db_info = $db_user . "<-*->" . $db_passwd . "<-*->" . $db_name;
if (file_put_contents($filename,$db_info) === false){
$ret["error"] = "Can't create/write: $filename.";
mysqli_close($con);
echo json_encode($ret);
return;
}
When I test my web app I get the create/write error I wrote. I checked and the file is not created. My working directory is in /var/www/tests however I've checked tests and my user has read and write permissions and is owner of that folder. If I create a file with a ordinary file explorer I have no problems whatsoever. Since the function is so simple, I am at a loss. Please help!
does web server has read/write permissions (e.g. nginx uses www-data user)? try setting permissions to 777 for folder where you want to create that file.

PHP Write a variable to a txt file

I am trying to get a variable from the query string and write it to a text file.
I have tried like this:
<?php
$content=( $_GET['var'] );
echo $content;
$file = fopen("etlLOG.txt","w+");
echo fwrite($file,$content);
fclose($file);
?>
I get the following errors:
Warning: fopen(etlLOG.txt) [function.fopen]: failed to open stream: Permission denied in E:\Users\george\listener.php on line 8
Warning: fwrite(): supplied argument is not a valid stream resource in E:\Users\george\listener.php on line 9
Warning: fclose(): supplied argument is not a valid stream resource in E:\Users\george\listener.php on line 10
You may need to change the permissions as an administrator. Open up terminal on your Mac and then open the directory that etlLOG.txt is located in. Then type:
sudo chmod 777 etlLOG.txt
You may be prompted for a password. Also, it could be the directories that don't allow full access.
OR
PHP
chmod : Attempts to change the mode of the specified file to that given in mode.
chmod
Well it appears that the web server does not have access to that file. So used ftp fopen provided the correct credentials and paths and finally i did managed to access and edit the file
If you are running WAMP on windows
1) login as an administrator
2) Install wamp
3) Download subinacl.exe from microsoft:
4) grant permissions to the regular user account to manage the WAMP services: (subinacl.exe is in C:\Program Files (x86)\Windows Resource Kits\Tools)
subinacl /SERVICE \MachineName\wampapache /GRANT=domainname.com\username=F
subinacl /SERVICE \MachineName\wampmysql /GRANT=domainname.com\username=F
5) logout and log back in as the user. They should now be able to launch the WAMP taskbar application and control the WAMP service.
My guess is that PHP is trying to create the file at 'C:\WINDOWS', so the solution wouldn't be to set permissions there.
I would suggest you use a specific location, example: create a folder at the same location as your php script and use it to create the files:
$filePath = dirname(_FILE_) . "/temp_vars/etILOG.txt";
$file = fopen($filePath ,"w+");
Kind regards,
The code you are running is trying to create the etlLOG.txt in the same folder as your PHP script. You need to give Full Permission to your IUSER_ account on the E:\Users\george folder.
If you do not know how you can browse the following links
http://technet.microsoft.com/en-us/library/bb727008.aspx
http://www.wikihow.com/Change-File-Permissions-on-Windows-7
How to set 777 permission on a particular folder?

Can not opendir() network unit in php

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.

Categories