为了账号安全,请及时绑定邮箱和手机立即绑定

在 PHP 中上传不会创建临时文件

在 PHP 中上传不会创建临时文件

PHP
慕田峪9158850 2023-12-15 15:05:58
我正在尝试通过上传表单将图像发送到 php 脚本,但该文件没有显示在我的临时文件夹中。我尝试搜索错误并找到了此答案。多次检查每个步骤后,我仍然发现自己没有上传文件...表格 :<form id="addImageForm" action="functions/add_image.php" method="POST" enctype="multipart/form-data">  <label for="articleImage">Select a file:</label>  <input type="file" name="articleImage" id="articleImage">  <input type="submit" class="btn btn-success" value="Submit"></form>add_image.php 脚本:<?php/* Checking request parameters */if(!isset($_FILES['articleImage']) || empty($_FILES['articleImage'])) {    echo "Error : the file is missing";    die();}/* Checking upload errors */if($_FILES['articleImage']['error'] != UPLOAD_ERR_OK) {    echo "Error : ".$_FILES['articleImage']['error'];    die();}/* Checking file size */if($_FILES['articleImage']['size'] == 0) {    echo "Error : the file is empty";    die();}/* Checking file has been uploaded to tmp */if(is_uploaded_file($_FILES['articleImage']['tmp_name'])) {    echo "Error : the file didn't upload";    die();}/* Checking file doesn't already exist */$imagePath = "img/articles/".$_FILES['articleImage']['name'];if(file_exists($imagePath)) {    echo "Error : the filename is already taken";    die();}/* Writing image */if(!move_uploaded_file($_FILES['articleImage']['tmp_name'], $imagePath)) {    echo "Error : the file coundn't be written";    die();}echo 'Success';die();?>请求已正确发送:-----------------------------45933353531894573431775954628Content-Disposition: form-data; name="articleImage"; filename="my_file.png"Content-Type: image/png[image content]-----------------------------45933353531894573431775954628--但我仍然收到“文件未上传”的消息错误...正如我之前提到的,我确实检查了我的 php conf(使用 /tmp 作为 upload_tmp_dir 编辑了正确的 php.ini 文件并将上传限制设置为 100M),并且 /tmp 上有 777 chmod。我缺少什么?
查看完整描述

1 回答

?
噜噜哒

TA贡献1784条经验 获得超7个赞

我已经复制并以某种方式测试了您的代码。您不需要执行 is_uploaded_file 调用,因为这些检查是在 move_uploaded_file 函数中进行的(https://www.php.net/manual/en/function.is-uploaded- file.php#113766)。另请检查存储图像的目录是否存在。


<?php


/* Directory where uploaded images will be placed */

$imageDir = 'img/articles';


/* Checking request parameters */

if (!isset($_FILES['articleImage']) || empty($_FILES['articleImage'])) {

    echo "Error : the file is missing";

    die();

}


/* Checking upload errors */

if ($_FILES['articleImage']['error'] != UPLOAD_ERR_OK) {

    echo "Error : " . $_FILES['articleImage']['error'];

    die();

}


/* Checking file size */

if ($_FILES['articleImage']['size'] == 0) {

    echo "Error : the file is empty";

    die();

}

/* Checking if directory exists */

if (!is_dir($imageDir)) {

    echo 'Image directory does not exist';

    die();

}


/* Checking file doesn't already exist */

$imagePath = $imageDir . $_FILES['articleImage']['name'];

if (file_exists($imagePath)) {

    echo "Error : the filename is already taken";

    die();

}


/* Writing image */

if (!move_uploaded_file($_FILES['articleImage']['tmp_name'], $imagePath)) {

    echo "Error : the file coundn't be written";

    die();

}


echo 'Success';

die();


查看完整回答
反对 回复 2023-12-15
  • 1 回答
  • 0 关注
  • 124 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信