PHP文件上传与PDF / DOCX而不是图像 [英] PHP fileUpload with Pdf/Docx Instead of Image

查看:148
本文介绍了PHP文件上传与PDF / DOCX而不是图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <?php 
$ target_dir =上传/;
$ target_file = $ target_dir。基名($ _ FILES [ fileToUpload] [ 名称]);
$ uploadOk = 1;
$ imageFileType = pathinfo($ target_file,PATHINFO_EXTENSION);

if(isset($ _ POST [submit])){
$ check = getimagesize($ _ FILES [fileToUpload] [tmp_name]);
if($ check!== false){
echoFile is a image - 。 $ check [mime]。 ;
$ uploadOk = 1;
} else {
echo文件不是图像。
$ uploadOk = 0;


$ b $ if(file_exists($ target_file)){
echoSorry,file already exists。;
$ uploadOk = 0;


if($ _FILES [fileToUpload] [size]> 500000){
echo抱歉,您的文件太大了。
$ uploadOk = 0;

$ b $ if if $ $ imageFileType!=jpg&& $ imageFileType!=png&& $ imageFileType!=jpeg
& & $ imageFileType!=gif){
echo对不起,只允许使用JPG,JPEG,PNG和GIF文件。
$ uploadOk = 0;

$ b $ if($ uploadOk == 0){
echoSorry,your file was uploaded。;
$ b $ else {
if(move_uploaded_file($ _ FILES [fileToUpload] [tmp_name],$ target_file)){
echoThe file。 basename($ _FILES [fileToUpload] [name])。 已上传。
} else {
echo抱歉,上传文件时出现错误。
}
}
?>

但我的主要目的是上传 Pdf& Docx 作为用户的输入,而不是图像。



我试图改变但是不能成功。任何链接或至少一个描述与一些链接我可以假设其余的为我工作(我希望如果它是一个重复,但没有问我以相同的方式问)。

$ imagefileType 改成了 $ fileType ,有用!!我刚刚跳过了图像检查,如下面的注释部分所示。即使不确定是否需要pdf / doc& docx不像图像。我很感激如果有人提供了额外的信息,我已经跳过了,或者已经很好了@ bxN5 @Ravinder Reddy。
$ b $ p

这是简单的。

 <?php 
$ target_dir =uploads /;
$ target_file = $ target_dir。基名($ _ FILES [ fileToUpload] [ 名称]);
$ uploadOk = 1;
$ fileType = pathinfo($ target_file,PATHINFO_EXTENSION);
/ *
if(isset($ _ POST [submit])){
$ check = getfilesize($ _ FILES [fileToUpload] [tmp_name]);
if($ check!== false){
echoFile is a image - 。 $ check [mime]。 ;
$ uploadOk = 1;
} else {
echo文件不是图像。
$ uploadOk = 0;


$ b if(file_exists($ target_file)){
echoSorry,file already exists。;
$ uploadOk = 0;

if($ _FILES [fileToUpload] [size]> 500000){
echoSorry,your file too large。;
$ uploadOk = 0;

if($ fileType!=pdf&& $ fileType!=doc&& $ fileType!=docx){
echo对不起,只允许PDF,DOC和DOCX文件。;
$ uploadOk = 0;

if($ uploadOk == 0){
echoSorry,your file was not uploaded。;
} else {
if(move_uploaded_file($ _ FILES [fileToUpload] [tmp_name],$ target_file)){
echoThe file。 basename($ _FILES [fileToUpload] [name])。 已上传。
} else {
echo抱歉,上传文件时出现错误。
}
}
?>


I have an example code below working for images as user's input,

<?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.";
    }
}
?>

but my main intention is to upload Pdf & Docx as user's input this time instead of images.

I tried to change but couldn't succeeded. Any link or at least a description with a couple of links I can assume the rest works for me (I hoped if it's a duplicate but not asked the same way I guess).

解决方案

Basically, I just changed all $imagefileType to $fileType and it works!! I just skipped the image check as shown with the comment section below. Even not sure if it is needed for pdf/doc & docx unlike image. I appreciate If anyone provide additional information I skipped or it's already fine this way @bxN5 @Ravinder Reddy.

It's just simple as is.

<?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.";
    }
}
?>

这篇关于PHP文件上传与PDF / DOCX而不是图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆