菜鸟教程小白 发表于 2022-12-12 09:32:25

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:@&#34;http://******.co.uk/****/imageupload.php&#34;]
                                    cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                    timeoutInterval:20.0];
    ;
    [request setValue:@&#34;image/jpg&#34;
   forHTTPHeaderField:@&#34;Content-type&#34;];
    [request setValue:[NSString stringWithFormat:@&#34;%lu&#34;,
                     (unsigned long)]
   forHTTPHeaderField:@&#34;Content-length&#34;];
    ];
   
    NSURLConnection *theConnection = [ initWithRequest:request delegate:self];
   
    if( theConnection )
    {
      [ retain];
    }
    else
    {
      NSLog(@&#34;theConnection is NULL&#34;);
    }
   
    ;
</code></pre>
<p>和<code>imageupload.php</code>:</p>
<pre><code>&lt;?php
$handle = fopen(&#34;image.jpg&#34;, &#34;wb&#34;); // write binary

fwrite($handle, $HTTP_RAW_POST_DATA);

fclose($handle);

print &#34;Received image file.&#34;;
?&gt;
</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(@&#34;Error: %@&#34;, error);
    } else {
      NSLog(@&#34;Success: %@ %@&#34;, 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>&lt;?php
if(is_uploaded_file($_FILES[&#39;image&#39;][&#39;tmp_name&#39;])){
    $folder = &#34;uploads/&#34;;
    $file = basename( $_FILES[&#39;image&#39;][&#39;name&#39;]);
    $full_path = $folder.$file;
    if(move_uploaded_file($_FILES[&#39;image&#39;][&#39;tmp_name&#39;], $full_path)) {
      echo &#39;{&#34;success&#34;:true, &#34;msg&#34;: &#34;succesful upload, we have an image!&#34;}&#39;;
    } else {
      echo &#39;{&#34;success&#34;:false, &#34;msg&#34;: &#34;upload received! but process failed&#34;}&#39;;
    }
}else{
    echo &#39;{&#34;success&#34;:false, &#34;msg&#34;: &#34;upload failure ! Nothing was uploaded&#34;}&#39;;
}
?&gt;
</code></pre>

<h3> Objective-C </h3>

<pre><code>NSData *imageData = UIImagePNGRepresentation();
NSMutableURLRequest *request = [
                              initWithURL:[NSURL
                  URLWithString:@&#34;http://******.co.uk/****/imageupload.php&#34;]
                              cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                              timeoutInterval:20.0];
;
;
[request setValue:[NSString stringWithFormat:@&#34;%lu&#34;,
                   (unsigned long)]
forHTTPHeaderField:@&#34;Content-length&#34;];
];
//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(@&#34;Success=%@&#34;,result[@&#34;msg&#34;]);
}else{
    NSLog(@&#34;Fail=%@&#34;result[@&#34;msg&#34;]);
}
</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]
查看完整版本: php - 如果照片上传成功