如何使用php mysql Xcode从应用程序上传图像并保存为文件系统以及数据库

时间:2015-01-27 10:41:45

标签: php ios mysql iphone xcode

我正在尝试上传数据库中的图像以及服务器中的某个文件夹我怎么在文件夹中我正在获取图像但总是只有一个当我上传它替换现有的我之前上传的内容甚至不保存在我的数据库中我想在数据库和文件系统中保存图像,以便我进一步使用Web。我知道我要么缺少一些东西,但我不知道我有点新意,请帮助我的代码

 NSData *imageData = UIImageJPEGRepresentation(self.imageview.image,90);
                NSString *urlString = @"http://befirstwin.com/upload.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"];
                // file
                NSMutableData *body = [NSMutableData data];
                [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

                [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
                //[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", uname]] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[NSData dataWithData:imageData]];
                [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
                // close form
                [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                // set request body
                [request setHTTPBody:body];
                //return and test
                NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
                NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:returnString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                [alert show];

for php

<?php

//my connection


$abdb = $_FILES['userfile']; //getting image Here //getting textLabe Here 
    $target_path = "abdbphoto/";
     if(move_uploaded_file($abdb['tmp_name'], $target_path . basename( $abdb['name'])))

    { echo "The file ". basename( $abdb['name']). " has been uploaded"; } 

    else 

    {
     echo "There was an error uploading the file, please try again!";
     } 
   if (isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0) { 

      // Temporary file name stored on the server
      $tmpName  = $_FILES['userfile']['tmp_name'];  
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);



if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

      // Create the query and insert
      // into our database.
     $query = "INSERT INTO pictures (image ) ".
"VALUES ('$content')";
      $result = mysql_query($query);

if(!$result)
{
die("we have problem");
}
else
{
      // Print results
      print "Thank you, your file has been uploaded.";
}
}
else {
   print "No image selected/uploaded";
}   
mysql_close();
?>

请检查出来,请提出我缺少的内容,并提供图像数据库文件我有我的电子邮件ID,我不知道如何同时发送它...提前感谢

0 个答案:

没有答案