 |
|
ผมต้องการทำการเซฟภาพ จาก แอพ ios ลงยังเซิฟเวอร์ครับ
โดยจะเก็บพาทกับ url ไว้ในฐานข้อมูล แล้วเก็บไฟล์รูปภาพไว้ในโฟลเดอร์ครับ
คือตอนผม จำลองฐานข้อมูลในเครื่อง สามารถเก็บภาพใส่ลงในโฟลเดอร์ได้ครับ
แต่พออัพโหลดขึ้น โฮสจริง ไม่สามารถเก็บภาพลงโฟลเดอร์ได้ ผมควรทำไงครับ
โค้ดส่วน แปลงไฟล์รูปภาพในแอพครับ
NSData *imageData = UIImageJPEGRepresentation(self.imageView.image , 90);
NSString *urlString = @"http://www.letsgogoapp.com/apiletsgogo/apiuppic_locat.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------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=\".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]];
[request setHTTPBody:body];
NSError *error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
if(error)
{
NSLog(@"%@",[NSString stringWithFormat:@"%@",error.localizedDescription]);
return;
}
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
โค้ดนี้แปลงไฟล์ได้ปกติคับ ผมคิดว่าน่าจะไปติดปัญหาตรงส่วน php
UpImage.php
<?
ob_start();
session_start();
include("apiconnection.php");
$sstripID = $_SESSION["trip_ID"];
$sslocatID = $_SESSION["locat_ID"];
$ssimageID = $_SESSION["image_ID"];
$picnew_id =mysql_result(mysql_query("Select Max(imageID)+1 as MaxID from ImageDetail"),0,"MaxID");//เลือกเอาค่า id ที่มากที่สุดในฐานข้อมูล
$uploaddir = './trip/'; //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
//$randomNumber = rand(0, 999999);
$randomNumber = "locat0".$sslocatID."p".$picnew_id;
$newName = $uploadDir . $randomNumber . $uploadFile;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "Temp file uploaded. \r\n";
} else {
echo "Temp file not uploaded. \r\n";
}
if ($_FILES['userfile']['size']> 3000000) {
exit("Your file is too large.");
}
//$imageDetail = $_POST["imageDetail"];
$imageDateTime = date("Y-m-d H:i:s");
$imageurl = "http://www.letsgogoapp.com/apiletsgogo/trip/".$newName;
$_SESSION["image_ID"] = $picnew_id;
$strSQL = "INSERT INTO ImageDetail (imageID,tripID,locatID,imageDateTime,imagePath,imageUrlPic)
VALUES ('".$picnew_id."','".$sstripID."','".$sslocatID."','".$imageDateTime."','".$newName."','".$imageurl."')";
echo "insert OK!";
$objQuery = mysql_query($strSQL);
$arr = null;
if(!$objQuery)
{
$arr["Status"] = "0";
$arr["Message"] = "Insert Data Failed";
}
else
{
$arr["Status"] = "1";
$arr["Message"] = "Insert Data Successfully";
}
echo json_encode($arr);
if (move_uploaded_file($_FILES['userfile']['tmp_name'],"./trip/".$newName)) {
$postsize = ini_get('post_max_size'); //Not necessary, I was using these
$canupload = ini_get('file_uploads'); //server variables to see what was
$tempdir = ini_get('upload_tmp_dir'); //going wrong.
$maxsize = ini_get('upload_max_filesize');
echo "move sucess!!";
}
?>
พอรันแอพ มันแจ้งขึ้นว่า
Code
<b>Warning</b>: move_uploaded_file(./trip/locat019p19.jpg) [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: failed to open stream: Permission denied in <b>/home/qqzero/domains/letsgogoapp.com/public_html/apiletsgogo/apiuppic_locat.php</b> on line <b>57</b><br />
<br />
<b>Warning</b>: move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: Unable to move '/tmp/phpxQh6U3' to './trip/locat019p19.jpg' in <b>/home/qqzero/domains/letsgogoapp.com/public_html/apiletsgogo/apiuppic_locat.php</b> on line <b>57</b><br />
ตรงส่วน line 57 คือบรรทัดนี้ครับ
Code
if (move_uploaded_file($_FILES['userfile']['tmp_name'],"./trip/".$newName))
Tag : Mobile, Device (Mobile), iOS, iPhone, Mac
|
|
 |
 |
 |
 |
Date :
2013-02-25 17:00:42 |
By :
qqzero |
View :
1756 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |