简体   繁体   English

从iOS通过POST PHP上传文件

[英]Uploading file through POST PHP from iOS

Hello I want to show you what I'm doing because my PHP or my iOS is not sending the File to the FTP server :S 您好,我想向您展示我在做什么,因为我的PHP或iOS没有将文件发送到FTP服务器:S

iOS side: 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");

And this is my PHP side: 这是我的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);
}
 ?>

And I don't know how to debug via XCode my PHP code... 而且我不知道如何通过XCode调试我的PHP代码...

Thanks!! 谢谢!!

I'd recommend you to go through this nice article on how to upload a UIImage to the web so you can get an idea on how to prepare and POST the file to the web 我建议您仔细阅读这篇有关如何将UIImage上载到Web的文章,以便您了解如何准备文件并将其发布到Web

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

Then on your PHP Server side you should consider that files are delivered through the $_FILES global variable so you should handle them on this matter 然后,在您的PHP Server端,您应该考虑通过$ _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);

?>

Hope this can give you some insight on your issue 希望这可以给您一些有关您的问题的见解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM