Re: create file permission problem (solved)
| From: | tedd | Date: | Fri, 15 Jun 2007 21:17:45 +0000 |
| Subject: | Re: create file permission problem (solved) | ||
| References: | 1 2 3 4 5 6 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-257138@lists.php.net to get a copy of this message | ||
At 1:06 PM -0400 6/14/07, Daniel Brown wrote:
For the sake of people reading the archives now (and in years to come), what was the cause of the problem, Tedd?Daniel: Ok, good point -- I would have liked to have this example to review. I'm not sure of the problems I had, because they were a comedy of errors, but the following is my solution. If anyone finds anything wrong, or if I have misstated something, please feel free to comment or correct me. Remember, this works for me, but perhaps not for others. Cheers, tedd --- here's the code, please watch for line breaks --- <?php /* Virtual Host -- how to create and/or write to file To open a file for writing requires that you change its permissions to 0777. If the file is not there, then to create one requires that you change its parent directory to 0777 and then create the file. The following code works for me. */ include('config.php'); // get your FTP password and user id $server= 'ftp.yourdomain.com'; // your domain name $path = 'httpdocs/read-write/test/'; // path to the folder "test" $folder = 'test/'; // folder name $filename = 'my.txt'; // file name $tpath = $path . $filename; // total path for file $contents = "This is my text"; // give the file some text $connection = ftp_connect($server); // connection $result = ftp_login($connection, $user, $pass); // login to ftp server if ((!$connection) || (!$result)) { echo ('<p>ERROR</p>'); } else { //----------------------------------------------- echo("Open connection<br/>"); echo("Change parent directory permission to 0777 for writing<br/>"); $str="CHMOD 0777 ". $path; // change permission for directory to 0777 ftp_site($connection, $str); echo("Checking to see if file exist<br/>"); if (is_file($folder . $filename)) { echo("File found <br/>"); } else { echo("File not found<br/>"); echo("Creating file -- $filename<br/>"); } //----------------------------------------------- echo("Opening and Writing file -- $filename<br/>"); $file = fopen( $folder . $filename, "w" ); // open file fwrite( $file, $contents); // write file fclose( $file ); // close file echo("Change permission of file to 0644<br/>"); chmod($folder . $filename, 0644);
echo("Change permission for parent directory back to 0755<br/>"); $str="CHMOD 0755 ". $path; // change permission for directory back to 755
ftp_site($connection, $str);
echo("Close connection<br/><br/>");
ftp_close($connection); // close connection
//-----------------------------------------------
echo("<hr><br/>Reading file:<br/>");
$filesize = filesize($folder . $filename);
$file = fopen($folder . $filename, "r" );
$text = fread( $file, $filesize );
fclose( $file );
echo( "File name: $filename<br/> ");
echo( "File size: $filesize bytes<br/>" );
echo( "File contents:<br/>$text<br/>" );
}
?>
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com