Amar Amar - 3 years ago 95
PHP Question

Upload file to server and return name of file

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);
}
?>

Answer Source

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"]; anf in your html you have defined file name as fileToUpload but your accessign unknown name so it should be $file = $_FILES["fileToUpload"]["name"];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download