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

时间:2016-10-20 06:22:33

标签: php ios pdf

我在php中创建了一个脚本,用于从iOS应用程序上传服务器上的pdf文件,但我不明白为什么它说未定义的索引pdfFile如果你抓到我的错误请告诉我

这是我的iOS代码

 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)
            }
        }
    )

这是我的PHP Web服务脚本: - 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 个答案:

答案 0 :(得分:0)

让我们检查您的错误文件mime类型的代码。 在php中检查文件类型mimetype文件上传。

  

每个上传文件都是mime类型不同的

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

}

替换你的php文件类型检查行见上文。 此代码检查每个pdf mimetype文件上传。

  

下面是我的ios代码   这是我的代码我没有得到任何类型的错误。

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)
        }
        }

答案 1 :(得分:0)

当我等待咖啡开始时,我很快写了这篇文章 - 它遵循我通常如何处理文件上传 - 希望它可能有用。

$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 ) );
}

使用以下表格,我现在收到一条json消息,说上传失败......

    <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!"}