iphone SDK:将图像从iphone上传到php服务器发送空文件?(内部示例代码链接)

发布于 2024-09-28 10:00:39 字数 2351 浏览 1 评论 0原文

我尝试通过 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 "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";
  }
?>

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

注意:这个例子将文件保存到一个名为“upload”的新文件夹

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

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

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

我直接将代码放在我的发送 GPS&image 中按钮

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

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

1.文件无效

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

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

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

,我还检查里面是否有数据或不是

简单地将这段代码添加到按钮操作中

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

它工作得很好......

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

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

但是如何...

我以前从未使用过相机功能并上传数据

希望有人能解决这个问题

非常感谢!!!

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 !!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

℡寂寞咖啡 2024-10-05 10:00:39

使用 HTTP 多部分 POST 或 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];

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];
软的没边 2024-10-05 10:00:39

添加这个很好

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

[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];

它收到了很多错误消息,我认为委托问题

首先编译器说“请求未声明”和“ASIFormDataRequest未声明”

和“requestString未声明”

我尝试给出请求和requestString声明

所以我使用 ASIFormDataRequest *request;NSString *requestString;

它清除了有关“equestString undeclared”的错误

我不知道如何声明 ASIFormDataRequest 所以它仍然是一个错误...

我把它放在 IBAction 中对吗???

我在这里查看一些示例

再次感谢~你真是个好人!

更多编辑:

好的,这是有关如何导入必要的 .h .m 和框架的更多步骤

在此处下载

并导入此标头:

ASIHTTPRequestConfig.h

ASIHTTPRequestDelegate.h

ASIProgressDelegate.h

ASICacheDelegate.h

ASIInputStream.h

ASIInputStream.m

ASIHTTPRequest.h

ASIHTTPRequest.m

ASIFormDataRequest.h

ASIFormDataRequest.m

ASINetworkQueue.h

ASINetworkQueue.m

ASIDownloadCache.h

ASIDownloadCache.m

iPhone 项目还必须包括:

ASIAuthenticationDialog.h

ASIAuthenticationDialog.m

Reachability.h (in the External/Reachability folder)

Reachability.m (in the External/Reachability folder)

导入框架:

CFNetwork

SystemConfiguration.framework

MobileCoreServices.framework

CoreGraphics.framework

libz.1.2.3。 dylib

It's fine to add this

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

[UIImageJPEGRepresentation(img, 0.5) writeToFile:jpgPath atomically:YES];

But When I try to put in my button action

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

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

It got many error message ,I think is delegate problem

first complier says "request undeclared" and "ASIFormDataRequest undeclared"

and "requestString undeclared"

I try to give request and requestString declared

So I use ASIFormDataRequest *request; and NSString *requestString;

It clear an error about "equestString undeclared"

I don't know how to declared ASIFormDataRequest so it still a error...

Did I right to put this in IBAction ???

I look some example here

Thanks again~you are a really nice guy !

More Edit :

OK, Here is more step about how to import necessary .h .m and framework

download here

and import this header :

ASIHTTPRequestConfig.h

ASIHTTPRequestDelegate.h

ASIProgressDelegate.h

ASICacheDelegate.h

ASIInputStream.h

ASIInputStream.m

ASIHTTPRequest.h

ASIHTTPRequest.m

ASIFormDataRequest.h

ASIFormDataRequest.m

ASINetworkQueue.h

ASINetworkQueue.m

ASIDownloadCache.h

ASIDownloadCache.m

iPhone projects must also include:

ASIAuthenticationDialog.h

ASIAuthenticationDialog.m

Reachability.h (in the External/Reachability folder)

Reachability.m (in the External/Reachability folder)

than import Framework:

CFNetwork

SystemConfiguration.framework

MobileCoreServices.framework

CoreGraphics.framework

libz.1.2.3.dylib

鱼窥荷 2024-10-05 10:00:39

好的,这是 iPhone3 和 iPhone3 之间的一个小错误。 iPhone4/touch4

@ dhil 的答案几乎是正确的,你只需要给出一个正确的密钥(取决于服务器)

[request setPostValue:requestString forKey:@"data"];
[request setFile:photoPath forKey:@"apiupload"];

好吧...现在如果你使用 iPhone3 ,并且你不给出 requestString alloc

它运行良好〜

但如果你在 iPhone4/touch4 上运行,

即使两个设备都是 iOS 4.1,

它也会导致 EXC_BAD_ACCESS所以...希望这个答案能帮助你节省上传时间图像到服务器。

OK,Here is a small bug between iPhone3 & iPhone4/touch4

@ dhil's Answer is almost right , You just need to give a right key (depends on Server)

[request setPostValue:requestString forKey:@"data"];
[request setFile:photoPath forKey:@"apiupload"];

Ok...now if you use iPhone3 , and you don't give requestString alloc

It runs fine ~

But if you run on iPhone4/touch4 ,It will cause EXC_BAD_ACCESS

even both device is iOS 4.1

So...Hope this answer will help you to save time about upload image to server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文