0

您好,我想向您展示我在做什么,因为我的 PHP 或我的 iOS 没有将文件发送到 FTP 服务器:S

iOS端:

NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
NSString *nombreArchivo = @"wtthdb.db";

NSData *archivo = [NSData dataWithContentsOfFile:[[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:nombreArchivo]]];
NSLog(@"Tamaño del archivo %i",[archivo length]);

NSMutableString *post = [[NSMutableString alloc] initWithFormat:@"file=%@&filename=%@",archivo,nombreArchivo];
[post appendFormat:@"%@",archivo];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString:@"http://zsoft.es/subidaios.php"];
NSMutableURLRequest *peticion = [NSMutableURLRequest requestWithURL:url];

NSURLConnection *con;
@try
{
    [peticion setHTTPMethod:@"POST"];
    [peticion setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [peticion setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [peticion setHTTPBody:postData];
    con = [NSURLConnection connectionWithRequest:peticion delegate:self];
    [con start];
}
@catch (NSException *e)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error del servidor"
                                                    message:[NSString stringWithFormat:@"Error: %@",e]
                                                   delegate:self
                                          cancelButtonTitle:@"Aceptar"
                                          otherButtonTitles:nil];
    [alert show];
}
NSLog(@"Empezado");

这是我的 PHP 方面:

  <?php
if (isset($_POST['name']) && isset($_POST['filename']))
{
    $name  = $_POST['name'];
    $filename  = $_POST['filename'];
    $ftp = ftp_connect("ftp.marinesignal.com");
    ftp_login($ftp, "user", "pass");
    ftp_pasv($ftp, true);
    ftp_put($ftp,$filename,$name,FTP_BINARY);
    ftp_close($ftp);
}
 ?>

而且我不知道如何通过 XCode 调试我的 PHP 代码......

谢谢!!

4

1 回答 1

1

我建议您阅读这篇关于如何将 UIImage 上传到网络的好文章,以便您了解如何准备文件并将其发布到网络

http://zcentric.com/2008/08/29/post-a-uiimage-to-the-web/

然后在你的 PHP 服务器端,你应该考虑文件是通过 $_FILES 全局变量传递的,所以你应该在这个问题上处理它们

<?php
$temporalFolder = "temporal/";

$filePath = $temporalFolder . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $temporalFolder)) {
    echo "The file has been uploaded";
} else{
    echo "There was an error uploading the file";
}

$name  = $_POST['name'];
$ftp = ftp_connect("ftp.marinesignal.com");
ftp_login($ftp, "user", "pass");
ftp_pasv($ftp, true);
ftp_put($ftp,$filePath,$name,FTP_BINARY);
ftp_close($ftp);

?>

希望这可以让您对您的问题有所了解

于 2013-01-24T15:57:25.377 回答