-3

I have been looking for an answer & reading since 24hrs that for security reasons this is not allowed by most of the browser but

What I am trying to do - I want to upload an image using HTML input file tag. When I select the image using file browser, I want to get the path of the chosen file to work on it into the code.

I am able to get the name of the file but not the path.

How do I get the path of the selected file?

<script type="text/javascript">
    function fileSelected(){
        var name = document.getElementById("fileToUpload").files[0].name;
        console.log(name); //gives the name of the selected file

        var val = document.getElementById("fileToUpload").files[0].value;
        console.log(val); //gives undefined
    }
</script>


<form action="vanilla.php" method="post" enctype="multipart/form-data">
    <input type="file" name="fileToUpload" id="fileToUpload" onchange="fileSelected();">
</form>

OR you can suggest any other way to upload a file using PHP

Thanks!

Ankush Rathi
  • 622
  • 1
  • 6
  • 26
  • @Jay Blanchard I don't see how this is an EXACT duplicate of "multiple file upload in php"!! This guy is asking specifically how to get the path of a file while the quoted duplicate generally asks how to upload multiple files and as a one word addition to the description, "get the path". If OP had searched "how to upload multiple files in php" he would have found the reference post. Obviously he has been searching for keywords related to "file path". down votes unwarranted. – silversunhunter Oct 05 '17 at 16:13
  • 1
    Once the OP learns how to properly upload files he will then know how to find the path of the uploaded file which is not available until `move_uploaded_file()` is invoked @silversunhunter. It isn't an EXACT (none really are if you wish to be pedantic) match, but gives the OP a starting point for actually uploading files, which he asks for *"OR you can suggest any other way to upload a file using PHP"*. If you think the DV's are unwarranted create a post on https://meta.stackoverflow.com/ to plead your case. – Jay Blanchard Oct 05 '17 at 16:31

1 Answers1

1

You need an script in PHP apart of this for process the file. (Ej: vanilla.php) the file will be included in $_FILES with the name fileToUpload, you don't need the path, just the file. With operations like move_uploaded_file you can move to the path that you want.

Example

   <?php 
     move_uploaded_file($_FILES['fileToUpload'], "/folder/photo.png");
luis moyano
  • 100
  • 4