iphone SDK:从iphone上传图片到php服务器发送空文件?(示例代码链接里面) [英] iphone SDK : Upload image from iphone to a php Server send empty file ?(sample code link inside)

查看:153
本文介绍了iphone SDK:从iphone上传图片到php服务器发送空文件?(示例代码链接里面)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过PHP向服务器发送照片和GPS位置



这里是PHP部分:从这里复制
保存上传的文件



上面的例子创建了一个服务器上PHP临时文件夹中上载文件的临时副本。



当脚本结束时,临时复制的文件将消失。要存储上传的文件,我们需要将其复制到其他位置:

 <?php 
if(( ($ _FILES [file] [type] ==image / gif)
||($ _FILES [file] [type] ==image / jpeg)
||($ _FILES [file] [type] ==image / pjpeg))
&&($ _FILES [file] [size]< 20000 ))
{
if($ _FILES [file] [error]> 0)
{
echo返回代码:。 $ _FILES [file] [error]。 < br />;
}
其他
{
echo上传:。 $ _FILES [file] [name]。 < br />;
echo类型:。 $ _FILES [file] [type]。 < br />;
echo大小:。 ($ _FILES [file] [size] / 1024)。 Kb< br />;
echo临时文件:。 $ _FILES [file] [tmp_name]。 < br />;

if(file_exists(upload /。$ _FILES [file] [name]))
{
echo $ _FILES [file] [名称] 。 已经存在。 ;
}
else
{
move_uploaded_file($ _ FILES [file] [tmp_name],
upload /。$ _FILES [file] [名称]);
echo存储于:。 上传/。 $ _FILES [ 文件] [ 名称];
}
}
}
其他
{
echo无效文件;
}
?>

上面的脚本检查文件是否已经存在,如果不存在,则将文件复制到指定文件夹。



注意:此示例将文件保存到名为upload的新文件夹



现在返回到iOS部分,我只能将GPS位置发送到服务器,这很容易,因为只是一个字符串。



你可以在这里下载示例代码



但是图片不是,我做了一些搜索,发现了这个讨论



我直接将代码放入我的发送GPS&图像按钮



最好的事情是它可以反馈我发送文件成功或失败



这是我将照片发送到服务器后获得的最多消息



1.文件无效



2.返回代码:
Up载入:
类型:
大小:0 Kb
临时文件:
存储:上传/



我按照一些指南将图像保存到NSData

  NSData * photoData = UIImageJPEGRepresentation(self.theimageView.image,1.0); 

我还检查里面是否有数据



所以只需将此代码添加到Button操作中

  if(photoData == nil){
NSLog( @照片什么都没有!);
}
else {
NSLog(@Photo inside !!!!);

工作正常.......



但如何将图像上传到服务器???



也许我需要提供文件名然后上传它???



但是如何...



我从不使用相机功能和



上传数据希望有人能解决这个问题



非常感谢!!!使用HTTP多部分POST或ASIHTTPRequest






  @WebberLai 

将图像保存到主目录:
NSString * jpgPath = [NSHomeDirectory()stringByAppendingPathComponent: @ 文档/ image.jpg的];
UIImage * img = pickerImage;


//以最小压缩(最佳质量)将UIImage写入JPEG
[UIImageJPEGRepresentation(img,0.5)writeToFile:jpgPath atomically:YES];

NSString * photoPath = [NSHomeDirectory()stringByAppendingPathComponent:@Documents / image.jpg];

request = [ASIFormDataRequest requestWithURL:webServiceUrl];
[请求setPostValue:requestString forKey:@data];
[请求setFile:photoPath forKey:@apiupload];
[请求startSynchronous];


I try to send a photo and GPS location to server via PHP

here is the PHP part:Copy from here Saving the Uploaded File

The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server.

The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a different location:

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

The script above checks if the file already exists, if it does not, it copies the file to the specified folder.

Note: This example saves the file to a new folder called "upload"

now back to iOS part ,I only can send GPS location to server ,it's easy because just a string.

and you can download sample code here

But an image is not,I do some search and found this discussion

I directly put the code in my send GPS&image button

the best thing is it can feedback I send the file success or failed

this is the most message I got after I send photo to server

1.Invalid file

2.Return Code:
Upload:
Type:
Size: 0 Kb
Temp file:
Stored in: upload/

I follow some guide to save image to NSData

NSData *photoData= UIImageJPEGRepresentation(self.theimageView.image, 1.0);

and I also check there is data inside or not

so simply add this code into Button action

if (photoData == nil) {
    NSLog(@"The photo is nothing !!!");
}
else {
    NSLog(@"Photo inside !!!!");

It works fine.......

But How To Upload An Image To Server ???

maybe I need to give a file name then upload it ???

but how to...

I never use camera function and upload data before

Hope someone can figure out this problem

Many Great Thanks !!!

解决方案

use HTTP multipart POST or ASIHTTPRequest


@WebberLai

Save the image to home directory:
 NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"];
    UIImage *img = pickerImage;


// Write a UIImage to JPEG with minimum compression (best quality) 
    [UIImageJPEGRepresentation(img, 0.5) writeToFile:jpgPath atomically:YES];

    NSString *photoPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"];

    request = [ASIFormDataRequest requestWithURL:webServiceUrl];
    [request setPostValue:requestString forKey:@"data"];
    [request setFile:photoPath forKey:@"apiupload"];
    [request startSynchronous];

这篇关于iphone SDK:从iphone上传图片到php服务器发送空文件?(示例代码链接里面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆