gpt4 book ai didi

php - 在 iPhone 中使用多部分表单数据发布图像和其他数据

转载 作者:行者123 更新时间:2023-12-03 21:23:41 24 4
gpt4 key购买 nike

我正在使用 Objective C 中的 multipart/form-data 向服务器发送一些数据和图像。请给我一些 PHP 代码,我如何将图像保存在服务器上,我能够获取服务器上与图像一起传递的其他变量。请查看我的 obj C 代码和 php,并告诉我哪里错了。

我们将非常感谢您的帮助。

这里我发出 POST 请求。

///////////////////////

    NSString            *stringBoundary, *contentType, *baseURLString, *urlString;
NSData *imageData;
NSURL *url;
NSMutableURLRequest *urlRequest;
NSMutableData *postBody;

// Create POST request from message, imageData, username and password
baseURLString = @"http://localhost:8888/Test.php";
urlString = [NSString stringWithFormat:@"%@", baseURLString];
url = [NSURL URLWithString:urlString];
urlRequest = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
[urlRequest setHTTPMethod:@"POST"];

// Set the params
NSString *path = [[NSBundle mainBundle] pathForResource:@"LibraryIcon" ofType:@"png"];
imageData = [[NSData alloc] initWithContentsOfFile:path];

// Setup POST body
stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
[urlRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];

// Setting up the POST request's multipart/form-data body
postBody = [NSMutableData data];

[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"lighttable"] dataUsingEncoding:NSUTF8StringEncoding]]; // So Light Table show up as source in Twitter post

[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:book.title] dataUsingEncoding:NSUTF8StringEncoding]]; // title

[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"isbn\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:book.isbn] dataUsingEncoding:NSUTF8StringEncoding]]; // isbn

[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"price\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:txtPrice.text] dataUsingEncoding:NSUTF8StringEncoding]]; // Price

[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"condition\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:txtCondition.text] dataUsingEncoding:NSUTF8StringEncoding]]; // Price


NSString *imageFileName = [NSString stringWithFormat:@"photo.jpeg"];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"upload\"; filename=\"%@\"\r\n",imageFileName] dataUsingEncoding:NSUTF8StringEncoding]];
//[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"upload\"\r\n\n\n"]dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[postBody appendData:imageData];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];


// [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

NSLog(@"postBody=%@", [[NSString alloc] initWithData:postBody encoding:NSASCIIStringEncoding]);
[urlRequest setHTTPBody:postBody];
NSLog(@"Image data=%@",[[NSString alloc] initWithData:imageData encoding:NSASCIIStringEncoding]);

// Spawn a new thread so the UI isn't blocked while we're uploading the image
[NSThread detachNewThreadSelector:@selector(uploadingDataWithURLRequest:) toTarget:self withObject:urlRequest];

我使用 uploadingDataWithURLRequest 方法将请求发布到服务器...

这是我的 php 代码

?php
$title = $_POST['title'];
$isbn = $_POST['isbn'];
$price = $_POST['price'];
$condition = $_POST['condition'];
$image=$_FILES['image']['name'];



if($image)
{
$filename = 'newimage.jpeg';
file_put_contents($filename, $image);
echo "image is there";
}
else
{
echo "image is nil";
}


?>

我无法在服务器上获取图像,请帮助我解决错误的地方。

最佳答案

您访问上传的文件的方式不正确。 $_FILE['image']['name'] 是客户端上传的文件的名称。您要做的就是将该名称(一个简单的字符串,如“test.jpg”)写入 $filename,以便您的“newimage.jpeg”文件包含一个字符串,而不是 JPEG 数据。

您想要的是以下内容:

move_uploaded_file($filename, $_FILES['image']['tmp_name']);

$_FILES 数据的“tmp_name”部分是文件临时存储在服务器上的绝对路径(例如“/var/tmp/sometemporaryuglyfilename”),然后您可以使用以下命令将其移动到其最终存放位置move_uploaded_file()。

关于php - 在 iPhone 中使用多部分表单数据发布图像和其他数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2552496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com