php - 上传图片到服务器不起作用

标签 php ios xcode image upload

我正在尝试通过 xcode 中的 php 请求将图片上传到我的服务器。我尝试了很多可能性,但到目前为止似乎没有任何效果。我对 php 不是很熟悉,所以可能是错误所在。现在它只是给我错误“文件类型不正确”,但我肯定使用的是 jpg 图像。 (试过不止一次)

这是我在 xcode 中的内容:

NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
NSString *urlString = @"http://myserver/upload.php";

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"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]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

和我的 php 文件:

<?php
$uploaddir = '/images/';      //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
echo "Temp file uploaded. \r\n";
} else {
echo "Temp file not uploaded. \r\n";
}

if ((!($_FILES['userfile']['type'] == "image/jpeg")) && (!($_FILES['userfile']['type']    == "image/png")))           
{
exit("Incorrect file type.  " . $_FILES['userfile']['type'] . " is the file type you     uploaded."); 
}


if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    $postsize = ini_get('post_max_size');   
    $canupload = ini_get('file_uploads');    
    $tempdir = ini_get('upload_tmp_dir');   
    $maxsize = ini_get('upload_max_filesize');
    echo "http://www.mycompanyname.com/myappname/{$file}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
}

 ?>

最佳答案

检查文件类型并检查目录路径 $uploaddir = '/images/';这是对的吗?这段代码对我来说工作正常。

关于php - 上传图片到服务器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9349318/

相关文章:

objective-c - 如何使用 ViewController 修复 iOS 7 View 中的 44px 空白空间?

php - 如何将详细信息写入文件?

ios - 自定义导航栏后退按钮点击区域

php - 将复选框插入数据库

ios - 表情符号破坏了我在 UItextView Swift 4 中的代码

iOS 如何以编程方式简单地返回到先前呈现/推送的 View Controller ?

ios - 如何在 Xcode 控制台中漂亮地打印 NSError 对象?

xcode - Xcode 4 中未显示用于代码签名的自签名证书

php - 如何实现根据用户尝试保存的值而变化的权限系统?

php - 计算mysql中的列乘以排序数