Jump to content


Photo

Connect to network share


  • Please log in to reply
5 replies to this topic

#1 nick1

nick1
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 25 September 2006 - 09:52 PM

Greetings,

Quick question before I head to bed:

Is it possible to retrieve a file from a remote server via a PHP script?
For example, lets say there's a log file on a remote server that you want to retrieve each day,
sort the log files contents, then dump the info into a MySQL database.

I guess the required steps would be:

1.) Connect to the remote server. Possibly a Windows share.
2.) Suppy the login credentials.
3.) Download the file from the remote server.
4.) Chop-up the file however you wish.
5.) Write info to database.

If it's possible, please point me to the appropriate functions and/or suppy advice on how to make
PHP do this.

Thank you for your time,

*Nick*

#2 AncientSage

AncientSage
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 25 September 2006 - 10:45 PM

Possible, yes.

ftp_connect();
http://us3.php.net/ftp_connect

ftp_get();
http://us3.php.net/m...ion.ftp-get.php

ftp_close();
http://us3.php.net/m...n.ftp-close.php

file_get_contents();
http://us2.php.net/f...le_get_contents

Of course, you'll need to write it to a file, but you can unlink the file after you're done. Or just let it be overwritten each time. You can also create a file, connect to ftp, download file, write to that file, close ftp, then unlink the file after writting to a db. There are a few other functions needed, but those are a good portion of the ones that you can use. You can also read a file into an array using file();



#3 vbnullchar

vbnullchar
  • Members
  • PipPipPip
  • Advanced Member
  • 428 posts
  • LocationPasig City, Philippines

Posted 26 September 2006 - 05:04 AM

try this -> http://www.tweakhound.com/linux/samba/page_2.htm
Registered Linux User #399942
Ubuntu User #14134
--
my blog

#4 nick1

nick1
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 27 September 2006 - 01:24 AM

First off, my system specs:
-Ubuntu 6 server
-LAMP installation

Why doesn't this code work?


<?php

//Connect to a Windows share and download a file.

	$ch = curl_init("file://\\\\192.168.1.105\\d$\\test.txt");

		curl_setopt($ch, CURLOPT_USERPWD, "me:pass1");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

		$results = curl_exec($ch);
		$error = curl_error($ch);

	curl_close($ch);

	print_r($error);
	print_r($results);

?>


The only piece of documentation I could find about using the curl file:// function is located here:
http://curl.haxx.se/...01-08/0025.html

I checked the log files on the Windows server and I do not see any failed logon attempts.

Thank you for your time,

*Nick*

#5 nick1

nick1
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 28 September 2006 - 06:23 PM


Some updates:

The following PHP script appears to not fully execute...

<?php

//Connect to a Windows share and download a file.

	$results = shell_exec("smbclient //192.168.206.1/d$ -U me mypasswd ; get test.txt /home/me/test.txt ; quit");

	print_r($results);
	echo "<div>$results</div>";

?>

...I checked the Windows logs and the script is connecting then immediately disconnecting and not transferring text.txt.  I think "; get test.txt /home/me/test.txt ; quit" is not being executed.  Why?

I can however issue the smbclient commands manually at the command line, one command at a time, and successfully transfer test.txt.



So next I figured I'd try writing a shell script then executing the shell script in PHP...

Contents of shell script:

#!/bin/bash
#Test script

smbclient //192.168.206.1/d$ -U me mypasswd && get test.txt /home/me/test.txt && quit


Contents of php script:

<?php

//Connect to a Windows share and download a file.

	$results = shell_exec("sh shell");

	print_r($results);
	echo "<div>$results</div>";

?>

...I checked the Windows logs and the script is connecting then immediately disconnecting and not transferring text.txt.  Again, I think "&& get test.txt /home/me/test.txt && quit" is not being executed.  Why?

Thanks,

*Nick*

#6 nick1

nick1
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 28 September 2006 - 08:32 PM

The following command executes successfully at a command line:

smbclient //192.168.206.1/d$ -U me mypasswd -c 'get test.txt' /home/me/test.txt

However the following PHP code fails and returns error "Error opening local file test.txt":

<?php //Connect to a Windows share and download a file. 

$results = shell_exec("smbclient //192.168.206.1/d$ -U me mypasswd -c 'get test.txt' /home/me/test.txt"); 

print_r($results); 
echo "<div>$results</div>"; 
?>

Any suggestions? Thanks,

*Nick*




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users