php - 如果照片上传成功
<p><p>我希望能够将照片上传到我的网站,并在完成后将其定位到不同的 ViewController 。如果它执行 xyz 有错误:</p>
<p>喜欢:</p>
<pre><code>if (!error) {
;
}
else
{
NSError *error = ;
}
</code></pre>
<hr/>
<h2>我如何上传照片:</h2>
<p>所以 objective-c 代码:</p>
<pre><code>NSData *imageData = UIImagePNGRepresentation();
NSMutableURLRequest *request = [
initWithURL:[NSURL
URLWithString:@"http://******.co.uk/****/imageupload.php"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:20.0];
;
[request setValue:@"image/jpg"
forHTTPHeaderField:@"Content-type"];
[request setValue:[NSString stringWithFormat:@"%lu",
(unsigned long)]
forHTTPHeaderField:@"Content-length"];
];
NSURLConnection *theConnection = [ initWithRequest:request delegate:self];
if( theConnection )
{
[ retain];
}
else
{
NSLog(@"theConnection is NULL");
}
;
</code></pre>
<p>和<code>imageupload.php</code>:</p>
<pre><code><?php
$handle = fopen("image.jpg", "wb"); // write binary
fwrite($handle, $HTTP_RAW_POST_DATA);
fclose($handle);
print "Received image file.";
?>
</code></pre>
<hr/>
<p>我也注意到iphone的屏幕顶部没有这样的加载gif,上传图片时怎么来的?:</p>
<p> <img src="/image/Dds2p.png" alt="enter image description here"/> </p>
<hr/>
<h1>编辑:</h1>
<pre><code>NSURLSessionConfiguration *configuration = ;
AFURLSessionManager *manager = [ initWithSessionConfiguration:configuration];
NSURL *URL = ;
NSURLRequest *request = ;
//where does the http code come in?
NSURL *filePath = ;
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Success: %@ %@", response, responseObject);
}
}];
;
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><h3>PHP</h3>
<p>始终添加检查以确保您确实得到了一些东西,</p>
<p>您可以使用<a href="http://www.php.net/manual/en/function.is-uploaded-file.php" rel="noreferrer noopener nofollow">is_uploaded_file</a>试试这样:</p>
<pre><code><?php
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$folder = "uploads/";
$file = basename( $_FILES['image']['name']);
$full_path = $folder.$file;
if(move_uploaded_file($_FILES['image']['tmp_name'], $full_path)) {
echo '{"success":true, "msg": "succesful upload, we have an image!"}';
} else {
echo '{"success":false, "msg": "upload received! but process failed"}';
}
}else{
echo '{"success":false, "msg": "upload failure ! Nothing was uploaded"}';
}
?>
</code></pre>
<h3> Objective-C </h3>
<pre><code>NSData *imageData = UIImagePNGRepresentation();
NSMutableURLRequest *request = [
initWithURL:[NSURL
URLWithString:@"http://******.co.uk/****/imageupload.php"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:20.0];
;
;
[request setValue:[NSString stringWithFormat:@"%lu",
(unsigned long)]
forHTTPHeaderField:@"Content-length"];
];
//Send the Request
NSData* returnData = [NSURLConnection sendSynchronousRequest: request
returningResponse: nil error: nil];
//serialize to JSON
NSDictionary *result = ;
//parsing JSON
bool success = boolValue];
if(success){
NSLog(@"Success=%@",result[@"msg"]);
}else{
NSLog(@"Fail=%@"result[@"msg"]);
}
</code></pre></p>
<p style="font-size: 20px;">关于php - 如果照片上传成功,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/23256387/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/23256387/
</a>
</p>
页:
[1]