soma56 Posted July 16, 2010 Share Posted July 16, 2010 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? Link to comment https://forums.phpfreaks.com/topic/207962-how-to-create-a-txt-file-to-a-specific-directory-path/ Share on other sites More sharing options...
wildteen88 Posted July 16, 2010 Share Posted July 16, 2010 how would it be possible to create a file to an existing directory? Include the path to the directory within your $filename variable, eg $filename = 'path/to/directory/'.$date.'.txt'; Link to comment https://forums.phpfreaks.com/topic/207962-how-to-create-a-txt-file-to-a-specific-directory-path/#findComment-1087148 Share on other sites More sharing options...
soma56 Posted July 16, 2010 Author Share Posted July 16, 2010 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+'); Link to comment https://forums.phpfreaks.com/topic/207962-how-to-create-a-txt-file-to-a-specific-directory-path/#findComment-1087155 Share on other sites More sharing options...
wildteen88 Posted July 16, 2010 Share Posted July 16, 2010 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 Link to comment https://forums.phpfreaks.com/topic/207962-how-to-create-a-txt-file-to-a-specific-directory-path/#findComment-1087161 Share on other sites More sharing options...
soma56 Posted July 16, 2010 Author Share Posted July 16, 2010 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. Link to comment https://forums.phpfreaks.com/topic/207962-how-to-create-a-txt-file-to-a-specific-directory-path/#findComment-1087203 Share on other sites More sharing options...
gwolgamott Posted July 16, 2010 Share Posted July 16, 2010 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/207962-how-to-create-a-txt-file-to-a-specific-directory-path/#findComment-1087214 Share on other sites More sharing options...
gwolgamott Posted July 16, 2010 Share Posted July 16, 2010 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. why would forward slashes not be allowed... Link to comment https://forums.phpfreaks.com/topic/207962-how-to-create-a-txt-file-to-a-specific-directory-path/#findComment-1087221 Share on other sites More sharing options...
soma56 Posted July 16, 2010 Author Share Posted July 16, 2010 I'm not too sure, however, when I removed the forward slash from the following code it created the file: $filename = 'tempdir/'.$date.'.html'; However, it included the name of the directory in the actual filename. Link to comment https://forums.phpfreaks.com/topic/207962-how-to-create-a-txt-file-to-a-specific-directory-path/#findComment-1087229 Share on other sites More sharing options...
wildteen88 Posted July 16, 2010 Share Posted July 16, 2010 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/207962-how-to-create-a-txt-file-to-a-specific-directory-path/#findComment-1087232 Share on other sites More sharing options...
soma56 Posted July 16, 2010 Author Share Posted July 16, 2010 Wildteen - that worked. And a Big Thanks From me. I'm not too sure why my original example didn't. Solved! Link to comment https://forums.phpfreaks.com/topic/207962-how-to-create-a-txt-file-to-a-specific-directory-path/#findComment-1087244 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.