简体   繁体   中英

How to make file uploading work with PHP and Nginx?

I am trying to have file uploading with PHP 7.3 FPM and Nginx but it is not working. What is weird is that on my localhost this works (as long as I change the file upload path and that sort of thing). My phpinfo() says that file uploads are on. My index.php is

<html>
<body>
<form action = "upload.php" method="POST" enctype="mutipart/form-data">
<input type="file" name="file">
<button type="submit" name="submit">Upload</button>
</body>
</html>

The directory it is in is /var/www/html/ and then my upload directory is /var/www/html/uploads/ my upload.php is

<?php
if (isset($_POST["submit"]))  {
    $file = $_FILES["file"];
    $fileName = $_FILES["file"]["name"];
    $upload_folder = "/var/www/html/uploads/";
    move_uploaded_file($_FILES["file"]["tmp_name"], $upload_folder.$_FILES["file"]["name"]);
}

When I put in echo print_r($file) it returns 1. Any idea what is going wrong?

I believe you are uploading file to your "uploads" folder directly under your web folder.

  1. Make sure the uploads folder has been created in the server
  2. Make sure the uploads folder is write-permitted (the files uploaded by php are owned by and comes under the group www-data)
  3. Please use relative path (absolute path may be forbidden (or totally different from your localhost path) in hosting environments). Hence change
$upload_folder = "/var/www/html/uploads/";

to

$upload_folder = "./uploads/";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM