View Full Version : Connecting to FTP

11-12-2006, 06:27 PM
Hello. I'm trying to connect with FTP to run a CHMOD command or whatever you want to call it with PHP.


There it will explain exactly what I'm trying to do... But for now, I'll give you the 411. I've erased all coding and I'm starting from scratch just for testing. So, basically, all that coding you saw on the previous page is worthless for now.


If you scroll down on that page, you'll see how they connected with FTP. I don't understand much of any of it...

So, basically, what does every each and every part of that coding mean? How does it work when it's all put together?

Thanks in advance.

11-12-2006, 06:41 PM
If this is what you don't understand:

// Read and write for owner, nothing for everybody else
chmod("/somedir/somefile", 0600);

// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);

// Everything for owner, read and execute for others
chmod("/somedir/somefile", 0755);

// Everything for owner, read and execute for owner's group
chmod("/somedir/somefile", 0750);
then basically you have to connect to the ftp server (http://uk.php.net/ftp). Then the first part of the code:

Sets the php chmod fuction.

specifies the server path. You can look into this in more detail by using your ftp client, which may well display the server path as you browse.

Is the unix number you want to chmod to.

11-13-2006, 03:07 AM
If you read the last post of the thread I posted... the CHMOD function doesn't work on all servers sometimes or something. So, basically, I can't do that. I meant the FTP connection example they have at the bottom of the page was what I didn't understand. I understood the CHMOD command.

11-13-2006, 03:34 AM
I don't think adding in an FTP connection is the way to go. You're going to be passing your username / password in clear text over the internet on every page request. I'd shy away from that approach if possible.

I think the relevant information from php.net is this ...

Note: The current user is the user under which PHP runs. It is probably not the same user you use for normal shell or FTP access. The mode can be changed only by user who owns the file on most systems.

Note: This function will not work on remote files as the file to be examined must be accessible via the servers filesystem.

Note: When safe mode is enabled, PHP checks whether the files or directories you are about to operate on have the same UID (owner) as the script that is being executed. In addition, you cannot set the SUID, SGID and sticky bits.

My guess is your server has php safe_mode enabled and the file you are trying to chmod does not have the same owner as required to chmod the file.

11-13-2006, 04:13 AM
It wouldn't be sending a user name and password. :S. Didn't you people read the thread? :P

It will create a file and write a MySQL connection in it with the provided content in the form fields. So, it doesn't matter.

SeeIT Solutions
11-13-2006, 06:03 AM
There is another way to CHMOD and it works on different servers. I've had the problem where chmod() didnt work but this did.

Try this

//path to filename is relative to the current FTP directory so if you want to modify a file in the root called 'filename.txt' use this:
$conn_id = ftp_connect('SERVER');
$login_result = ftp_login($conn_id,'USERNAME','PASSWORD');
ftp_site($conn_id,"CHMOD 777 filename.txt");

//if the file is in a directory called directory you can use
$conn_id = ftp_connect('SERVER');
$login_result = ftp_login($conn_id,'USERNAME','PASSWORD');
ftp_site($conn_id,"CHMOD 777 directory/filename.txt");

// OR
$conn_id = ftp_connect('SERVER');
$login_result = ftp_login($conn_id,'USERNAME','PASSWORD');
ftp_chdir($conn_id, 'directory');
ftp_site($conn_id,"CHMOD 777 filename.txt");

Hope that helps

11-14-2006, 01:22 AM
if(!isset($_POST['submit'])) {
echo "Form requiring user name and password.";
} else {
The code you showed me except with a $user_name = $_POST['variable']; etc.

It'd still work if I did this, right? They'd also have to type in the path... So could it be "CHMOD 777 $path"? Wouldn't it CHMOD the directory?

SeeIT Solutions
11-15-2006, 01:12 PM
Well you have to change the username and password etc because I'm sure your users don't have them set as USERNAME and PASSWORD :p They can come from wherever you want, post variables, defines, hard coded whatever.

You can use the post submit thing, the only thing I showed was how to CHMOD a file. The username and password need to be the ftp username and password and the server has to be correct. Also, you will need to get the path right, and I don't know if alot of users (without much knowledge) would know how to fill that in.