Solved

PHP ftp_chmod Function

Posted on 2012-04-12
6
415 Views
Last Modified: 2012-04-17
I'm trying to run a PHP script that will change the permissions on a file and am having troubles.

Below is what I have, but I'm afraid that I don't have enough knowledge to know why it's failing. It errors out each time I run it. I can FTP to this IP address no problem though. Even change permissions via my FTP client. I just need it to work here.

Thank you all!


<?php

$ftp_server="10.3.32.239";
$ftp_user_name="ftpuser";
$ftp_user_pass="password";
$file = '/uploads/test.txt';

 // set up basic connection
 $conn_id = ftp_connect($ftp_server);
// login with username and password
 $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to chmod $file to 644
 if (ftp_chmod($conn_id, 0755, $file) !== false) {
 echo "$file chmoded successfully to 755\n";
} else {
 echo "could not chmod $file\n";
}
// close the connection
 ftp_close($conn_id);

 ?>
0
Comment
Question by:brendan-amex
  • 3
  • 3
6 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37840975
I think you have to change to the directory where the file resides instead of trying to use a path to access it.  At least, that's what I had to do to get it to work.  See here: http://us2.php.net/manual/en/function.ftp-chdir.php
0
 

Author Comment

by:brendan-amex
ID: 37841073
Well it allowed me to change directories, so that's good. But still it fails when trying to update the file with chmod

Dave, can you post the code that you got to work to compare to what I have?
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 37841306
Here is my code.  There was some MySQL code to go with this but I stripped it out.  "ftphdr.inc.php" contains the connection and login code.  This code is working on a Godaddy web site right now.

<?php
		require_once "ftphdr.inc.php";
		$place = 0;
		if (ftp_chdir($conn_id, "/kbb")) {
 		  echo "Current directory is now: " . ftp_pwd($conn_id) . "<br>\n";
			$place = 1;
			} else { 
		  	echo "Couldn't change directory<br>\n";
				}
		// upload the file
		if (!isset($_REQUEST['nfname'])){$nfname = "";} else {$nfname = $_REQUEST['nfname'];}
		$destination_file = $nfname;
		$source_file = $_FILES['userfile']['tmp_name'];
		// try to change the directory to somedir
		echo "Initial directory is: " . ftp_pwd($conn_id) . "<br>\n";
		$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
		if (!$upload) {
	    	echo "FTP upload has failed!<br>";
	  		} else {
				if ($place) ftp_chmod($conn_id, 0644, $destination_file);
				echo "Uploaded $destination_file.<br>";
   			}
		ftp_chdir($conn_id, "..");	 
		echo "Exit directory is: " . ftp_pwd($conn_id) . "<br>\n";
		ftp_close($conn_id);
		} //end add photo
?>

Open in new window

0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:brendan-amex
ID: 37842834
I can't get it to work. I'm not sure why. I read that it needs to be php5 to run this command, but it definately is...
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37844013
Did you change to $file = 'test.txt'; and put the directory in the ftp_chdir($conn_id, "/uploads") part?  When you are using an FTP program, you can only change files in the current directory.  You can't enter a path and do it that way in any FTP program I have.
0
 

Author Closing Comment

by:brendan-amex
ID: 37857213
I made sure of all that, I did a chdir to where the file is and it still doesn't work. The whole reason I was trying it in PHP is because an ubuntu issue with 10.04 on new files. I setup a cron task to run that re permissions new files and it seems to work. It's really ghetto, but it works. Thanks for the help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now