Sam
Sam

Reputation: 305

unable to move files to mounted network share folder

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");
}

Upvotes: 0

Views: 841

Answers (1)

Sam
Sam

Reputation: 305

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.

Upvotes: 3

Related Questions