PHP upload no slashes in path
I am trying to get the full path of an uploaded file. The php code is like this:
<?php
$destination_path = getcwd() . DIRECTORY_SEPARATOR;
$result = 0;
$target_path = $destination_path . basename($_FILES['thefile']['name']);
if(@move_uploaded_file($_FILES['thefile']['tmp_name'],*$target_path)) {
$result = 1;
}
?>
<script language="javascript" type="text/javascript">
//d = '<?php echo basename( $_FILES['thefile']['name']); ?>';
d = '<?php echo $target_path; ?>';
window.top.window.phpUpload(d);
</script>
I can open the json file with the re开发者_开发知识库m'd out line but I need the path to return it at session end. Testing with an alert the full path is shown without slashes and the initial letter 'n' of the filename missing ...
Any help much appreciated.
(Click on Names then open nset.json at this test site to see what I'm trying to do)
You are assumingly using this on Windows, where DIRECTORY_SEPARATOR
is a backslash. If the filename starts with a n
then your Javascript code will end up like this:
d = '..\path\nameoffile.txt';
Javascript unlike PHP will interpret \n
in single quoted strings.
The solution to your dilemma is either not using DIRECTORY_SEPARATOR
, or outputting a correctly escaped Javascript string:
d = <?php echo json_encode($target_path); ?>;
Do you mean the full path to the file on the client's machine? JavaScript security will not reveal that. It will just send the actual file name to the server.
精彩评论