首页 > 解决方案 > PHP:无法将上传的文件从 tmp 移动到远程服务器上的指定文件夹?

问题描述

我正在尝试将图像上传到我的远程服务器。但它不能像在本地服务器上那样正常工作。我正在检查允许的最大文件大小并将其修改为 6MB 并检查权限。它表明我有它来上传图像。另一方面,我设置的目标目的地是$targetPath = $_SERVER['DOCUMENT_ROOT']. '/blog/uploads/' .basename($fileName);。但是,当我运行代码时,最后的 if-else 仍然反馈“no”,并且 error.log 显示

Warning: move_uploaded_file(): Unable to move 'F:\xammp\xxx\tmp\xxx.tmp' to 'uploads/' in F:\xammp\xammp\htdocs\xxx\main.php on line 47

处理上传文件和数据的 PHP 代码:

  <?php
      session_start();
      require_once('./conn.php');
      require_once('./utils.php');
    
      if(isset($_POST['submit_btn'])) {
        if(empty($_POST['title']) || empty($_POST['content']) || empty($_POST['category_id'])) {
          header("Location: ./create_post.php");
          die("please enter the information");
        }
        
        $user = getName($_SESSION['email']);
        $author = $user['name'];
        $title = $_POST['title'];
        $content = $_POST['content'];
        $categoryID = $_POST['category_id'];
        $file = $_FILES['fileToUpload'];
        $fileName = $_FILES['fileToUpload']['name'];
        $fileType = $_FILES['fileToUpload']['type'];
        $fileTmpname = $_FILES['fileToUpload']['tmp_name'];
        $fileError = $_FILES['fileToUpload']['error'];
        $fileSize = $_FILES['fileToUpload']['size'];
    
        // To split a string
        $fileExt = explode('.', $fileName);
        $fileActualExt = strtolower(end($fileExt));
        $allowFiletype = array('jpg', 'jpeg', 'png');
    
        // define the uploaded image name
        // $postImageName = time() . '_' .$fileName;
        $targetPath = $_SERVER['DOCUMENT_ROOT']. '/blog/uploads/' .basename($fileName);
    
        // Checks if a value exists in an array
        if(!in_array($fileActualExt, $allowFiletype)) {
          echo "You can not upload file of this type";
          exit();
        }
    
        // if($fileError !== 0) {
        //   echo "There was an error uploading your file";
        //   exit();
        // }
    
        if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $targetPath)) {
          // $sql = "INSERT into oscar_articles (author, title, content, image, category_id) VALUES (?, ?, ?, ?, ?)";
          // $stmt = $conn->prepare($sql);
          // $stmt->bind_param('sssss', $author, $title, $content, $postImageName, $categoryID);
          // $result = $stmt->execute();
          // if(!$result) {
          //   die('Fail to add new category');
          // } else {
          //   header("Location: ./home.php");
          // }
        } else {
          echo "no";
        };
      }
    ?>

标签: php

解决方案


推荐阅读