Solved

PHP ftp_chmod Function

Posted on 2012-04-12
6
418 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
installing docker on ubuntu 6 28
php simple error message 4 22
mysql date time 14 28
How is this connection happening? 3 8
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
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 how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
The viewer will learn how to dynamically set the form action using jQuery.

785 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