Viewed   79 times

I am using a file upload in PHP. I want to get the Full PATH of the File Selected. Is there any way i can get the full path in PHP??

For Eg: i uploaded a image from : "c:uploadcheck.jpeg"
I want the full path.. as "c:uploadcheck.jpeg".

i tried pathinfo, $_FILES. But no help.

Thanks in advance.

 Answers

2

You probably can't. Many browsers these days don't send that information, as it can give attackers information about the user's system.

Saturday, November 12, 2022
 
2

You can't know the full path on the client system. Most browsers simply do not expose that information to webpages. Giving that information can expose information about the client system (e.g. usernames, installed software packages) and the usecases for the server knowing the path are very rare.

Sunday, August 21, 2022
 
5

First of all, the first rule of multipart Content-Type is to define a boundary that will be used as a delimiter between each part (because as the name says, it can have multiple parts). The boundary can be any string that is not contained in the content body. I will usually use a timestamp:

define('MULTIPART_BOUNDARY', '--------------------------'.microtime(true));

Once your boundary is defined, you must send it with the Content-Type header to tell the webserver what delimiter to expect:

$header = 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY;

Once that is done, you must build a proper content body that matches the HTTP specification and the header you sent. As you know, when POSTing a file from a form, you will usually have a form field name. We'll define it:

// equivalent to <input type="file" name="uploaded_file"/>
define('FORM_FIELD', 'uploaded_file'); 

Then we build the content body:

$filename = "/path/to/uploaded/file.zip";
$file_contents = file_get_contents($filename);    

$content =  "--".MULTIPART_BOUNDARY."rn".
            "Content-Disposition: form-data; name="".FORM_FIELD.""; filename="".basename($filename).""rn".
            "Content-Type: application/ziprnrn".
            $file_contents."rn";

// add some POST fields to the request too: $_POST['foo'] = 'bar'
$content .= "--".MULTIPART_BOUNDARY."rn".
            "Content-Disposition: form-data; name="foo"rnrn".
            "barrn";

// signal end of request (note the trailing "--")
$content .= "--".MULTIPART_BOUNDARY."--rn";

As you can see, we're sending the Content-Disposition header with the form-data disposition, along with the name parameter (the form field name) and the filename parameter (the original filename). It is also important to send the Content-Type header with the proper MIME type, if you want to correctly populate the $_FILES[]['type'] thingy.

If you had multiple files to upload, you just repeat the process with the $content bit, with of course, a different FORM_FIELD for each file.

Now, build the context:

$context = stream_context_create(array(
    'http' => array(
          'method' => 'POST',
          'header' => $header,
          'content' => $content,
    )
));

And execute:

file_get_contents('http://url/to/upload/handler', false, $context);

NOTE: There is no need to encode your binary file before sending it. HTTP can handle binary just fine.

Tuesday, August 9, 2022
5

Your current PHP code is for handling multiple file uploads, but your C# code is only uploading one file.

You need to modify your PHP code somewhat, removing the foreach loop:

<?php
$uploads_dir = './files'; //Directory to save the file that comes from client application.
if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["file"]["tmp_name"];
    $name = $_FILES["file"]["name"];
    move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
?>

You also need to ensure that the ./files directory exists.

I have tested the above PHP code with your C# code and it worked perfectly.

  • For more information on handling file uploads, refer to the PHP documentation.

  • For more information on uploading multiple files with C# and PHP, here are some helpful links:

    Upload files with HTTPWebrequest (multipart/form-data)

    Use Arrays in HTML Form Variables

    PHP: Uploading multiple files

    If you want something simple for uploading multiple files, you just just upload one file at a time to upload.php in a C# loop.

Wednesday, August 3, 2022
 
1

To copy the selected file name/path to different text box, first have this JS:

function CopyMe(oFileInput, sTargetID) {
    document.getElementById(sTargetID).value = oFileInput.value;
}

And it will work with such HTML:

<div>
    <input type="file" onchange="CopyMe(this, 'txtFileName');" />
</div>
<div>
    You chose: <input id="txtFileName" type="text" readonly="readonly" />
</div>

Test case: http://jsfiddle.net/yahavbr/gP7Bz/

Note that modern browsers will hide the real full path showing something like C:fakepathrealname.txt so to show only the name (which is real) change to:

function CopyMe(oFileInput, sTargetID) {
    var arrTemp = oFileInput.value.split('\');
    document.getElementById(sTargetID).value = arrTemp[arrTemp.length - 1];
}

(http://jsfiddle.net/yahavbr/gP7Bz/1/)

Monday, September 12, 2022
 
bbb
 
bbb
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :