Jump to content

Why does basename() work as follows?


OldWest

Recommended Posts

I am doing some study, and I am looking through an upload script.. Can someone explain to me why this is so:

 

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']

);

 

From reading the manual and looking at other tutorials, it appears basename() returns the file path name thus would strip off the actual file name in the above??

 

For example if $_FILES['userfile']['name'] was mike.jpg wouldn't basename() just snag "mike"?

 

Am I right??

 

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

 

 

Link to comment
https://forums.phpfreaks.com/topic/220900-why-does-basename-work-as-follows/
Share on other sites

I still don't know why basename() is used. I ran this test:

 

<form  method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="submit" type="submit" value="Go >>" />
</form>

<?php
if(isset($_POST['submit'])) {
  $uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename("ed.png");
echo $uploadfile;
}
?>

 

Output is: /var/www/uploads/ed.png

 

But why is basename() needed or recommended in so many examples?

 

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.