Getting the full path with input type="file"

Hallo,

I am using for the first time the input type=“file” to get the user select a path on his system where the rest of my code will look to perform some calculations.

After not having found on this forum any answer to my problem I decided to open this thread.

This is the code I am using:


echo('
<form method="post" action="'.$_SERVER[PHP_SELF].'">
<input type="file" name="dir_to_search" size="50">
<input type="submit" name="submit" value="Search">
<br /><br />'."\
");

if (isset($_POST['submit']) && ($_POST['submit'] == 'Search')) {

	$dir_to_search = $_POST['dir_to_search'];
	echo $dir_to_search.'<br />';

}

Now, the result I got in $dir_to_search is different pending on the browser I use.

The path I select is:
C:\Documents and Settings\Ugo\Desktop\cela 007.jpg

Mozilla 1.7.2 only returns “cela 007.jpg” while IE 6 returns “C:\\Documents and Settings\\Ugo\\Desktop\\cela 007.jpg”

As I am using Mozilla as my default browser and test IE just for compatibility, I wonder how I can get the full path in Mozilla :confused:

Another problem I have is that I am forced to select a file using input type=“file”, is there any way to select a directory?

Thank you very much for your help,
Ugo

Use $_FILES instead of $_POST


if (isset($_POST['submit']))
{
if ($_POST['submit'] == 'Search')
  {
   $dir_to_search = $_FILES['dir_to_search']['name'];
   echo $dir_to_search.'<br>';
  }
}

Also do a print_r($_FILES[‘dir_to_search’]);
You’ll find 5 list of attributes assigned to $_FILE as an associated array.

Thank you for your prompt answer anjanesh, unfortunately it does not solve my problem.

The output I got after the modification you suggest is fully empty…

I then noticed I was missing the enctype=“multipart/form-data” in the form string, then changed my code as follow:


echo('
<form enctype="multipart/form-data" method="post" action="'.$_SERVER[PHP_SELF].'">
<input type="file" name="dir_to_search" size="50">
<input type="submit" name="submit" value="Search">
<br /><br />'."\
");

if (isset($_POST['submit']) && ($_POST['submit'] == 'Search')) {

	$dir_to_search = $_FILES['dir_to_search']['name'];
	echo 'file: '.$dir_to_search.'<br />';
	print_r($_FILES['dir_to_search']);
	echo '<br />';
}

Now the output on both browsers is the following:

file: cela 007.jpg
Array ( [name] => cela 007.jpg [type] => image/pjpeg [tmp_name] => C:\PHP\uploadtemp\php35.tmp [error] => 0 [size] => 397551 )

Unfortunately what I need is to get the full path (C:\Documents and Settings\Ugo\Desktop\cela 007.jpg) in the $dir_to_search variable…

Sorry to bother, if anyone can help…
Thank you so much,

The whole idea of getting the full path from a client’s system will totally be unacceptable by users. If it were possible then any site can get information from the client’s machine. This will never be possible simply due to security reasons. So I guess there is really no way of getting that path.
But if there is some way do let me know.


echo('
<form method="post" action="'.$_SERVER[PHP_SELF].'">  // note: no enctype="multipart/form-data" on this line!
<input type="file" name="dir_to_search" size="50">
<input type="submit" name="submit" value="Search">
</form>
<br /><br />'."\
");

if (isset($_POST['submit']) && ($_POST['submit'] == 'Search')) {

	$dir_to_search = $_POST['dir_to_search'];
	echo 'post: '.$dir_to_search.'<br />';
}

If I run this page with IE6, this is the result I get:
post: \\\\Dell700\\Doc\\Musica\\music.bmp

For a directory on another local PC.

If I select a file on the same system, I got:
post: D:\\ShareDocs\\Musica\\Global list.wpl

I am not sure if it can help you. Actually all I needed is being able to read all files included within the starting directory (the one choosen by the user) and create a list of them in a txt file. Its only something I wil use locally.

It looks it works here, all I need is to remember to use IE as with Mozilla it does not work…

take care,

Hi,

I search also an anser to this question. An because i have found nothing i have created a javascript that can be usefull.

Here it is.

<form method=“what you want” enctype=“do not use multipart/form-data to avoid transmission of the file contents to the server”>
<input type=“hidden” name=“full_path_real”>
<input name=“full_path_fake” type=“file” onchange=‘document.forms[0].elements[“full_path_real”].value=document.forms[0].elements[“full_path_fake”].value’>
<input value=“Go” type=“submit”>
</form>

Now use $_REQUEST[‘full_path_real’] and forgot $_REQUEST[‘full_path_fake’];

That’s all folks for all javascript able internet clients.

Hope it was what you want. In my case i am happy.