Solved

PHP ftp_chmod Function

Posted on 2012-04-12
6
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 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