Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ftp problem 1

Status
Not open for further replies.

jordanking

Programmer
Sep 8, 2005
351
hello,

I am attempting to create a page that retrieves a text file for updating by the user. I have no problem retireving and placing the contents of a text file within a user form. This idea is that the user then changes some or all of the text and when they push the update button the content of the form overwrites the existing text file.

the problem I am getting is that the code breaks at fopen function. here is my code.

Code:
<?php 
$error = array();
$f_error = array();
$f_contents = array();
$success = array();

// initiate session
if (!isset($_SESSION)) {
  session_start();
  }
// check that the form has been submitted and that name and password match required values
if ($_SESSION && $_SESSION['user']=='********' && $_SESSION['pwd']=='*********'){
} else {
	header("Location: ../admin/login_fail.php"); 
	exit();
}

$MM_flag="MM_update";
if (isset($_POST[$MM_flag])) {
	// user input validation rotines here
	// ensure content is not empty and format for html
	if (empty($_POST['content'])) {
		$error['content'] = 'Please enter text for content';
	} else {
		$content = nl2br(trim($_POST['content']));
	}
} 
	
$editFormAction = $_SERVER['PHP_SELF'];

if (!$error) {
	if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "admin")) {
		// this will open an ftp connection and read the file into memory
		$ftp_server = "[URL unfurl="true"]www.c3metro.ca";[/URL]
		$ftp_user = "*********";
		$ftp_password = "********";
		$destination_directory = "text/";
		$destination_file = "history.txt";
		// set up a connection
		$conn_id = ftp_connect($ftp_server);
		// login to ftp
		$login_result = ftp_login($conn_id, $ftp_user, $ftp_password);
		// set passive
		$set_passive = ftp_pasv($conn_id, true);
		// set timeout
		$set_timeout = ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 300);
		// set up variables
		//open file for writing
		$f_handle = fopen('../text/history.txt', 'w+'); 
		if ($f_handle) { 
			// This will write to the start of the file
			fwrite($f_handle, $content); 
			// close the files
			fclose($f_handle);
		} else {
			$f_error['file'] = "File failed to open";
		}
		// close connection
		ftp_close($conn_id);
	}	
}

the following is the exact error


Code:
Warning: fopen("../text/test2.txt", "w") - Permission denied in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\admin.php[/URL] on line 49

File failed to open

I know that the user name and password are right because when i use them on any other ftp manager it works and grants me full permission. Also, I am able to read the file without a problem. I am not very familiar with php's ftp functions so I might be missing something obvious.

Any ideas?

Thank in advance
 
i can see you logging in etc to your ftp server. but then you don't do anything with it. you use fopen to open the file for writing. in the absence of any protocol qualifier this defaults to using the filesystem functions and not ftp.

the error you are seeing has nothing to do with ftp but is simply that you have not given the user under whose credentials the webserver is running, write access to the relevant directory. do this by navigating to the directory and right clicking in the folder. select security. add the user (normally IUSR_MACHINENAME for IIS) and give it write access.
 
Thanks for the reply.

I corrected the fopen call to the following

Code:
$f_handle = fopen('ftp://********:********@[URL unfurl="true"]www.c3metro.ca.com/text/history.txt',[/URL] 'w');

this is the error I got

Code:
Warning: php_network_getaddresses: gethostbyname failed in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history.php[/URL] on line 49

Warning: fopen("ftp://...@[URL unfurl="true"]www.c3metro.ca.com/text/history.txt",[/URL] "w") - No error in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history.php[/URL] on line 49

File failed to open
 
sorry, i had a .ca followed by a .com.

here is the real error after resolving that

Code:
Warning: php_hostconnect: connect failed in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history.php[/URL] on line 49

Warning: fopen("ftp://...@[URL unfurl="true"]www.c3metro.ca/text/history.txt",[/URL] "w") - Bad file descriptor in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history.php[/URL] on line 49

File failed to open
 
this error could be caused by your host server not accepting passive ftp connections.

i suggest a quick test as follows:

Code:
$ftp_server = "[URL unfurl="true"]www.c3metro.ca";[/URL]
        $ftp_user = "*********";
        $ftp_password = "********";
        $destination_directory = "text/";
        $destination_file = "history.txt";
        // set up a connection
        $conn_id = ftp_connect($ftp_server);
        // login to ftp
        $login_result = ftp_login($conn_id, $ftp_user, $ftp_password);
        // set passive
        $set_passive = ftp_pasv($conn_id, false);
        // set timeout
        $set_timeout = ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 300);
        $fname = "tmp_".uniqid();
        $fh = fopen ($fname, "wb");
        fwrite ($fh, $contents);
        fclose ($fh);
        ftp_fput($conn_id, 
                 $destination_directory.$destination_file,
                 $fname,  
                 FTP_BINARY);
        unlink ($fh);
 
I tired the test and got a whole slew of errors, I my have applied it wrong. here is what i did

Code:
if (!$error) {
	if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "admin")) {
		// this will open an ftp connection and read the file into memory
		$ftp_server = "[URL unfurl="true"]www.c3metro.ca";[/URL]
		$ftp_user = "c3metro";
		$ftp_password = "leffe4me69";
		$destination_directory = "text/";
		$destination_file = "history.txt";
        // set up a connection
        $conn_id = ftp_connect($ftp_server);
        // login to ftp
        $login_result = ftp_login($conn_id, $ftp_user, $ftp_password);
        // set passive
        $set_passive = ftp_pasv($conn_id, false);
        // set timeout
        $set_timeout = ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 300);
        $fname = "tmp_".uniqid();
        $fh = fopen ($fname, "wb");
        fwrite ($fh, "hello");
        fclose ($fh);
        ftp_fput($conn_id, 
                 $destination_directory.$destination_file,
                 $fname,  
                 FTP_BINARY);
        unlink ($fh);
		// close connection
		ftp_close($conn_id);
	}	
}

the errors were

Code:
Warning: uniqid() expects at least 1 parameter, 0 given in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history2.php[/URL] on line 47

Warning: fopen("tmp_", "wb") - Permission denied in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history2.php[/URL] on line 48

Warning: fwrite(): supplied argument is not a valid File-Handle resource in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history2.php[/URL] on line 49

Warning: fclose(): supplied argument is not a valid File-Handle resource in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history2.php[/URL] on line 50

Warning: ftp_fput() expects parameter 3 to be resource, string given in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history2.php[/URL] on line 54

Warning: unlink() failed (Permission denied) in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history2.php[/URL] on line 55

I appreciate all the effort
 
you must be using php below version 5.

fix the uniqid error by doing the following
Code:
$fname = uniqid("tmp_", true);

the other errors relate to permissions. as i said above, you have not give the webserver user write permissions to the relevant directories.
 
thanks for the help,

Yes i am using php 5

the permissions on my hosting company do not take effect immediatley. So i have to wait for it to update.

Jordan
 
your hosting company uses windows? that's unusual.

I do not think you are using php5. the error relating to uniqid is not a php5 error. it is a php4 error. php5 does not require uniqid to take parameters. check with a call to phpinfo();
 
no, you are right,

it is php 4.2.3. I got it confused with another site for the same client that I am running with the same hosting company. The permissions have all been changed. Is there a simple way for me to confirm these changes?

thanks again for your time

jk
 
I think the permission issues are resolved because I am getting a different set of errors that when i first tried the original script below

Code:
	if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "admin")) {
		// this will open an ftp connection and read the file into memory
		$ftp_server = "[URL unfurl="true"]www.c3metro.ca";[/URL]
		$ftp_user = "user";
		$ftp_password = "password";
		$destination_directory = "text/";
		$destination_file = "history.txt";
		 //set up a connection
		$conn_id = ftp_connect($ftp_server);
		if ($conn_id){
			echo "connection established<br />";
			}
		 //login to ftp
		$login_result = ftp_login($conn_id, $ftp_user, $ftp_password);
		if ($login_result){
			echo "login success<br />";
			}
		 //set passive
		$set_passive = ftp_pasv($conn_id, false);
		if ($set_passive){
			echo "passive set false success<br />";
			}
		 //set timeout
		$set_timeout = ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 300);
		if ($set_timeout){
			echo "timeout set<br />";
			}
		// set up variables
		// open file for writing
		$f_handle = fopen('ftp://user:password@[URL unfurl="true"]www.c3metro.ca/text/history.txt/',[/URL] 'w'); 
		if ($f_handle) { 
			// This will write to the start of the file
			fwrite($f_handle, $content); 
			// close the files
			fclose($f_handle);
		} else {
			$f_error['file'] = "File failed to open";
		}
		// close connection
		ftp_close($conn_id);
	}
i included some checks to see what is happening
i also have tried seting the ftp to passive false and true. same erors

the error returned from the remote server is:

Code:
connection established
login success
passive set false success
timeout set

Warning: php_network_getaddresses: gethostbyname failed in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history.php[/URL] on line 61

Warning: fopen("ftp://...@[URL unfurl="true"]www.c3metro.ca/text/history.txt/",[/URL] "w") - No error in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history.php[/URL] on line 61

File failed to open
 
again - you are opening ftp connections with the php ftp class and then ignoring those open connections and using fopen instead.

fopen uses passive mode. it appears that your ftp host may not support this. use the pure ftp test that i supplied above.
 
sorry, i am not sure what you mean by ignoring the php ftp class. Is there a way to use the connection to open the file without resupplying the username and password within the fopen call.

i copied you code exaclty and ran it by itself wihout any other php code.
Code:
<?php
$ftp_server = "[URL unfurl="true"]www.c3metro.ca";[/URL]
        $ftp_user = "user";
        $ftp_password = "password";
        $destination_directory = "text/";
        $destination_file = "history.txt";
        // set up a connection
        $conn_id = ftp_connect($ftp_server);
        // login to ftp
        $login_result = ftp_login($conn_id, $ftp_user, $ftp_password);
        // set passive
        $set_passive = ftp_pasv($conn_id, false);
        // set timeout
        $set_timeout = ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 300);
        $fname = uniqid("tmp_", true);
        $fh = fopen ($fname, "wb");
        fwrite ($fh, $contents);
        fclose ($fh);
        ftp_fput($conn_id, 
                 $destination_directory.$destination_file,
                 $fname,  
                 FTP_BINARY);
        unlink ($fh);
?>
[code]

these are the errors i got

[code]
Warning: fopen("tmp_446ca48f0d2e36.14855952", "wb") - Permission denied in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history2.php[/URL] on line 16

Notice: Undefined variable: contents in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history2.php[/URL] on line 17

Warning: fwrite(): supplied argument is not a valid File-Handle resource in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history2.php[/URL] on line 17

Warning: fclose(): supplied argument is not a valid File-Handle resource in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history2.php[/URL] on line 18

Warning: ftp_fput() expects parameter 3 to be resource, string given in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history2.php[/URL] on line 22

Warning: unlink() failed (Permission denied) in D:\Websites\[URL unfurl="true"]www.c3metro.ca\admin\history2.php[/URL] on line 23
 
Warning: fopen("tmp_446ca48f0d2e36.14855952", "wb") - Permission denied in D:\Websites\ on line 16
this means you do not have write permission for the current working directory.
Notice: Undefined variable: contents in D:\Websites\ on line 17
This is because you have not filled the $contents variable however you are intending. For a test why don't you provide some static content in the $contents variable.

All the other error messages are reflections or inevitable results of the two states set out above.

On the more general issue:

fopen uses built in wrappers and uses the passive mode. you don't use ftp_ prefix functions with fopen. for this test, if you need to use active transfer mode, then you cannot go down the fopen route. you must use the ftp library supplied with php. my code uses fopen and fwrite to create a local version of the file you want to upload. it then PUTS the file via ftp to where you want it on the remote server and finally deletes the temp file.
 
jpadie,

thank you so much for the effort, again.

I guess i need some clarification. I am looking to load in the contents of a file stored on the remote server that is saved as a .txt file within the /test/ folder. I then want to be able to display the text in a user form that will allow them to change the text and update/resave it over the original file. Maybe I am going about this all wrong. I have no trouble working with the text/display/form functions, only the ftp functions.

The permission issue is troubling because i called my hosting company and they said that they gave read/write access to the respective user and enabled the /text/ folder to be a writeable folder. I will have to call them again.

SO i guess my question is, am i attempting this the right way and just having trouble, or should I go about this a totaly different way.


thanks

JK
 
The permission issue is troubling because i called my hosting company and they said that they gave read/write access to the respective user and enabled the /text/ folder to be a writeable folder. I will have to call them again.

i think you sold them a dummy. it is the fopen call that is failing for permission invalidity and not the ftp call. i can confirm that the ftp bit works fine.

but ... now that i understand what your actual aims are - can you tell me why you are using ftp and not writing directly to the file system? is everything happening on the same server?
 
Thank you very much for the script. It works great.

why you are using ftp and not writing directly to the file system?
to be honest, because i am a rookie.

I thought that because I wanted to retrieve the original contents of the file, i would need to open it using the ftp functions.

The files are all within the same site on the server. Does that mean i do not have to use the ftp calls.
 
you do not need to use ftp, no.

use a combination of fwrite and fread (or file_get_contents). reference the file you are reading and writing to as if you were accessing it locally (i.e. using the file:// protocol and not http etc).

there are lots of examples of similar scripts in this forum. just browse through. to provide an editor for users to enter their data you might check out FCKEditor that people on this forum have recommended. I have not used it myself.

lastly - the php online manual is a great source of code snips.
 
thanks for all your help, once again, i'm off and running
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top