图像上传发送数据但没有在接收端创建文件

I'm stuck. and lost at what to do to solve it. I'm sending a image with the methods below in Swift from a iPhone. And I can see the data is being received by the server with Wireshark. I can also see in the php-server's terminal that I'm receiving data.

it's data that I don't know how to handle.

I'm currently using the php script below to handle everything with the upload there is a lot of test variables that I'm using to just see if I could figure out what I was receiving if anything.

    public func post(data: Data, url: URL)
    {
        var request = URLRequest(url: url)
        //request.addValue("filenameTest.jpg", forHTTPHeaderField: "filename")
        request.httpMethod = "POST"
        let body = createBodyWithParameters(filePathKey: "filenameTest.jpg",data: data,boundary: generateBoundaryString())
        request.setValue("Keep-Alive", forHTTPHeaderField: "Connection")
        let configuration = URLSessionConfiguration.default
        let session = URLSession(configuration: configuration,delegate: self, delegateQueue: OperationQueue.main)
        let task = session.uploadTask(with: request as URLRequest, from: body)
        task.resume()

    }
func createBodyWithParameters(filePathKey: String?, data: Data!, boundary: String) -> Data {
        var body = Data();

        let mimetype = "image/jpg"

        body.append(Data( "--\(boundary)
".utf8))
        //body.append(Data( "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(parameters!["filename"]!)\"
".utf8))
        body.append(Data( "Content-Disposition: form-data; filename=\"\(filePathKey!)\"
".utf8))
        body.append(Data( "Content-Type: \(mimetype)

".utf8))


        body.append(data)
        body.append(Data( "
".utf8))

        body.append(Data( "--\(boundary)--
".utf8))

        return body
    }
    func generateBoundaryString() -> String {
        return "----Boundary---"
    }

Wireshark

Requested: ??~?3?<??6??Ns?i|2?~`kxB?;??N???$_?|?9?:  

Requested: ???c?S?Y?Gn8>?5?k???y?H??3?N:???????M?_?r??q????~??O?6?Pc?]%"d??x??u?:  

Requested: ??mRKmS????:  

Requested: ????~k]?~}t?E??EO???????'???-????????$?W??|_?Wsl???f???n?:?Q????_?_???_?_2c_8?S?c?:  

Requested: ?????#?:  

Requested:  
[Thu Mar  9 13:55:51 2017] 192.168.250.101:55327 [200]: /




<?php




$target_dir = "wp-content/uploads";

if(!file_exists($target_dir))
{
mkdir($target_dir, 0777, true);
}

$target_dir = $target_dir . "/" . basename($_SERVER['HTTP_FILENAME']);




if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir)) {
echo json_encode([
"Message" => "The file ". basename(header('filename')). " has been uploaded.",
"Status" => "OK"
]);

} else {

echo json_encode([
"Message" => "Sorry, there was an error uploading your file.",
"Status" => "Error"

]);
}
?>

and I'm using this kind of code to figure out what variables contain what.

 $testvar = $_SERVER['HTTP_FILENAME'];
    file_put_contents("php://stdout", "
Requested : $testvar /r/n");
    $testvar = count($_REQUEST);
    $testvar = $_REQUEST[3];
    file_put_contents("php://stdout", "
Requested : $testvar /r/n");
    foreach($_FILES['file'] as $value){
    $testvar = ": ". $value;
    file_put_contents("php://stdout", "
Requested: $testvar 
");
    }

I don't know how to fix this. I know the data is there just can't seem to get a hold of it. I've tried several ways to send it with the swift part. this one plus 2 others had the same issue when I get to the php side the filename was nowhere to be found and I couldn't even find a temp name of the file.

note that the image data is never saved to a file on the iPhone and I have no intention of ever letting that happen. I simply need to take a image with the camera and send it, I've accomplished that now but when I try to save a image file on the PHP side everything becomes seemingly impossible to do.

there is no content in $_FILES as far as I can see the content is in the $_REQUEST as well as the file but when I try to access that one I just get wired signs and question marks.

查看全部
doulingzou1712
doulingzou1712
2017/03/09 13:15
  • php
  • ios
  • swift
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复