【问题标题】:iPhone image upload to URL; upload_tmp_dir emptyiPhone 图片上传到 URL; upload_tmp_dir 为空
【发布时间】:2013-11-15 12:24:46
【问题描述】:

我知道很多人在这里问过类似的问题,但不知何故,我仍然无法正常工作。 为了使它更简单,我只是尝试将以前加载的 UIImage 存储在 ObjectiveC(使用 C4 框架)中,如下所示:

-(void)setup {
    UIImage *myImage=[UIImage imageNamed:@"image.jpg"];

    NSData *imageData = UIImageJPEGRepresentation(myImage, 1.0);
    // setting up the URL to post to
    NSString *urlString = @"my full server address/imageupload.php";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"dr.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    C4Log(@"returnString: %@", returnString);

}

然后我的upload.php 看起来像这样:

<?php
      $uploaddir = './'; //Uploading to same directory as PHP file
      $file = basename($_FILES['userfile']['name']);
      $uploadFile = $file;
      $randomNumber = rand(0, 99999); 

      $newName = $uploadDir . $uploadFile;

    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
   echo "Temp file uploaded. \r\n ";
    } else {
    echo "Temp file not uploaded. \r\n";
   }
   move_uploaded_file($_FILES['userfile']['tmp_name'], $newName);

         $postsize = ini_get('post_max_size'); 
         $canupload = ini_get('file_uploads');  
         $tempdir = ini_get('upload_tmp_dir'); 
         $maxsize = ini_get('upload_max_filesize');
         echo "http://mlab.taik.fi/UrbanAlphabets/{$file}" . "\r\nsize:" .  $_FILES['userfile']['size'] . "\r\ntype:" . $_FILES['userfile']['type']. "\r\npostsize:" . $postsize."\r\ncanupload:" . $canupload ."\r\ntempdir:" .$tempdir."\r\nmaxsize:".$maxsize;

?>

由于某种原因,'upload_tmp_dir' 看起来好像是空的,但我不知道如何修复它...... 这是回声:

Temp file uploaded. 

 MY SERVER/dr.jpg

size:31476

type:application/octet-stream

postsize:100M

canupload:1

tempdir:

maxsize:100M

有什么提示吗?

【问题讨论】:

    标签: php ios iphone c4


    【解决方案1】:

    脚本执行完成后,临时目录将始终为空。 所以你必须在脚本中移动你的文件..

    并且不要在临时目录中查找文件,因为它不会在您移动的地方查找它..

    这将有助于http://us2.php.net/move_uploaded_file

    【讨论】:

    • 如需更详细的解释,请使用this php 示例。
    • 显然我正在寻找我试图移动文件的位置,但我在那里找不到它。所以它似乎没有被写出来......
    • 发送时需要以字节为单位发送数据,即多部分,收到时总是显示application/octet-stream。
    • 啊,我想我知道您没有创建您要移动文件的目录。首先在服务器上创建要移动文件的目录。
    • 我测试了“image/jpg”,但没有帮助。此外,所有在线示例都使用“application/octet-stream”。关于图像目录...是的,它确实存在。我不会先将它放入任何文件夹中。只是简单的 php 文件在哪里。
    【解决方案2】:

    使用脚本中的完整路径指向上传目录,也仅在上传临时文件时移动临时文件。示例:

    <?php
          $uploaddir = '/Volumes/Web/mlab/mlabwww/UrbanAlphabets/'; //Uploading to same directory as PHP file
          $file = basename($_FILES['userfile']['name']);
          $uploadFile = $file;
          $randomNumber = rand(0, 99999); 
    
          $newName = $uploadDir . $uploadFile;
    
        if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
             echo "Temp file uploaded. \r\n ";
    
             move_uploaded_file($_FILES['userfile']['tmp_name'], $newName);
    
             $postsize = ini_get('post_max_size'); 
             $canupload = ini_get('file_uploads');  
             $tempdir = ini_get('upload_tmp_dir'); 
             $maxsize = ini_get('upload_max_filesize');
             echo "http://mlab.taik.fi/UrbanAlphabets/{$file}" . "\r\nsize:" .  $_FILES['userfile']['size'] . "\r\ntype:" . $_FILES['userfile']['type']. "\r\npostsize:" . $postsize."\r\ncanupload:" . $canupload ."\r\ntempdir:" .$tempdir."\r\nmaxsize:".$maxsize;
    
        } else {
             echo "Temp file not uploaded. \r\n";
       }
    
    ?>
    

    【讨论】:

    • 谢谢,但这并没有什么不同...它告诉 ne 它会上传文件,但之后我在目录中看不到它...
    • 您是否设置了完整路径?喜欢/home/user/public/uploads/
    • 我要加入“mlab.taik.fi/UrbanAlphabets”。如果这还不够,我如何找出完整路径?
    • 确实不行,完整路径可以通过:echo realpath(dirname(__FILE__));
    猜你喜欢
    • 2013-02-18
    • 1970-01-01
    • 2011-03-02
    • 2011-03-22
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多