【问题标题】:Unable to Upload file in PHP无法在 PHP 中上传文件
【发布时间】:2016-02-17 04:57:32
【问题描述】:

大家好,我正在尝试在 php 中上传文件。但它没有上传文件

这里是代码

$excel = new PhpExcelReader;
if(isset($_POST["submit"]))
{
    $target_dir="../upload/";
    $target_path=$target_dir.basename($_FILES['fileToUpload']['name']);     

    //move_uploaded_file($_FILES['fileToUpload']['name'],$target_path);
    if(move_uploaded_file($_FILES['fileToUpload']['name'],$target_path))
    {
        echo basename($_FILES['fileToUpload']['name']);
    }
    else
    {
        echo "Possible file upload attack!\n";
    }
        print_r($_FILES);
    /* $handle = realpath($_FILES["fileToUpload"]["name"]);
    $excel = new PhpExcelReader;
    $excel->read($handle);
    echo $handle; */

}

这段代码总是让我陷入 else 状态。在我的 html 表单中,我还添加了 enctype="multipart/form-data" 并检查了我的 $_FILES 数组中的 $_FILES 数组,我得到了这个

Array ( [fileToUpload] => Array ( [name] => Book1.xlsx [type] 的副本 => application/vnd.openxmlformats-officedocument.spreadsheetml.sheet [tmp_name] => H:\PHP\xampp\tmp\phpF54F.tmp [error] => 0 [size] => 第13459章

【问题讨论】:

    标签: php file-upload


    【解决方案1】:

    在您的 if 条件下将其更改为 move_uploaded_file($_FILES['fileToUpload']['tmp_name'],$target_path)

    【讨论】:

    • 感谢老兄的作品,但为什么它不适用于文件名?
    • 在服务器上上传文件时,它首先存储在临时位置,move_uploaded_file 函数从临时位置获取文件并上传到给定的目标位置。
    【解决方案2】:

    试试这个。

    if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'],$target_path))
    {
        echo basename($_FILES['fileToUpload']['name']);
    } else {
        echo "Possible file upload attack!\n";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-26
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-08
      • 2012-04-26
      • 2012-11-09
      相关资源
      最近更新 更多