我正在为我的 iPhone 应用程序做服务器端工作,由于某种原因,imageData 不会上传。如果您能看一下我的 PHP 代码并告诉我是否有问题,我将不胜感激。
addImage.php 在调用 addrecipie 方法后调用。
我上传图片的 Obj-c 代码是:
NSString *name = [@"Name: " stringByAppendingFormat:@"%d", arc4random()%20091011];
NSString *username = [@"User: " stringByAppendingFormat:@"%d", arc4random()%20091011];
NSString *description = [@"Description: " stringByAppendingFormat:@"%d", arc4random()%20091011];
NSString *shortDescription = [@"ShortDescription: " stringByAppendingFormat:@"%d", arc4random()%20091011];
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSArray *array = [NSArray arrayWithObjects:@"llama", @"30", @"beef", @"beef", @"beefy", nil];
NSString *ingredients = [writer stringWithObject:array];
NSString *method = ingredients;
NSString *category = @"Health";
NSString *length = @"20";
[writer release];
NSArray *arr = [NSArray arrayWithObjects:name,description,shortDescription,ingredients,method,UIImageJPEGRepresentation([UIImage imageNamed:@"cupboard.jpg"], 1.0),username,length,category, nil];
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:arr forKeys:recipieKeys];
[[AccountManager sharedManager] addRecipie:dict];
[dict release];
- (NSString *)addRecipie:(NSDictionary *)recipie {
NSData *data = [recipie objectForKey:@"image"];
//form data away....
NSString *url = [NSString stringWithFormat:@"http://...com/(..).php?name=%@&description=%@&shortDescription=%@&ingredients=%@&method=%@&username=%@&length=%@&category=%@", [recipie objectForKey:@"name"], [recipie objectForKey:@"description"], [recipie objectForKey:@"shortdescription"], [recipie objectForKey:@"ingredients"], [recipie objectForKey:@"method"], [recipie objectForKey:@"username"], [recipie objectForKey:@"length"], [recipie objectForKey:@"category"]];
NSString *escaped = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[escaped stringByReplacingOccurrencesOfString:@"$" withString:@"%24"];
[escaped stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
[escaped stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
[escaped stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
[escaped stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
[escaped stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
[escaped stringByReplacingOccurrencesOfString:@";" withString:@"%3B"];
[escaped stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
[escaped stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
[escaped stringByReplacingOccurrencesOfString:@"@" withString:@"%40"];
[escaped stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
[escaped stringByReplacingOccurrencesOfString:@"\t" withString:@"%09"];
[escaped stringByReplacingOccurrencesOfString:@"#" withString:@"%23"];
[escaped stringByReplacingOccurrencesOfString:@"<" withString:@"%3C"];
[escaped stringByReplacingOccurrencesOfString:@">" withString:@"%3E"];
[escaped stringByReplacingOccurrencesOfString:@"\"" withString:@"%22"];
[escaped stringByReplacingOccurrencesOfString:@"\n" withString:@"%0A"];
ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:escaped]];
[req startAsynchronous];
if (![req error]) {
NSString *ids = [req responseString];
ASIFormDataRequest *reqs = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:[@"http://maxkdevelopment.co.uk/recipes/addimage.php?id=" stringByAppendingFormat:@"%@", [ids stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]]];
[reqs setData:data forKey:@"image"];
[reqs startAsynchronous];
if (![reqs error]) {
return [reqs responseString];
}
else {
return [[reqs error] description];
}
}
else {
NSLog(@"%@", [[req error] description]);
}
return @"";
}
(addImage.php)
<?php
$id = $_GET['id'];
mysql_connect(...);
mysql_select_db("...") or die(mysql_error());
$query = "SELECT * FROM recipies WHERE id=$id";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
$sql = sprintf("UPDATE recipies SET Image = '%s' WHERE id=$id", mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"])));
$results = mysql_query($sql) or die(mysql_error());
$error = mysql_error();
echo "error: $error";
}
?>
(GetImage.php)
<?php
$id = $_GET['id'];
mysql_connect(...);
mysql_select_db(...");
$query = "SELECT * FROM recipies WHERE id=$id";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
echo $row['Image'];
}
}
?>
(添加Recipe.php)
<?php
function uk_date() {
$sign = "-";
$h = "0";
$dst = "true";
if ($dst) {
$daylight_saving = date('I');
if ($daylight_saving){
if ($sign == "-"){ $h=$h-1; }
else { $h=$h+1; }
}
}
$hm = $h * 60;
$ms = $hm * 60;
if ($sign == "-"){ $timestamp = time()-($ms); }
else { $timestamp = time()+($ms); }
return $gmdate = gmdate("d/m/Y g:i:s A", $timestamp);
}
$name = $_GET['name'];
$description = $_GET['description'];
$shortDescription = $_GET['shortDescription'];
$ingredients = $_GET['ingredients'];
$category = $_GET['category'];
$method = $_GET['method'];
//$image = $_GET['image'];
$username = $_GET['username'];
$length = $_GET['length'];
$dateAdded = uk_date();
$conn = mysql_connect('...');
mysql_select_db('...');
$query = sprintf("INSERT INTO recipies (Name, Description, shortDescription, Ingredients, Method, Length, dateAdded, Username, Category) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
mysql_real_escape_string($name),
mysql_real_escape_string($description),
mysql_real_escape_string($shortDescription),
mysql_real_escape_string($ingredients),
mysql_real_escape_string($method),
mysql_real_escape_string($length),
mysql_real_escape_string($dateAdded),
mysql_real_escape_string($username),
mysql_real_escape_string($category));
$result = mysql_query($query) or die(mysql_error());
$query2 = "SELECT * FROM `recipies` WHERE `Name` = $name AND `shortDescription` = $shortDescription";
$result2 = mysql_query($query2) or die(mysql_error());
if (mysql_num_rows($result2) > 0) {
while($row = mysql_fetch_assoc($result2)) {
die($row['id']);
}
}
?>