我是 PHP 和 Objective-C 的新手,我已经搜索过这个问题的答案,但一切似乎都很复杂,我无法理解。我正在尝试使用 PHP 将图像文件上传到我的 ftp 服务器。
我在我的应用程序中使用此代码上传图片:
UIImage *myImage = [UIImage imageNamed:@"black_strip.png"];
NSData *imageData = UIImagePNGRepresentation(myImage);
// setting up the URL to post to
NSString *urlString = @"http://www.myurl.net/GagVidApp/uploadProfImage.php";
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"returnString: %@", returnString);
这是我的php代码:
<?php
$file = basename($_FILES['userfile']['name']);
$remote_file = basename($_FILES['userfile']['name']);
//$remote_file = 'readme.txt';
// set up basic connection
$ftp_server = "www.myurl.net";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypassword";
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
我在我的应用程序中得到的返回字符串是:
returnString: Connected to www.myurl.net, for user myuser<br />
<b>Warning</b>: ftp_put(ipodfile.png) [<a href='function.ftp-put'>function.ftp-put</a>]: **failed to open stream: No such file or directory in** <b>/home/load2unet/domains/myurl.net/public_html/GagVidApp/uploadProfImage.php</b> on line <b>24</b><br />
There was a problem while uploading ipodfile.png
我一直在努力寻找答案,但没有任何运气。任何帮助将不胜感激!谢谢