Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-11
13
Medium Priority
?
231 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1000 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
 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

972 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