How to write file without 777 permission

November 15, 2009 at 11:27 am 5 comments

File writing is a very important part in PHP programming. We need to do this many time.  Every time when you do this, you need to give  777 permission to the directory  in which you are going to write the file.By this you become eligible for creating , writing,reading and deleting the file by your PHP code.  This is not a secure way. Giving this permission to any of you directory can create big security problems. And of course your server guys will never suggest you to do this.

So what to do – Either go for less featured permissions (775 , 770 etc) but they have their own restrictions. Here I give you a way to write  the file without bothering of your directory permissions. This way is to write file through FTP. PHP has a strong collection of FTP functions that are very useful in file writing. With the help of this you can easily write your file without fighting with your server guys for specific permissions.

Here is the code of file writing though PHP..

<?php

/*
################################# File Writing Through FTP ##########################
Subject      :File writing through FTP (An alternative of 777 permission )
Developed By :Hitesh Mathpal
E mail       :hitesh.mathpal@gmail.com

*/

Class ftpUtility
{

/*
* variable declarations
*/

var $ftpHost;
var $ftpUser;
var $ftpPassword;
var $ftpstream;

/*
* @param1 is the name of the FTP Host where you want to write the file
* @param2 is the user name of the FTP Host where you want to write the file
* @param3 is the password of the FTP Host where you want to write the file
*/

function __construct($host,$user,$pass)
{
$this->ftpHost=$host;
$this->ftpUser=$user;
$this->ftpPassword=$pass;
$this->ftpstream=ftp_connect($this->ftpHost);

}

/*
Checks the FTP connection.
*/

function checkFTP()
{

if(ftp_login($this->ftpstream, $this->ftpUser,$this->ftpPassword))
return true;
else
return false;

}

/*
/*@Param1 is the path where you want to write the file
/*@Param2 is the content that is to be written in the file

*/

function ftpWrite($filePath,$content)
{
$temp = tmpfile();
fwrite($temp,$content);
fseek($temp,0);

$upload=ftp_fput($this->ftpstream, $filePath,$temp,FTP_BINARY);

if($upload)
return true;
else
return false;

}

/* Close FTP stream */
function ftpclose()
{
ftp_close($this->ftpstream);
}

}

############################################### File Writing #########################################

$ftpHost=”www.example.com”;
$ftpUser=”XXXXXXXXXXXXXXXXXXX”;
$ftpPassword=”XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”;
$filePath = “public_html/testing/myfile.txt”;
$content=”My FTP test”;

$ftp=new ftpUtility();

if($ftp->checkFTP())
{

if($ftp->ftpWrite($filePath,$content))
echo “Cheers! Check your file!”;
else
echo “Error: Try again.”;

}
$ftp->ftpclose();

?>

NOTE: You can use either the FTP details of root or can create separate FTP credentials of the directory in which you wanna perform the action (Your server guys can help you in second option)

Advertisement

Entry filed under: Development. Tags: , , , , .

How to post photos in flickr through API and PHP Database engines in MySQL. MyISAM and InnoDB

5 Comments Add your own

  • 1. Sypeingence  |  November 27, 2009 at 3:07 am

    I am always looking for brandnew informations in the internet about this topic. Thankz!

    Reply
  • 2. Hitesh Mathpal  |  February 2, 2010 at 11:10 am

    Thanks for reading !

    Reply
  • 3. Martin  |  March 3, 2010 at 4:38 pm

    I get exe cancer.. Have you ever heard about phpDoc?

    Reply
    • 4. Hitesh Mathpal  |  March 5, 2010 at 7:14 pm

      Will you please explore this?

      Reply
      • 5. Martin  |  March 5, 2010 at 11:35 pm

        How you write your comments and code is just very ugly..

        “PHPDoc is an adoption of Javadoc to the PHP world.”

        Example:
        /**
        * Checks the FTP connection.
        * @returns boolean Success
        */
        function checkFTP()

        or
        /**
        * Writes the file to the FTP server.
        *
        * @param string The path where you want to write the file
        * @param string The content that is to be written in the
        * file
        * @return boolean Success
        */
        function ftpWrite($filePath,$content)

        And why do you use the deprecated var instead of public/protected/private ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed



Follow

Get every new post delivered to your Inbox.