Jump to content

How to Create a .txt file to a specific directory path


soma56

Recommended Posts

I've figured out how to create a file that will never have been previously created through the use of time:

 

<?php
$date = date('l-jS-F-Y-h-i-s-A');
$filename = "$date".".txt";
$Content = "My text Goes Here!\r\n";

$handle = fopen($filename, 'x+');
fwrite($handle, $Content);
fclose($handle);
echo "<a href=\"$filename\">Go To File</a>";
echo "<br />";

if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo "<a href=\"$file\n\">"."$file"."</a><br />";
        }
    }
    closedir($handle);
}
?>

 

I also figured out how to create a directory using php's mkdir function. However, considering the short script above, how would it be possible to create a file to an existing directory?

Tried that and I thought that was the way to go - thank you though. What happens is that instead of writing to the directory the 'filepath' is included in the filename rather then writing inside it:

 

$filename = "tempdir".$date.".html";

 

tempdirFriday-16th-July-2010-05-43-45-PM.txt

 

I suspect it has something to do with how it's making use of fopen:

 

$handle = fopen($filename, 'x+');

You need to add the directory separator (usually a forward slash /) between  'tempdir' and your $data variable. As you'll see in the example I posted above.

 

$filename = 'tempdir/'.$date.'.txt';

Note the slash after tempdir

Tried that.

 

Warning: fwrite() expects parameter 1 to be resource, boolean given in

 

$filename = 'tempdir/'.$date.'.html';

 

I think it's because the $filename is not considering the path but rather including in the acutal name and forward slashes are not allowed.

 

The problem is here:

 

$handle = fopen($filename, 'x+');
fwrite($handle, $Content);

 

Not too sure how to figure this one out.

Try the actual full url to that directory.... i wonder if the script is pointed elsewhere thus not finding that directory... Where exactly is this script located with reference to the directory you are trying to write to?

 

try this:

$filename = 'http://Yoursite.com/subdirectory/subdirectory/'.$date.'.html';
$handle = fopen($filename, "x+");

 

 

EDIT:

Also do some error checking to see where it may be failing... I know you want to create a new file, but this will work on directories alone as well.

 

<?php
//Will check directories or files
if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
?>

Are you sure tmpdir already exists and that it is writeable. You can check that a directory/file is writable using the is_writable function. Try out this example

<?php
$directory = 'tempdir';
$filename = 'test.txt';
$filepath = $directory . DIRECTORY_SEPARATOR . $filename;
$somecontent = "Add this to the file\n";

if(file_exists($directory))
{
echo "$directory exists... ";

if(is_writable($directory))
{
	echo " and is writable!<br />";

	echo "Openning/Creating $filename... ";

	if (!$handle = fopen($filepath, 'a'))
	{
		echo "Cannot open file ($filepath)";
		exit;
	}

	echo "OK<br />";

	echo "Writing to $filename... ";

	if (fwrite($handle, $somecontent) === FALSE) {
		echo "Cannot write to file ($filename)";
		exit;
	}

	echo "OK<br />";

	echo "Success, wrote ($somecontent) to file ($filename)";

	fclose($handle);

}
else
{
	echo "but is not writable!";
}
}
else
{
	echo "$directory does not exists";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.