gpt4 book ai didi

php - 将 SQLite 文件发送到服务器 iOS

转载 作者:行者123 更新时间:2023-11-28 22:22:11 25 4
gpt4 key购买 nike

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

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

- (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>";

?>

我的输出是

 <pre>Possible file upload attack!
Here is some more debugging info:Array
(
[file] => Array
(
[name] => onehealth.sqlite
[type] => application/octet-stream
[tmp_name] => /tmp/phpeg93fL
[error] => 0
[size] => 262144
)

)

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

最佳答案

问题出在 Objective C 代码或 PHP 代码中。

简单来说,您正在创建一个表单,其中包含用于上传文件的“文件”字段。所以在代码 php 中你必须使用键 'file' 而不是 'userfile'。

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['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>";

?>

或者不想改php代码,可以改ObjC代码,改下一行:

[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]];

我还有一个建议:您可以使用图书馆 AFNetworking用于管理对服务器的请求。你已经解决了很多事情。比如上传文件。

关于php - 将 SQLite 文件发送到服务器 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20037184/

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