0

I want to add this option for my users. They will be presented with a "Browse" button, they can MULTI select different files. And once they are done selecting the files.I want to collect the file names alone without any upload and pass it onto my form action webpage. process.php

Basically, my program will work on the file names.

I've browsed several posts such as this How to Get File Name from Upload Form Using jQuery?. But wasn't able to put together my knowledge on jquery and php together. Also, creating a multi file upload.

MY ATTEMPT

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 

upload_file.php

<?php
if ($_FILES["file"]["error"] > 0) {
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?> 

But this gives file name only AFTER upload is done. I do not know any other method.

Thank you for your answer.

Best Regards, John

Community
  • 1
  • 1

1 Answers1

0

Try this way: http://jsfiddle.net/p42Wv/

The global idea is, when you select a file, the file name is extracted at the same time and put in another input (in that example, the #filename one).

In your PHP code, you can get the file name by using the $_POST verb as usual.

Hope that helps.

SiZiOUS
  • 638
  • 1
  • 8
  • 9
  • Hi! Thanks for this.A small suggestion would be to make it a hidden value also :) .That's a nice way to solve this problem.How do I slect multiple files and folders (Both could be mixed). And extract the file name (without extension) for files as you've done and just the folder name for folder? – user3141741 Jun 11 '14 at 18:01
  • Here it says: http://stackoverflow.com/questions/1175347/how-can-i-select-and-upload-multiple-files-with-html-and-php-using-http-post you can add a multiple file upload BUT how do i modify this code? When i run this with this update, it gives me only the LAST file name. http://jsfiddle.net/p42Wv/1/ – user3141741 Jun 11 '14 at 18:16
  • Thank to SiziOus! I was able to come with a nice solution. http://jsfiddle.net/p42Wv/2/ The only problem is that I'm unable to include **Folder names** along with file names. :) – user3141741 Jun 11 '14 at 19:03
  • Some browsers (like Webkit - Chrome / Safari / Opera) protects the user privacy, so you can't get the folder names (you'll get **C:\fakepath\** instead). – SiZiOUS Jun 12 '14 at 07:25