php - 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";
};
}
?>
解决方案
推荐阅读
- python - Python Tkinter 滑块
- entity-framework - EF Core 2.1 更新相关数据
- rxjs - RxJS Observable 这个场景会有内存泄漏吗?
- python - 正则表达式和 os.walk
- c# - C# 在动态属性的 LINQ GroupBy 语句中使用字符串
- mysql - 更新 MySQL 数据库列,其中元数据从一行复制到另一行
- rest - SharePoint Online 获取站点/驱动器权限 REST API
- c++ - C++中字符串的子串与索引不一致
- c# - 如何在带有 .Net 框架 4.0 版的 SharpDevelop C# 中使用 IronPython
- r - R:Validate() 中的错误:所有 GA 查询必须有标题参数