1

am trying get size of a file by passing the file bath from javascript to an php file then return the result , problem is javascript passing file bath as the following ex, c:\fakepath\NAME OF THE FILE , so the php file getting an wrong path so always returning an false !!!

 $(document).ready(function (){


        $("#file").click(function (){


            fileBath=$("#name").val();

                $.get('newfile2.php','fileBath='+fileBath,function(data){
                alert(data);

                    });
            });
});

php file

getmax($_GET['fileBath']);

function getmax($fileBath){

    $value=filesize($fileBath);
    echo  $value;

}

as i said the return value FALSE in php file because file bath is wrong !!

uno-2017
  • 449
  • 8
  • 25

4 Answers4

4

The purpose of the file input is to allow a file to be uploaded. The structure of the visitor's file system is considered to be confidential information that websites should not need.

There is no way to bypass the fakepath substitution as it is there to protect the visitor's privacy.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
1

since arising of HTML5 browsers implement strict rules to protect user's privacy, and the fakepath is used to hide the real path of the file, even on the MAC the path is C:\\fakepath..

so you can only detect the name of the file not it's real path, try this::

var filename = $('#name').val().replace(/C:\\fakepath\\/i, '');
undefined
  • 136,817
  • 15
  • 158
  • 186
  • Not sure how this would help the OP though? He won't be able to get the file's size either way... – sandradev Jul 01 '12 at 16:37
  • @sandradev but i'm sure how, the thing is that you can have only the name of the file not it's real path. determining the size of the file can be done on server-side or by using HTML5 File API. – undefined Jul 01 '12 at 16:39
0

use php str_replace or preg_replace .it will replace the unnecessary space or whatever you want then there wont be any problem

raju
  • 207
  • 1
  • 4
  • 11
0

JavaScript solution:

document.getElementById("fileInput").files[0].name;

jQuery solution:

$("#fileInput")[0].files[0].name;
Pang
  • 8,605
  • 144
  • 77
  • 113
NoviceCoder
  • 187
  • 1
  • 15