Solved

PHP ftp_chmod Function

Posted on 2012-04-12
6
414 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 82

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 82

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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 82

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

762 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

22 Experts available now in Live!

Get 1:1 Help Now