Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将文件上载选择从js读取到php?_Javascript_Php_File Upload - Fatal编程技术网

Javascript 如何将文件上载选择从js读取到php?

Javascript 如何将文件上载选择从js读取到php?,javascript,php,file-upload,Javascript,Php,File Upload,我在w3school中找到了这个示例,以显示上载文件的文件路径 我需要使用php获取所选的文件路径 我添加了带有方法“Post”的“form”和“submit”按钮,试图读取javascript值,但没有结果 “试用”按钮调用javascript函数 我想“提交”得到与“尝试”按钮相同的数据 选择要上载的文件: 单击下面的按钮以显示上面的文件上载按钮的文件路径(必须先选择一个文件) 试试看 函数myFunction(){ var x=document.getElementById(“myFi

我在w3school中找到了这个示例,以显示上载文件的文件路径

我需要使用php获取所选的文件路径

我添加了带有方法“Post”的“form”和“submit”按钮,试图读取javascript值,但没有结果

“试用”按钮调用javascript函数 我想“提交”得到与“尝试”按钮相同的数据


选择要上载的文件:
单击下面的按钮以显示上面的文件上载按钮的文件路径(必须先选择一个文件)

试试看

函数myFunction(){ var x=document.getElementById(“myFile”).value; document.getElementById(“demo”).innerHTML=x; }
你说你想用PHP获得相同的数据;然后在单击submit后读取名为$\u FILES的超全局数组。$_FILES数组包含有关正在上载的文件的信息:文件名、文件类型(MIME类型)、tmp_名称(包含上载文件的服务器上临时文件的路径)、错误和大小

<!DOCTYPE html>
    <html>
    <body>
    <form action="" method = "post" enctype="multipart/form-data">
    Select a file to upload:
    <input type="file" id="myFile" name="myFile" size="50">
    <br />
    <p>Click the button below do the display the file path of the file upload button above (you must select a file first).</p>
    <button type="button" onclick="myFunction()">Try it</button>
    <p id="demo" name="demo" type="text"></p>
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>

    <script type="text/javascript">
    function myFunction() {
        var x = document.getElementById("myFile").value;
        document.getElementById("demo").innerHTML = x;
    }
    </script>
    </body>
    </html>

    <?php
    if(isset($_POST["submit"]))
    {
        echo '<pre>';
        var_dump($_FILES);
        echo '</pre>';
        echo '<br />';

        echo 'C:\\fakepath\\' . $_FILES['myFile']['name'];

    }
    ?>

选择要上载的文件:

单击下面的按钮以显示上面的文件上载按钮的文件路径(必须先选择一个文件)

试试看


函数myFunction(){ var x=document.getElementById(“myFile”).value; document.getElementById(“demo”).innerHTML=x; }
预期的结果是什么?抱歉,我已经编辑了这篇文章并解释了更多为什么要使用
$\u GET[“demo”]
?我想读取id“demo”的值,它是由javascript函数获得的,可能是重复的,非常感谢它的工作,但仍然存在一个问题:如何获得真实路径而不是C:\fakepath\??$\u文件['myFile']['tmp\u name']这就是你所能得到的“真实路径”。它是服务器上包含上载文件的临时文件的完整路径。(上传的文件将作为临时文件存储,直到需要时为止。)
<!DOCTYPE html>
    <html>
    <body>
    <form action="" method = "post" enctype="multipart/form-data">
    Select a file to upload:
    <input type="file" id="myFile" name="myFile" size="50">
    <br />
    <p>Click the button below do the display the file path of the file upload button above (you must select a file first).</p>
    <button type="button" onclick="myFunction()">Try it</button>
    <p id="demo" name="demo" type="text"></p>
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>

    <script type="text/javascript">
    function myFunction() {
        var x = document.getElementById("myFile").value;
        document.getElementById("demo").innerHTML = x;
    }
    </script>
    </body>
    </html>

    <?php
    if(isset($_POST["submit"]))
    {
        echo '<pre>';
        var_dump($_FILES);
        echo '</pre>';
        echo '<br />';

        echo 'C:\\fakepath\\' . $_FILES['myFile']['name'];

    }
    ?>