doulai8128
doulai8128
2016-10-20 06:22

发布网络服务以上传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 doulingzou1712 5年前

    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!"}
    
    点赞 评论 复制链接分享
  • dongyong1897 dongyong1897 5年前

    lets check your code your mistake file mime type. check file type in php take mimetype file upload.

    every upload file mime type something different

    if ($_FILES['pdfFile']['type'] == "pdf") {
    
    }
    

    replace your php file type check line see above. this code check every pdf mimetype file upload.

    below here my ios code here is my code i am not getting any type of error.

    func callPostWithMultipartService(urlString: String, param : [String: AnyObject]?, fileData : NSData,  completion: (result: String, data : AnyObject) -> Void)
    {
    
        upload(.POST, urlString, multipartFormData: { MultipartFormData in
    
            MultipartFormData.appendBodyPart(data: fileData, name: "filepdf", fileName: "file.pdf", mimeType: "application/pdf")
            for (key, value) in param! {
                MultipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
            }
            }
    
    点赞 评论 复制链接分享