atiq576 Posted June 16, 2008 Share Posted June 16, 2008 Hi, I am writing a script that will write data to a file at remote server. I m provided with ftp information to connect to that server. I have written one other script where script read the file from that same ftp server and that is perfectly working. Now in this script i first read data form a local file and then connect to ftp server and try to write data there...and after writing data to that file script tries to read data from the same file it ealier wrote to for confirmation...but it returns back the error that system cant find the file... Now issue is that the server people say they havent gotten the file...we can't check if file is there as they hide the file as it gets written to that folder so we have no way to prove it... I m pasting my code down for your opinion to see if it had some problem or is there any other technique that i should use... <? $ftp_server = "server_name"; $ftp_user = "user_name"; $ftp_pass = "password"; //reading file data from local file starts here $handle = fopen("in/MR000000000104.opd", "r") or die("file not found"); while (!feof($handle)) { $strData .= fread($handle, filesize("in/MR000000000104.opd")) or die("file can't be read"); } fclose ($handle); //ends here //connecting to cb server $conn_id = ftp_connect($ftp_server); $login_result = ftp_login($conn_id, $ftp_user, $ftp_pass); ftp_pasv($conn_id, true); if ((!$conn_id) || (!$login_result)) { echo "FTP connection has failed!<br/>"; echo "Attempted to connect to $ftp_server for user $ftp_user_name"; exit; } //trying to write to file echo 'Before writing file to there server'; $handle = fopen("ftp://".$ftp_user.":".$ftp_pass."@".$ftp_server."/in/abc.opd", "w") or die("cant find file on there server to right"); fwrite($handle,$strData); fclose($handle); echo '<br/>After writing file to there server<br/>'; //this and above msg shows in our output so file written successfully //now trying to read the same file but it gives us the error $handle = fopen("ftp://".$ftp_user.":".$ftp_pass."@".$ftp_server."/in/abc.opd", "r") or die("Can't find the file on there server to read"); while (!feof($handle)) { $strData .= fread($handle, filesize("in/abc.opd")) or die("file can't be read"); } echo '<b>Data in file=</b>'.$strData; ?> I m getting this error after running above script: Before writing file to there server After writing file to there server Warning: fopen(ftp://...@server_name/in/abc.opd): failed to open stream: FTP server reports 550 /in/abc.opd: No such file. in /hsphere/local/home/myserver/uploadTest.php on line 35 Can't find the file on there server to read if you need more info let me know... any help would be appreciated... pre-thanks Atiq Quote Link to comment Share on other sites More sharing options...
btherl Posted June 17, 2008 Share Posted June 17, 2008 First you should check the return values of all your functions, especially the fwrite(). From the manul: "fwrite() returns the number of bytes written, or FALSE on error." As for the issue of not being able to see the uploaded file, if the server people need your file then you can expect their co-operation. If they can't tell your program if the file is uploaded or not, then you can at least ask them to have someone on standby to help you with debugging by telling you if it's there yet. If they don't really need your file, then you can't do that unfortunately. Another thing to try would be to test that your code works with another ftp server. Quote Link to comment Share on other sites More sharing options...
atiq576 Posted June 17, 2008 Author Share Posted June 17, 2008 Hi, Thanks for the answer... I added this code on fwrite function: $handle = fopen("ftp://".$ftp_user.":".$ftp_pass."@".$ftp_server."/in/abc.opd", "w") or die("<br/>Cant find file on there server to right"); $rv = fwrite($handle,$strData) or die("<br/>Cant write to the file on there server"); if ( $rv === false || $rv == 0 ) echo "<br/>no bytes written"; else echo "<br/>bytes written are=".$rv; fclose($handle); rest of the code is same as in above post... now the out put is: Before writing file to there server bytes written are=760 After writing file to there server Can't find the file on there server to read its giving me the no of bytes written as well... Server people simply saying that folder is writable and other people are using our service successfully... Is there any other technique to write a file to remote server in php? Regards, Atiq Quote Link to comment Share on other sites More sharing options...
btherl Posted June 18, 2008 Share Posted June 18, 2008 You might want to try http://us2.php.net/manual/en/intro.ftp.php Then you can have more control over the ftp connection, and might be able to find what's going wrong. 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.