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

php

How to create folder so i can insert text file to that folde in my class
can please so some example?
firekiller15Asked:
Who is Participating?
 
ray-solomonConnect With a Mentor Commented:
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
 
ray-solomonCommented:
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
 
firekiller15Author Commented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
ray-solomonCommented:
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
 
firekiller15Author Commented:
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
 
ray-solomonCommented:
don't forget to change the directory to:

define('DIRECTORY', 'c:/test/');
0
 
ray-solomonCommented:
oops, I found a simple mistake in the code above.
On line 47, change it to:
if (file_exists(DIRECTORY.FILENAME))
0
 
ray-solomonCommented:
did you try the code I suggested? I tested it on Windows and Linux and it worked fine.
0
 
firekiller15Author Commented:
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
 
ray-solomonCommented:
no, for Windows, it would be like this instead.

define('DIRECTORY', 'c:/test/');
0
 
ray-solomonCommented:
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
 
firekiller15Author Commented:
>>.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
 
ray-solomonCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.