d.shankar Posted December 24, 2008 Share Posted December 24, 2008 Hi all. I need to append some content to a remote file in a webserver from my server. eg: the remote file www.remotesite.com/file.txt has to ba appended with some text from my server www.myserver.com Known & Available Possibility 1)ftp to remote site and retrieve the file.txt 2)copy the content of remote file to a local temp text file. 3)make the changes and upload it back via ftp The above procedure is time consuming , is there any other fastest procedure ? Quote Link to comment Share on other sites More sharing options...
chronister Posted December 24, 2008 Share Posted December 24, 2008 Since your dealing with 2 different servers you have no choice but to use php's FTP functions. It is not that difficult to do it as I have needed to do this before. http://us3.php.net/manual/en/ref.ftp.php Take a look at the ftp functions and you can automate this with little issue. Nate Quote Link to comment Share on other sites More sharing options...
d.shankar Posted December 25, 2008 Author Share Posted December 25, 2008 Chronister, Yea i have already coded it but i want to know whether i can do this without ftp. Like chmod the file in the remote server by giving it write access so i can write to the remote file without knowing the ftp username and password. Hope you get it. Quote Link to comment Share on other sites More sharing options...
chronister Posted December 25, 2008 Share Posted December 25, 2008 To the best of my knowledge, if we are talking about 2 separate physical/virtual server machines, then no there is no way as that would be a HUGE security risk. If the 2 sites live on the same physical server, then it would probably be possible. I understand the need for this as I too have needed to do it, but it sounds a bit shady... the whole "without knowing the ftp credentials" part. In the wrong hands this kind of thing could be very bad. I mean, think about it. Would you want someone to be able to write to files on your server by using curl or some other method and not need the FTP credentials? I know I wouldn't. Nate Quote Link to comment Share on other sites More sharing options...
d.shankar Posted December 25, 2008 Author Share Posted December 25, 2008 I understand the situation but the client will refuse to give their ftp username and password instead they will rather let us to write to a single file. Let us take it this way.Can this be done ? better to ask them to give write permission on the single file so that the file can be accessed by their i and my server ip alone ? Is this possible ? Quote Link to comment Share on other sites More sharing options...
chronister Posted December 25, 2008 Share Posted December 25, 2008 I may be wrong, but I don't think their permission matters at all. I think it is more of a server security issue here. You should look into fopen, curl and the like. I believe your server config determines whether or not you can use fopen and related functions across URLS. You may suggest to them that they create a new dir that you have ftp access to and limit the access to that dir when they create a new ftp username and pass. That way there is no issue with you fooling with their existing files and you still have ftp access to the file you need. Thats the best i can come up with. Do a search on google for php write to remote server. You may find a couple things there. Nate Quote Link to comment Share on other sites More sharing options...
d.shankar Posted December 25, 2008 Author Share Posted December 25, 2008 Can a ftp account be created such that can i download and upload a file and i cannot delete any files in their server ? Quote Link to comment Share on other sites More sharing options...
chronister Posted December 25, 2008 Share Posted December 25, 2008 You would have full access to that particular directory only. You could create files there, but only in that directory. The rest of the server would be un-touchable by you. Hell, they could even create this directory below the web root.. meaning if they place public web files in /www/public_html, they could create a directory at /www or even at root that would allow you to do what you need to in that dir, but you would not even be able to run the file on the web server. For instance, if they gave you a directory that was accessible by their URL, then you could say create a script that deletes the entire /www/public_html directory by running it as a web page. But if it was below web root, then you could not run the file. If they won't go for that, then I guess you will have to tell them that if they want you to complete the work they hired you for, then they have to give access. You could create a php script to accept POST data and write that to the file. That is the only other way I can think of right now. Seems silly to me that if they hired you to do a particular job, that they should give some kind of access to allow you to complete the job. Thats all I got. Merry Christmas. Nate Quote Link to comment Share on other sites More sharing options...
BloodyMind Posted December 25, 2008 Share Posted December 25, 2008 you can make the access to ur ip address only by modifying through the .htaccess I guess not sure of the directives themselves but google it Quote Link to comment Share on other sites More sharing options...
d.shankar Posted December 25, 2008 Author Share Posted December 25, 2008 Merry Christmas Buddy !!!!!! You could create a php script to accept POST data and write that to the file. That is the only other way I can think of right now. I am curious to know more about this. You mean cURL or something. Like to know about this. What about access restrictions ? Yes bloodymind. They can add my server ip in htaccess and thats what i was planning to do. But main thing is that. I should not be able to delete their files, since i need to access only a txt file or something in the root where the main .htaccess resides, they should give me access to modify,delete,upload,download the .txt file alone. Can this be done ? Quote Link to comment Share on other sites More sharing options...
chronister Posted December 25, 2008 Share Posted December 25, 2008 Yes cURL would work or you could use raw headers. Check out this page http://www.webmasterworld.com/forum88/9933.htm Nate Quote Link to comment Share on other sites More sharing options...
d.shankar Posted December 25, 2008 Author Share Posted December 25, 2008 Read the post nate. I am a bit confused though but it seems somewhat interesting... Is that guy writing something to the remote site without knowing the remote site's ftp credentials ?? If so, this can be done by any people so won't this be a security issue ? Quote Link to comment Share on other sites More sharing options...
corbin Posted December 25, 2008 Share Posted December 25, 2008 A short example script: This would go on the remote server, pretend it's called replace.php: $username = "corbin"; $password = "hello"; if(isset($_POST['username']) && isset($_POST['pasword']) && $_POST['username'] == $username && $_POST['password'] == $password) { if(isset($_POST['action']) && $_POST['action'] == 'write') { if(isset($_POST['content'])) { $f = fopen('file.txt', 'w'); if($f) { if(fwrite($f, $_POST['content'])) { echo '1'; //I figure just echo 1 for success or something. } } } } else { //assume the action == get readfile('file.txt'); exit; } } This would be the remote file: <?php $username = "corbin"; $password = "hello" $file_content = file_get_contents('file.txt'); $errno = $errstr = null; $f = fsockopen("someplace.com", 80, $errno, $errstr, 2); if($f) { if(fwrite("POST /path/to/replace.php HTTP/1.1\r\nHost: someplace.com\r\rConnection: close\r\n\r\n" . urlencode($file_content) . "\r\n")) { while(!feof($f)) { if(fgets($f) == '1') { echo 'Success!'; } } } } else { echo 'Could not connect socket.'; } You could even change it to have an append mode and so on. FTP would definitely be an option too though. The FTP user would need file permissions only on the file that you would modify. Going over the FTP protocol instead of the HTTP probably wouldn't be much more effort or slower. Quote Link to comment Share on other sites More sharing options...
d.shankar Posted December 25, 2008 Author Share Posted December 25, 2008 That was a scintillating example but which file should reside in my server .. 1st or 2nd ? Quote Link to comment Share on other sites More sharing options...
chronister Posted December 26, 2008 Share Posted December 26, 2008 Good Show Corbin The first file would live on the remote server. It is the file that accepts POST data and then writes it to the file you wish. The second file is what you would use on your end to manipulate the file. Nate Quote Link to comment Share on other sites More sharing options...
d.shankar Posted December 26, 2008 Author Share Posted December 26, 2008 Thank you guys i will check with this and once it works. I will come and click "Topic Solved" Quote Link to comment Share on other sites More sharing options...
corbin Posted December 26, 2008 Share Posted December 26, 2008 Quote Link to comment 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.