Page 1 of 2

dirname() or basename() which is more appropriate

Posted: Sun Sep 10, 2006 7:52 am
by akimm
My problem at this point is I have been told by users on this site that you can use basename() to create files within a directory, and I now know dirname() is akin to basename. If the above is true, and I understood fully, then how would one go about using it. I will attach a sample code I typed up now and pray that its at least generally what I need.

Thanks for any help.

Code: Select all

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input name="name" type="text">
<input type="submit" name="submit">
</form>
<?php 
$name = $_POST['name'] . rand();
// $file = '/articles/' . $name . '.txt' 
$file = basename(__FILE__). '/articles/' . $name . '.txt';

fopen($file, 'a+');
fwrite($file, $name);
fclose($handle);
?>

Posted: Sun Sep 10, 2006 8:02 am
by feyd
basename() extracts the filename from a given path while dirname() extracts the path to that filename.

So my usage??

Posted: Sun Sep 10, 2006 8:03 am
by akimm
Its improper? If so could you suggest any functions that could achieve my intended goal?

Re: So my usage??

Posted: Sun Sep 10, 2006 8:09 am
by feyd
akimm wrote:Its improper?
What's improper, your code? Likely, it is unless you have "foo.php" as a directory for some reason.
akimm wrote:If so could you suggest any functions that could achieve my intended goal?
I'm not sure what your goal is, but your code snippet above would suggest using dirname() over basename() in this case. However your code has a security hole: you've used a post variable without validation and verification. This could potentially be used to corrupt other files in your webspace.

Oh

Posted: Sun Sep 10, 2006 8:17 am
by akimm
Well thank you for the security suggestion. I am just making this program for my own usage, really just to practice. But if I ever actually use this I will certainly add some validation as you've suggested, because I never actually considered that.

My intended goal is to create files on the fly. Like if I put my name sean down, I'd like the program to take the POST variable and write sean.txt, this file would contain an article I wrote so I can have an interface for my articles, so I don't need to use as much html, I can automate it so to speak.

I know as of now its not accepting the _$POST['artice'] I just need to write that part yet, but my chief concern is discovering which tool to use for this problem.

Posted: Sun Sep 10, 2006 8:55 am
by volka
They do different things

Code: Select all

<?php
$path = '/dirA/dirB/dirC/file.name';

echo 'dirname: ', dirname($path),
	"<br />\n",
	'basename: ', basename($path);
?>
so, which one do you need?

Ok

Posted: Sun Sep 10, 2006 1:17 pm
by akimm
My intended goal is to create files on the fly. Like if I put my name sean down, I'd like the program to take the POST variable and write sean.txt, this file would contain an article I wrote so I can have an interface for my articles, so I don't need to use as much html, I can automate it so to speak.

I know as of now its not accepting the _$POST['artice'] I just need to write that part yet, but my chief concern is discovering which tool to use for this problem.

Basically I am not familier enough with either one to telll you exactly which I need. Although what I want to be able to do is write files on the fly. like if I enter an article in the form and $_POST['article'] = sean's article or whatever it may be.

Is this possible?[/quote]

Posted: Sun Sep 10, 2006 2:29 pm
by volka
Did you try the example?

Problem can still..

Posted: Sun Sep 10, 2006 3:10 pm
by akimm
This file doesn't exist, so I don't think these functions are what I need. the file will be created when I select a title for the article, and submit it. Upon doing so I want to write it to my directory, then i can include it with a simple line of PHP code.

Posted: Sun Sep 10, 2006 3:22 pm
by volka
Even if so, what could possibly happen? An error message maybe? uhhh.
Tip: Doesn't matter wether the file exists or not. Please try the example.

Code: Select all

<?php // try me
$path = '/dirA/dirB/dirC/file.name';

echo 'dirname: ', dirname($path),
        "<br />\n",
        'basename: ', basename($path);
?>

This was the output

Posted: Sun Sep 10, 2006 3:42 pm
by akimm
dirname: /dirA/dirB/dirC
basename: file.name



I'm not sure if it was the intended result, but thats how it acted on my server. I checked my directory, it did not create the dirname if it was meant to, it didn't as it is now.

Re: This was the output

Posted: Sun Sep 10, 2006 3:58 pm
by Christopher
akimm wrote:dirname: /dirA/dirB/dirC
basename: file.name

I'm not sure if it was the intended result, but thats how it acted on my server.
Yes, that is exactly how it should work: dirname() give the directory portion of the string and basename() gives the filename portion of the string.
akimm wrote:I checked my directory, it did not create the dirname if it was meant to, it didn't as it is now.
It is not meant to create directories. The dirname() and basename() functions only deal with extracting sub-strings from full file path strings. They do not do anything to the file system. There are other functions for those functions (see the PHP manual for Filesystem functions).

But..

Posted: Sun Sep 10, 2006 6:56 pm
by akimm
Can those other functions do the task I need? To create files when they're not there to begin with. Because, I know for sure we can append to files, but that will do me no good.

Posted: Mon Sep 11, 2006 2:53 am
by volka

Well..

Posted: Mon Sep 11, 2006 9:15 am
by akimm
Danke Shon, Volka, but I still don't see a file function with the link you gave me that will create a file as i need..

I looked here: