Solved

PHP ftp_chmod Function

Posted on 2012-04-12
6
423 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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 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.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

628 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