Closed Thread Icon

Topic awaiting preservation: read remote file via FTP (PHP) (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=26023" title="Pages that link to Topic awaiting preservation: read remote file via FTP (PHP) (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: read remote file via FTP (PHP) <span class="small">(Page 1 of 1)</span>\

 
CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 06-13-2005 14:57

I am trying to open a file for read-only on a remote server. I have set the chmod of the file to 666 (and 777 just to test) and it still says that the file can not be opened for read-only.

I made a test page to just see the steps in action and it bombs out on reading the file.

Here is the code:

code:
$ftp_server = "69.93.246.225";
$ftp_user = "user";
$ftp_pass = "password";
$fpt_file = "test.txt";

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");

// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
   echo "Connected as $ftp_user@$ftp_server <br />\n";
} else {
   echo "Couldn't connect as $ftp_user\n";
}

//check passive mode
if(ftp_pasv($conn_id, true)){
	echo "Passive mode on<br />\n";
}else{
	echo "Passive mode off<br />\n";
}


// what directory are we in?
print ("Working directory : " . ftp_pwd($conn_id) . "<br />\n");

//change directories
if(@ftp_chdir($conn_id, "database")){
	echo "Changed directories <br />\n";
}else{
	echo "Can't change directories <br />\n";
}

// what directory are we in now?
print ("Working directory : " . ftp_pwd($conn_id) . "<br />\n");

//see if the playerstatus.fpt exists
if(!$fpt_file){
	echo "$fpt_file Doesn't seem to be here <br />\n";
}else{
	echo "$fpt_file is here <br />\n";
}



if(!(fopen($fpt_file,"r"))){
	print("Can't open the file");
}else{
	print("File is open");
}


// close the connection
ftp_close($conn_id);



and here it is in all it's bombed out glory:

http://development.ncs-legendforge.com/ftpTest.php

anyone see anything funky as to why it would not be able to open the file for read-only?

Thanks in advance!

Later,

C:\

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 06-13-2005 17:33

Alright you have an issue here

First is here:

code:
//see if the playerstatus.fpt exists
if(!$fpt_file){
	echo "$fpt_file Doesn't seem to be here <br />\n";
}else{
	echo "$fpt_file is here <br />\n";
}



All that's doing is seeing if the variable is set, not if the file exists. Since you're setting the file name this check is going to pass.

What you probably want to do is download the file.

if ( ftp_get($fpt_file)) {
if(!$fpt_file){
echo "$fpt_file Failed to download <br />\n";
}else{
echo "$fpt_file has been downloaded <br />\n";
}
}



.:[ Never resist a perfect moment ]:.

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 06-13-2005 18:42

OK.......changed that and it says that the file has been downloaded, but where does it download it to? It's not on the server that is running this script........sorry.........I'm confused

Well, I take that back. I used the code you supplied and it didn't do anything. Nothing showed up on the page if it failed or not. So, I just changed it to this :

code:
//try to download the file
if (ftp_get($conn_id,$fpt_file)) {
	echo "$fpt_file Failed to download <br />\n";
}else{
	echo "$fpt_file has been downloaded <br />\n";
}



is that doing the samething? Or did I just screw it up

Also, why would I want to download it if I just want to read it? Or is this just to replace the check to see if it exists or not?

Thanks in advance!

Later,

C:\

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 06-13-2005 18:48

ok........I changed it to this:

code:
$localfile = "testText.txt";

//try to download the file
if (ftp_get($conn_id,$fpt_file,$localfile, FTP_BINARY)) {
	echo "$fpt_file Failed to download <br />\n";
}else{
	echo "$fpt_file has been downloaded <br />\n";
}



and the file did download to the directory as testText.txt and it was fine. But I am just confused as to why I can't read the file from the remote server?..........

Also, it's kind of strange, if you view the page, it says "test.txt Failed to download" but if I veiew the directory from the ftp program, it is there, I can download it to my computer and it is right.

Later,

C:\



(Edited by CPrompt on 06-13-2005 18:51)

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 06-13-2005 18:53

Well there's not an ftp method for reading a file so your best bet is downloading it and then using the regular file reading methods for reading the file.

or

You can combine it all into three lines with this:

code:
// Straight from the fopen page
$handle = fopen("ftp://$ftp_user:$ftp_pass@$ftp_server/database/$fpt_file", "r");
$text = fread($handle, 4976);
echo $text;



(Note: you have fpt_file instead of ftp_file)

oh and the test was backwards. You have the failure messge if the ftp_get succeeds


.:[ Never resist a perfect moment ]:.

(Edited by bitdamaged on 06-13-2005 19:10)

(Edited by bitdamaged on 06-13-2005 19:12)

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 06-13-2005 20:42
quote:

bitdamaged said:

Well there's not an ftp method for reading a file so your best bet is downloading it and then using the regular file reading methods for reading the file.




Well, from the php.net website it says :

quote:

If you only wish to read from or write to a file on an FTP server, consider using the ftp:// wrapper with the filesystem functions which provide a simpler and more intuitive interface.



So that looks like what you are doing with the code above. Right?

quote:

bitdamaged said:

(Note: you have fpt_file instead of ftp_file)



yeah, the file that is going to be the actual file to be read is an fpt file. FoxPro memo field of the database.

quote:

bitdamaged said:

oh and the test was backwards. You have the failure messge if the ftp_get succeeds




hehe......boy that was stupid

thanks for the help!

Later,

C:\

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 06-13-2005 21:10
quote:

So that looks like what you are doing with the code above. Right?



yep.



.:[ Never resist a perfect moment ]:.

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 06-13-2005 23:06

well, i am not sure that that is going to do the trick. I'm trying to modify a script and it's starting to be kind of a pain in the ass (for me at least).

If I were to go with downloading the file to the other server, are there many security issues that I would have to worry about with these functions?

Also, what about bandwidth. This file that I would be reading, would have to be downloaded quite a bit and be overwritten. So, I am sure that is not the best way to go about getting the info from the *.fpt file.

I'm sure I'll be back

Thanks a bunch for the help!!!

Later,

C:\

« BackwardsOnwards »

Show Forum Drop Down Menu