gpt4 book ai didi

php - 从 iOS 通过 POST PHP 上传文件

转载 作者:行者123 更新时间:2023-11-29 04:10:20 24 4
gpt4 key购买 nike

您好,我想向您展示我在做什么,因为我的 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 代码...

谢谢!!

最佳答案

我建议您阅读这篇关于如何将 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);

?>

希望这能让您对问题有一些见解

关于php - 从 iOS 通过 POST PHP 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14504230/

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