【问题标题】:PHP fileUpload with Pdf/Docx Instead of ImagePHP fileUpload 使用 Pdf/Docx 而不是图像
【发布时间】:2017-08-24 15:42:16
【问题描述】:

我在下面有一个示例代码,用于将图像作为用户输入,

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}

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

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

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

if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";

} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

但我的主要目的是这次上传Pdf &amp; Docx 作为用户的输入而不是图片。

我试图改变,但没有成功。任何链接或至少包含几个链接的描述我可以假设其余的对我有用(我希望它是否是重复的,但不是以我猜的相同方式询问)。

【问题讨论】:

  • 更改检查文件格式/etension的条件
  • @RavinderReddy 有很多东西要改变,你可以指定一些东西(例如将'$imageFileType'改为'$fileType')等等。我不了解语言本身,也找不到合适的术语或 API。
  • tx 哥们,我在它上面..
  • 向我们展示错误或您尝试了什么

标签: php file-upload multifile-uploader


【解决方案1】:

基本上,我只是将所有$imagefileType 更改为$fileType 并且它有效!我只是跳过了图像检查,如下面的评论部分所示。甚至不确定 pdf/doc 和 docx 是否需要与图像不同。我很感激如果有人提供我跳过的其他信息,或者这种方式已经很好@bxN5 @Ravinder Reddy。

就这么简单。

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$fileType = pathinfo($target_file,PATHINFO_EXTENSION);
/*
if(isset($_POST["submit"])) {
    $check = getfilesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
*/
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
if($fileType != "pdf" && $fileType != "doc" && $fileType != "docx") {
    echo "Sorry, only PDF, DOC & DOCX files are allowed.";
    $uploadOk = 0;
}
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

【讨论】:

    猜你喜欢
    • 2012-08-06
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2019-04-12
    • 2021-07-27
    • 1970-01-01
    相关资源
    最近更新 更多