douluo1330 2016-11-28 06:55
浏览 155
已采纳

无法使用PHP上传docs / pdf文件

I have one issue while uploading file using PHP. I can upload the image type file successfully but could not able to upload the pdf/docs type file. I am explaining my code below.

$target_dir = "../../admin/enquiry/";
 $fileName = generateRandom() . '_' . $_FILES['attachment']['name'];
 $target_file = $target_dir . basename($fileName);
 $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
 $uploadOk = 1;
 $check = getimagesize($_FILES['attachment']['tmp_name']);
 header('Content-Type: application/json');  
 if ($check !== false) {  
       $result['msg'] = "check not false.";
            //  echo json_encode(array('status' => $check['mime']));
        $uploadOk = 1;
  } else {  
        $result['msg'] = "check false.";
            //  echo json_encode(array('status' => 'upload failed'));
        $uploadOk = 0;
   }

   if (file_exists($target_file)) {  
       echo json_encode(array('status' => 'file already exists'));
       $uploadOk = 0;
   }
    if ($uploadOk == 0) {  
        //
    } else {
              if (move_uploaded_file($_FILES['attachment']['tmp_name'], $target_file)) {  
                $result['msg'] = "File has uploaded successfully.";
                $result['num'] = 1;
                $result['img'] =$fileName;
              }else{
                $result['msg'] = "Sorry, Your File could not uploaded to the directory.";
                $result['num'] = 0;
              }

            }

Here i am getting message check false. means there is some problem with getimagesize. I can also passing the file size 138kb but in case of image its uploading successfully but other type file could not upload but i need to upload those.Please help me.

  • 写回答

3条回答 默认 最新

  • dongshan3759 2016-11-28 06:59
    关注

    At the very top of the file, add these code to enable php error.

    error_reporting(-1);
    ini_set('display_errors', 'On');
    set_error_handler("var_dump");
    
    
      $target_dir = "../../admin/enquiry/";
      if(isset($_FILES['attachment'])){
       $errors= array();
       $file_name =  generateRandom() . '_' .$_FILES['attachment']['name'];
       $target_file = $target_dir . basename($file_name);
       $file_size = $_FILES['attachment']['size'];
       $file_tmp = $_FILES['attachment']['tmp_name'];
       $file_type = $_FILES['attachment']['type'];
       $file_ext=strtolower(end(explode('.',$_FILES['attachment']['name'])));
    
       $extension= array("jpeg","jpg","png","pdf");
    
       if(in_array($file_ext,$extension)=== false){
           $errors[]="extension not allowed, please choose a JPEG,PNG or PDF file.";
       }
    
       if($file_size > 2097152) {
           $errors[]='File size must be exactely 2 MB';
       }
    
       if(empty($errors)==true) {
           move_uploaded_file($file_tmp,$target_file);
           echo "Success";
       }else{
           print_r($errors);
       }
      }
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么