简体   繁体   English

pdf和word文件在php中上传

[英]pdf and word file uploading in php

In here i have tried to upload jpeg,pdf ,png and word file formats in php. 在这里,我尝试以php上传jpeg,pdf,png和word文件格式。 But i could not able to upload pdf,png and word file formats.but i have successfully upload the jpeg file formats. 但是我无法上传pdf,png和word文件格式。但是我已经成功上传了jpeg文件格式。 So this is the code i have tried.please help to edit this code to upload other file formats with out the jpeg file format. 因此,这是我尝试过的代码。请帮助编辑此代码,以上载jpeg文件格式以外的其他文件格式。

<?php

include_once("db.php");

if(isset($_POST['save'])){

if(($_FILES['file']['type']=='pdf/pdf')
 ||($_FILES['file']['type']=='image/jpeg')
 ||($_FILES['file']['type']=='image/png')
  &&($_FILES['file']['size']<200000))

 {
 if($_FILES['file']['error']>0)
 {

 echo"return code :".$_FILES['file']['error'];
 }
 //else if(file_exists('upload/'.$_FILES['file']['name']))
  //{
  //echo $_FILES['file'] ['name']."Already exite";
  //}
              else if(move_uploaded_file($_FILES['file']         ['tmp_name'],'upload/'.$_FILES['file']['name']))
   {


         $part =$_FILES['file']['name'];
           $sql = mysql_query("INSERT INTO  stu                           (ptype,source,letterno,title,descrip,receiver,image) 
                 VALUES ('{$_POST['pt']}',
                         '{$_POST['so']}',
                         '{$_POST['ln']}',
                         '{$_POST['lti']}',
                         '{$_POST['dic']}',
                         '{$_POST['re']}',
                         '{$part}')");

                            //$sql=  "INSERT INTO  stu (ptype,source, letterno,                     title,descrip,receiver,image) VALUES ('$p', '$s', '$l', '$t','$d','$r','$part')"; 
                                if ($sql){
           echo"jhgjhgjh";
           //echo "successfully insert thise record";
             //echo "<script type='text/javascript'>alert('successfully    insert thise record')</script>";

                     echo "<script            type='text/javascript'>alert('successfully insert thise record')</script>";
         }
        }
          }
         }
             ?>

try changing : 尝试更改:

$_FILES['file']['type']=='pdf/pdf'

to: 至:

$_FILES['file']['type']=='application/pdf'

for doc type: 对于文档类型:

$_FILES['file']['type']=='application/msword'

for docx: 对于docx:

$_FILES['file']['type']=='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  • Note: also check if the file size of the file that you're trying to upload is not greater than allowed. 注意:还要检查您要上传的文件的文件大小是否不超过允许的大小。
  1. PDF documents have different MIME type: application/pdf , you should use it. PDF文档具有不同的MIME类型: application/pdf ,您应该使用它。
  2. Microsoft Word documents MIME type application/msword is not in list of your allowed mime types, so it is failed to get through your check. Microsoft Word文档MIME类型application/msword不在您允许的mime类型列表中,因此无法通过您的检查。
  3. You have size limit of 200kib, it is about 195kb, probably you have documents larger then this (pretty low) limit. 您的文件大小限制为200kib,约为195kb,可能您的文档大于此限制(相当低)。
  4. After raising local file limit - check your upload_max_filesize setting in php.ini since it is next size limitation boundary, you may stuck on. 提高本地文件限制后-检查php.ini中的upload_max_filesize设置,因为它是下一个大小限制边界,可能会卡住。

Besides this your code have logical problem into MIME type checking condition since you didn't group MIME checks into separate logical value. 除此之外,由于您没有将MIME检查分组为单独的逻辑值,因此您的代码在MIME类型检查条件中存在逻辑问题。 Your condition is actually: if (a OR b OR c AND d) while is should be if ((a OR b OR c) AND d) . 您的条件实际上是: if (a OR b OR c AND d) while是if ((a OR b OR c) AND d)

It will be even better to rewrite this code as: 最好将这段代码重写为:

$allowed_mime_types = array('image/png','image/jpeg','application/pdf','application/msword');
$size_limit = 200000;
if ((in_array($_FILES['file']['type'], $allowed_mime_types)) && ($_FILES['file']['size'] < $size_limit)) { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM