?
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
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses

764 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