dongmeixian9665 2017-09-30 11:34
浏览 76
已采纳

将文件上传到服务器并返回文件名

I'm trying to upload a file to my php server, then return the name of the file to display in the html document. But I get the following

`error: Objektet wasn't found! The requested address was not found on this server. The link on the previous page appears to be incorrect or out of date Error 404

localhost
Apache/2.4.27 (Win32) OpenSSL/1.0.2l PHP/7.1.8`

My html Doc

<html>
    <body>
        <form method="post" enctype="multipart/form-data" action="server.php">
        <input type="file" name="fileToUpload" id="fileToUpload" size="35">
        <br>
        <br>
        <input type="submit" value="Upload" name="submit">
    </body>
</html>

My php doc

<?php
header('Content-type: text/plain');
    if(isset($_POST["fileToUpload"])){
        $file = $_FILES["fileToUpload"];
        echo("File: ".$file);
    }
?>
  • 写回答

2条回答 默认 最新

  • doulan8330 2017-09-30 11:38
    关注

    You have many errors in PHP

    <?php
     if(isset($_FILES["fileToUpload"])){
            $file = $_FILES["fileToUpload"]["name"];
            echo "File: ".$file;
        }
    ?>
    

    HTML

    <html>
        <body>
            <form method="post" enctype="multipart/form-data" action="server.php">
            <input type="file" name="fileToUpload" id="fileToUpload" size="35">
            <br>
            <br>
            <input type="submit" value="Upload" name="submit">
        </body>
    </html>
    

    Errors

    1.if(isset($_POST["file"])){ its not post it should be $_FILES["fileToUpload"]) since its a file upload

    1. $file = $_FILES["file"]; and in your html you have defined file name as fileToUpload but your accessign unknown name so it should be $file = $_FILES["fileToUpload"]["name"];
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式