doulai8128 2016-10-20 06:22
浏览 65
已采纳

发布网络服务以上传pdf文件

I have made a script in php to upload pdf file on server from iOS application but I don't understand why it says undefined index pdfFile please let me know if you catch my mistake

Here is my iOS code

 Alamofire.upload(
        multipartFormData: { multipartFormData in

          //  multipartFormData.append(pdfUrl!, withName: "pdfFile")

            let pdfData = NSData(contentsOf: pdfUrl!)
            print((pdfData as? Data)!)
            multipartFormData.append((pdfData as? Data)!, withName: "pdfFile", mimeType: "application/pdf")


        },
        to: "http://www.webservice.pixsterstudio.com/uploadpdf.php",
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, , ):
                upload.responseJSON { response in
                    debugPrint(response)
                    print(response.result)
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )

Here is my PHP web service script:- uploadpdf.php

  <?php

if ($_FILES['pdfFile']['type'] == "application/pdf") {
    $source_file = $_FILES['pdfFile']['tmp_name'];
    $dest_file = "webservice.pixsterstudio.com/upload/".$_FILES['pdfFile']['name'];

    if (file_exists($dest_file)) {
        print "The file name already exists!!";
    }
    else {
        move_uploaded_file( $source_file, $dest_file )
        or die ("Error!!");
        if($_FILES['pdfFile']['error'] == 0) {
            $Return['status'] = 'true';
            $Return['message'] = "Pdf file uploaded successfully!";
            //print "Pdf file uploaded successfully!";
            //print "<b><u>Details : </u></b><br/>";
            //print "File Name : ".$_FILES['pdfFile']['name']."<br.>"."<br/>";
        //  print "File Size : ".$_FILES['pdfFile']['size']." bytes"."<br/>";
        //  print "File location : upload/".$_FILES['pdfFile']['name']."<br/>";



        }
    }
}
else {
    if ( $_FILES['pdfFile']['type'] != "application/pdf") {
            $Return['status'] = 'false';
            $Return['message'] = "Pdf file not uploaded !";
        //print "Error occured while uploading file : ".$_FILES['pdfFile']['name']."<br/>";
        //print "Invalid  file extension, should be pdf !!"."<br/>";
        //print "Error Code : ".$_FILES['pdfFile']['error']."<br/>";
    }
}
  header('Content-type: application/json');
  echo  json_encode($Return);

 ?>
  • 写回答

2条回答 默认 最新

  • doulingzou1712 2016-10-20 07:02
    关注

    Whilst I wait for the coffee to kick in I quickly wrote this - it follows how I would usually handle file uploads - hope it might be useful.

    $fieldname = 'pdfFile';
    $targetdir = 'webservice.pixsterstudio.com/upload/';
    $desiredtype = 'application/pdf';
    $return=array(
        'status'    =>    'false',
        'message'    =>    'Pdf file not uploaded!'
    );
    
    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_FILES[ $fieldname ] ) ){
        try{
    
            $obj = (object)$_FILES[ $fieldname ];
            $name = $obj->name;
            $size = $obj->size;
            $tmp  = $obj->tmp_name;
            $type = $obj->type;
            $error= $obj->error;
    
    
    
    
            if( is_uploaded_file( $tmp ) && $error == UPLOAD_ERR_OK && $type == $desiredtype ){
    
                $destination = $targetdir . $name;
    
                if( file_exists( $destination ) ){
    
                    $return['status']='false';
                    $return['message']='File already exists!';
                    $return['line']=__LINE__;
    
                    clearstatcache();
    
                } else {
    
                    $res = move_uploaded_file( $tmp, $destination );
                    $return['status']=$res ? 'true' : false;
                    $return['message']=$res ? 'Pdf file uploaded successfully!' : 'Moving the file failed!';
    
                }
    
            } else {
                $return['status']='false';
                $return['message']='File upload error!';
                $return['line']=__LINE__;
            }
        }catch( Exception $e ){
                $return['status']='false';
                $return['message']=$e->getMessage();
                $return['line']=__LINE__;
        }
    
        header('Content-type: application/json');
        exit( json_encode( $return ) );
    
    } else {
        exit( header( 'HTTP/1.1 405 Method Not Allowed',true, 405 ) );
    }
    

    Using the following form I now receive a json message saying the upload failed...

        <form method='post' action='http://www.webservice.pixsterstudio.com/uploadpdf.php' enctype="multipart/form-data">
            <h1>Upload PDF to WebService</h1>
            <input type='file' name='pdfFile' />
            <input type="submit" value="Send">
        </form> 
    
    {"status":"false","message":"Pdf file not uploaded!"}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘