JavaScript: Getting uploaded file local path

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

JavaScript: Getting uploaded file local path

Post by m3mn0n »

I was recently trying to get the local path of a file that was uploaded but it as not availible to me via $_FILES, nor was it availible through $_POST.

So I created this small little JavaScript snipplet that will do the trick. It inserts the information from the file type textfield into a hidden type textfieild so you can parse the information from the $_POST array.

Code: Select all

<script language="javascript">
function pathgrabber()
{
	var pathInfo = document.getElementById('file').value;
	document.getElementById('pathtofile').value = pathInfo;
}
</script>
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1">
  Location of the file to upload:<br>
  <input name="file" type="file" size="75">
  <input name="pathtofile" type="hidden" id="pathtofile">
  <br>
  <input type="submit" name="Submit" value="Submit" onClick="pathgrabber()">
</form>

Handle page:

Code: Select all

<?php
if ( isset ($_POST['Submit']) && isset ($_FILES['file']) )
{

	echo '<pre>';
		print_r ($_POST); // will display contents of array
	echo '</pre>';
	
}
?>


Enjoy.
Post Reply