Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将SQLite文件发送到服务器iOS_Php_Ios_Objective C_Sqlite - Fatal编程技术网

Php 将SQLite文件发送到服务器iOS

Php 将SQLite文件发送到服务器iOS,php,ios,objective-c,sqlite,Php,Ios,Objective C,Sqlite,我正在尝试从我的应用程序中按下一个按钮,将设备上的sqlite文件的副本发送到我的Web服务,以便在那里进行进一步的操作。我一直没有收到任何结果,因为它没有发送文件 这是我当前发送文件的代码 - (void)sendFile { //NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // NSString *documentsDir = [doc

我正在尝试从我的应用程序中按下一个按钮,将设备上的sqlite文件的副本发送到我的Web服务,以便在那里进行进一步的操作。我一直没有收到任何结果,因为它没有发送文件

这是我当前发送文件的代码

- (void)sendFile
{
//NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDir = [docPaths objectAtIndex:0];
// NSString *filePath = [documentsDir stringByAppendingPathComponent:@"database.sqlite"];

NSString *fileName = @"database.sqlite";
NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *filePath = [directoryPath stringByAppendingPathComponent:fileName];
NSString *serverURL = @"http://webserver/upload.php";
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
if (!fileData) {
    NSLog(@"Error: file error");
    return;
}

if (self.urlConnection) {
    [self.urlConnection cancel];
    self.urlConnection = nil;
}

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                initWithURL:[NSURL URLWithString:serverURL]];
[request setTimeoutInterval:30.0];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"780808070779786865757";

/* Header */
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

/* Body */
NSMutableData *postData = [NSMutableData data];
[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"file\"; filename=\"database.sqlite\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:fileData];
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];

self.urlConnection = [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 {
if (self.receivedData) {
    self.receivedData = nil;
}
self.receivedData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"finish requesting: %@", [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding]);
self.urlConnection = nil;
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"requesting error: %@", [error localizedDescription]);
self.urlConnection = nil;
}
我当前的php脚本是

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>

我的结果是

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>
可能的文件上传攻击!
下面是一些调试信息:数组
(
[文件]=>数组
(
[名称]=>onehealth.sqlite
[类型]=>应用程序/八位字节流
[tmp_name]=>/tmp/phpeg93fL
[错误]=>0
[大小]=>262144
)
)

更多信息,我在模拟器上运行,这会有问题吗?这会在ipad上留下sqlite文件的副本吗?还是我必须复制一份然后发送副本?

问题出在目标C代码或PHP代码中

简单地说,您正在创建一个带有字段“file”的表单,以便上传文件。因此,在php代码中,必须使用键“file”,而不是“userfile

PHP代码的解决方案是:

[postData appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"userfile\"; filename=\"database.sqlite\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
为此:

我还有一个建议:
您可以使用该库来管理对服务器的请求。你在里面解决了很多事情。就像上传文件一样。

我可以安抚您,多部分消息似乎还可以(这通常很容易出错);)现在,检查所有错误、响应状态代码、响应MIME类型并在代码中插入断言,以便收集更多信息。嘿!我会用错误信息编辑我的问题,这样我的英语会更整洁。
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"file\"; filename=\"database.sqlite\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"userfile\"; filename=\"database.sqlite\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];