How to get file path

Discussion in 'PHP' started by PinoyIto, Feb 22, 2011.

  1. #1
    I want to get the exact local path of input file in my php code but no success

    here is the code

    <input type="file" name="img">

    <?=$_FILES[name]?>

    the above will only display the file name not the exact local path. For exampel the exact local path of the file is C:\tmp\img1.jpg it only show img1.jpg I need to get the exact path.

    I tried also the following

    <input type="hidden" name="tmppat" value=''>
    <input type='file' name='img' onchange ="tmppat.value=this.value"> still it shows the file name only...
     
    PinoyIto, Feb 22, 2011 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Generally uploaded files are in temporary directory. If you wish to specifically have the file in any other directory, then use move_uploaded_file() function.
     
    mastermunj, Feb 23, 2011 IP
  3. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #3
    As uploaded files go into temp folder of the system on server, you can simply concatenate the path as prefix:

    $full_path = sys_get_temp_dir().$_FILES['img']['tmp_name'];
    Code (markup):
    function sys_get_temp_dir() returns you the temp folder path. Secondly, temp folder of the system is defined in environment variable paths, so accessing file directly using $_FILES['img']['tmp_name'] will allow you to read same file.


    I hope it helps.
     
    Vooler, Feb 23, 2011 IP
  4. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #4
    Thanks guys for the inputs but I want to record the local machine path of the file
     
    PinoyIto, Feb 23, 2011 IP
  5. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
  6. ap2010

    ap2010 Guest

    Messages:
    41
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I am not sure what you want exactly. But I suggest that you do a:

    <?php
    var_dump($_FILES);
    ?>
    PHP:
    This gives you all the information available in the $_FILES collection. Do understand that if you are expecting that PHP will tell you that the original path of the file was "c:\documents and settings\whatever\desktop\some document.doc" -- no, it won't. Browser security/privacy issue.
     
    ap2010, Feb 23, 2011 IP
  7. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #7
    Well, I misunderstood your question first. In Internet Explorer 8 using jQuery it WORKS perfect, and returns path too. If it is working using jQuery, ofcourse it works with plain javascript as well. So for reference it is here. While your user selects the file, you can save path in hidden variable and forward to next POST data receiving script.

    NOTE : This doesn't work in Firefox, I am not sure about other browser.

    Quick Example for IE8:

    
    <script src="jquery-1.3.2.min.js"></script>
    <input type="file" name="file1" id="file1" />
    
    <input type="button" value="Check" />
    <div id="out"></div>
    
    <script>
    	$(function() {
    			$("#file1").change(function(e) {
    			var arr = $(this).val().split("\\");
    			delete arr[arr.length-1];
    			arr = arr.join("\\");
    			$("#out").html(arr);
    		});
    	});
    </script>
    
    Code (markup):
     
    Vooler, Feb 24, 2011 IP