October 22, 2024
Chicago 12, Melborne City, USA
PHP

How to upload a file to a /upload folder in PHP?


<?php
if($_SERVER["REQUEST_METHOD"] == "GET"){
    $file = $_GET["fname"];
}else{
    if(isset($_POST["content"]) && isset($_POST["file"])){
          $folder = "/uploads/";
          $file = $_POST["file"];
          $content = $_POST["content"];
          if(file_exists($file)){
               echo "<h1>That file already exists on the server</h1>";
          }else{
               file_put_contents(basename($folder, $file), $content);

          }
          
     }else{
     echo "<h1>Request has failed to be sent</h1>";
    }

}

I used file_put_contents because I thought I could just put the file there and so on.

I tried playing around with my code



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video