nick1 Posted September 25, 2006 Share Posted September 25, 2006 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 makePHP do this.Thank you for your time,*Nick* Quote Link to comment https://forums.phpfreaks.com/topic/22034-connect-to-network-share/ Share on other sites More sharing options...
AncientSage Posted September 25, 2006 Share Posted September 25, 2006 Possible, yes.ftp_connect();http://us3.php.net/ftp_connectftp_get();http://us3.php.net/manual/en/function.ftp-get.phpftp_close();http://us3.php.net/manual/en/function.ftp-close.phpfile_get_contents();http://us2.php.net/function.file_get_contentsOf 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(); Quote Link to comment https://forums.phpfreaks.com/topic/22034-connect-to-network-share/#findComment-98594 Share on other sites More sharing options...
vbnullchar Posted September 26, 2006 Share Posted September 26, 2006 try this ->[url=http://www.tweakhound.com/linux/samba/page_2.htm] http://www.tweakhound.com/linux/samba/page_2.htm[/url] Quote Link to comment https://forums.phpfreaks.com/topic/22034-connect-to-network-share/#findComment-98730 Share on other sites More sharing options...
nick1 Posted September 27, 2006 Author Share Posted September 27, 2006 First off, my system specs:-Ubuntu 6 server-LAMP installationWhy 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.htmlI checked the log files on the Windows server and I do not see any failed logon attempts.Thank you for your time,*Nick* Quote Link to comment https://forums.phpfreaks.com/topic/22034-connect-to-network-share/#findComment-99346 Share on other sites More sharing options...
nick1 Posted September 28, 2006 Author Share Posted September 28, 2006 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 scriptsmbclient //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* Quote Link to comment https://forums.phpfreaks.com/topic/22034-connect-to-network-share/#findComment-100388 Share on other sites More sharing options...
nick1 Posted September 28, 2006 Author Share Posted September 28, 2006 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* Quote Link to comment https://forums.phpfreaks.com/topic/22034-connect-to-network-share/#findComment-100467 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.