Solved

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

Posted on 2008-06-11
13
224 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 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 …

860 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