【问题标题】:php move_uploaded_file() doesn't work in productionphp move_uploaded_file() 在生产中不起作用
【发布时间】:2020-10-13 06:25:56
【问题描述】:

所以这是我的问题: 我托管在 tsrato 上,想将文件上传到我的网络服务器,我懒得创建自己的,因为这个问题已经困扰我好几天了。我认为网络服务器有一个 tmp 目录,但我无法将 tmp 目录重定向到我的上传文件夹。 那么如何将文件直接发送到 htdocs 和我的上传文件夹中。 下面的 php 代码位于另一个名为 inc 的目录中。 文件的大小和安全性无关紧要,我只想将一个文件移动到我的目录中。 这是我的网站空间的目录 /mnt/rid/XX/X2/51XX52582/htdocs/

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["userfile"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["userfile"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}

// Check file size
if ($_FILES["userfile"]["size"] > 500000000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["userfile"]["name"]). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}

【问题讨论】:

  • 如果你尝试运行这段代码会不会出错?
  • 你得到什么错误?您的uploads/ 文件夹是否可写?您是否尝试过使用完整路径?
  • 移动文件时出错,代码的最后一部分,无法正确传输文件

标签: php html file file-upload webserver


【解决方案1】:

检查您的/uploads 文件夹是否可写。如果它不可写并且您没有权限,则不会传输文件。

【讨论】:

  • 我允许我自己的文件夹,它在网络空间上,我认为问题是 tmp_directory。我不知道它在哪里。
猜你喜欢
  • 2018-07-06
  • 1970-01-01
  • 2016-10-01
  • 2019-04-09
  • 2013-02-19
  • 2016-12-14
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多