Solved

How to create folder so i can insert text file to that folde in my class

Posted on 2008-06-11
13
222 Views
Last Modified: 2008-06-16
php

How to create folder so i can insert text file to that folde in my class
can please so some example?
0
Comment
Question by:firekiller15
  • 9
  • 4
13 Comments
 
LVL 10

Expert Comment

by:ray-solomon
ID: 21766565
Here is a function I created that can help you.

<?php
 

$directory_name = '/home/pocourl/public_html/test/';

$filename = 'test.txt';

$file_contents = 'This is a test';
 

echo createDirectory($directory_name, $filename, $file_contents);
 

function createDirectory($directory_name, $filename, $file_contents)

{

	if (!is_dir($directory_name))

	{

		if (@mkdir($directory_name, 0755))

		{

			if (is_dir($directory_name))

			{

				// write to file

				$fp = @fopen($directory_name.$filename, 'w');

				@fwrite($fp, $file_contents);

				@fclose($fp);

				

				if (file_exists($filename))

				{

					return 'I wrote some content to "'.$directory_name.$filename.'".';

				}

			}

		}

		else

		{

			return 'Could not create a directory';

		}

	}

	else

	{

		if (is_dir($directory_name))

		{

			// write to file

			$fp = @fopen($directory_name.$filename, 'w');

			@fwrite($fp, $file_contents);

			@fclose($fp);

			

			if (file_exists($filename))

			{

				return 'I wrote some content to "'.$directory_name.$filename.'".';

			}

		}

		else

		{

			return 'Could not create a directory';

		}

	}

}

?>

Open in new window

0
 

Author Comment

by:firekiller15
ID: 21767041
why i use your method still i cannot create a folder my code as follow

$directory_name = "c:/TEST";
$fileHandle = fopen("$directory_name.test", "a");

still the same the file still not in the folder why?
0
 
LVL 10

Expert Comment

by:ray-solomon
ID: 21767247
You did not specify the location of the file properly.
I put a slash in back of the directory name and hard-coded the file name:
This is how it should be.

$directory_name = 'c:/TEST/';
$fp = fopen($directory_name.'test.txt', 'w');

http://us2.php.net/fopen
0
 

Author Comment

by:firekiller15
ID: 21775180
I do like this
###config.php####
$directory_name = "c:/TEST/";
$fileHandle = fopen(directory_name."TEST.log", "a")

###main.php ###
function LogFile($string)
{
require("config.php");
  $str = $string;
  if (fwrite($fileHandle, $str))
    echo "<br>Wrote to file \n </br>";
  else
    echo "write failed. \n";
fclose($fileHandle);
}
but i still cant see TEST folder in the drive c why?
0
 
LVL 10

Accepted Solution

by:
ray-solomon earned 250 total points
ID: 21777154
I am not sure why the previous code I provided did not work. I tested it myself.
There must be some sort of permission problem with creating files outside of an area you are not allowed to write to (usually outside your web root).

I re-wrote the code according to how you are wanting it to be like.
The code you wrote, does not have any error checking in it and was poorly written.
I provided basic examples of how error checking should always be implemented in the code you write.
This will help you learn how to write code better.
:-)

If you still cannot get this to work, then it is not a code problem, but most likely a permission issue.
<?php
 
 

/* put these constants in config.php */

define('DIRECTORY', './test/'); // with a trailing slash

define('FILENAME', 'TEST.log');
 
 

/* put all this in main.php */
 

require('config.php'); // keep this out of the function
 

function LogFile($string)

{

	if (!is_dir(DIRECTORY))

	{

		if (@mkdir(DIRECTORY, 0777))

		{

			if (is_dir(DIRECTORY))

			{

				// write to file

				$fp = @fopen(DIRECTORY.FILENAME, 'w');

				@fwrite($fp, $string);

				@fclose($fp);

				

				if (file_exists(DIRECTORY.FILENAME))

				{

					@chmod(DIRECTORY.FILENAME, 0777);

					return true;

				}

			}

		}

		else

		{

			return false;

		}

	}

	else

	{

		if (is_dir(DIRECTORY))

		{

			// write to file

			$fp = @fopen(DIRECTORY.FILENAME, 'w');

			@fwrite($fp, $string);

			@fclose($fp);

			

			if (file_exists(FILENAME))

			{

				@chmod(DIRECTORY.FILENAME, 0777);

				return true;

			}

		}

		else

		{

			return false;

		}

	}

}
 
 

/* call the function within main.php this way */

if (LogFile('This is a test'))

{

	echo 'Written to file successfully';

}

else

{

	echo 'Could not write to file';

}
 

?>

Open in new window

0
 
LVL 10

Expert Comment

by:ray-solomon
ID: 21777319
don't forget to change the directory to:

define('DIRECTORY', 'c:/test/');
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:ray-solomon
ID: 21777335
oops, I found a simple mistake in the code above.
On line 47, change it to:
if (file_exists(DIRECTORY.FILENAME))
0
 
LVL 10

Expert Comment

by:ray-solomon
ID: 21786909
did you try the code I suggested? I tested it on Windows and Linux and it worked fine.
0
 

Author Comment

by:firekiller15
ID: 21790476
i put my directory in c:
can i do like this
define('DIRECTORY', 'c:./test/');???
output i get is Could not write to file
0
 
LVL 10

Expert Comment

by:ray-solomon
ID: 21790646
no, for Windows, it would be like this instead.

define('DIRECTORY', 'c:/test/');
0
 
LVL 10

Expert Comment

by:ray-solomon
ID: 21790653
here is the code again.
<?php

 

 

/* put these constants in config.php */

define('DIRECTORY', 'c:/test/'); // with a trailing slash

define('FILENAME', 'TEST.log');

 

 

/* put all this in main.php */

 

require('config.php'); // keep this out of the function

 

function LogFile($string)

{

        if (!is_dir(DIRECTORY))

        {

                if (@mkdir(DIRECTORY, 0777))

                {

                        if (is_dir(DIRECTORY))

                        {

                                // write to file

                                $fp = @fopen(DIRECTORY.FILENAME, 'w');

                                @fwrite($fp, $string);

                                @fclose($fp);

                                

                                if (file_exists(DIRECTORY.FILENAME))

                                {

                                        @chmod(DIRECTORY.FILENAME, 0777);

                                        return true;

                                }

                        }

                }

                else

                {

                        return false;

                }

        }

        else

        {

                if (is_dir(DIRECTORY))

                {

                        // write to file

                        $fp = @fopen(DIRECTORY.FILENAME, 'w');

                        @fwrite($fp, $string);

                        @fclose($fp);

                        

                        if (file_exists(DIRECTORY.FILENAME))

                        {

                                @chmod(DIRECTORY.FILENAME, 0777);

                                return true;

                        }

                }

                else

                {

                        return false;

                }

        }

}

 

 

/* call the function within main.php this way */

if (LogFile('This is a test'))

{

        echo 'Written to file successfully';

}

else

{

        echo 'Could not write to file';

}

 

?>

Open in new window

0
 

Author Comment

by:firekiller15
ID: 21790729
>>.i put my directory in c:
>>can i do like this
>>define('DIRECTORY', 'c:./test/');???
>>output i get is Could not write to file
i able to write to file for fwrite()

but how to write to folder for file_put_contents()?
0
 
LVL 10

Expert Comment

by:ray-solomon
ID: 21792361
just replace this in both places:

 // write to file
$fp = @fopen(DIRECTORY.FILENAME, 'w');
@fwrite($fp, $string);
@fclose($fp);

to this:

file_put_contents(DIRECTORY.FILENAME, $string);


This way is the same as fwrite method, except it is all rolled into a single function instead.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
The viewer will learn how to count occurrences of each item in an array.

932 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

8 Experts available now in Live!

Get 1:1 Help Now