Jump to content

Archived

This topic is now archived and is closed to further replies.

nick1

Connect to network share

Recommended Posts

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*

Share this post


Link to post
Share on other sites
Possible, yes.

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

ftp_get();
http://us3.php.net/manual/en/function.ftp-get.php

ftp_close();
http://us3.php.net/manual/en/function.ftp-close.php

file_get_contents();
http://us2.php.net/function.file_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();

Share this post


Link to post
Share on other sites
try this ->[url=http://www.tweakhound.com/linux/samba/page_2.htm] http://www.tweakhound.com/linux/samba/page_2.htm[/url]

Share this post


Link to post
Share on other sites
First off, my system specs:
-Ubuntu 6 server
-LAMP installation

Why doesn't this code work?

[code=php:0]

<?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);

?>

[/code]

The only piece of documentation I could find about using the curl file:// function is located here:
http://curl.haxx.se/mail/archive-2001-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*

Share this post


Link to post
Share on other sites

Some updates:

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

[code]<?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>";

?>[/code]

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

[code]#!/bin/bash
#Test script

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


Contents of php script:

[code]<?php

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

$results = shell_exec("sh shell");

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

?>[/code]

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

Share this post


Link to post
Share on other sites
The following command executes successfully at a command line:

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

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

[code]
<?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>";
?>
[/code]

Any suggestions? Thanks,

*Nick*

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.