Tôi đang cố gắng tải lên 3 hình ảnh lên máy chủ từ iOS lên dịch vụ PHP của mình. Vì lý do nào đó thư mục vẫn trống sau khi đăng, nhưng phần còn lại của dữ liệu đi qua và được tiếp tục tồn tại trong mysql.Tải lên PHP từ iOS bằng cách sử dụng Alamofire
Mã của tôi iOS:
Alamofire.upload(.POST, urlString, multipartFormData: {
multipartFormData in
if (firstPhoto != nil) {
if let firstImageData = UIImageJPEGRepresentation(firstImage!, 0.6) {
print("uploading image");
multipartFormData.appendBodyPart(data: firstImageData, name: "firstImage", fileName: "firstImage.png", mimeType: "image/png")
}
}
if (secondPhoto != nil) {
if let secondImageData = UIImageJPEGRepresentation(secondImage!, 0.6) {
print("uploading image");
multipartFormData.appendBodyPart(data: secondImageData, name: "secondImage", fileName: "secondImage.png", mimeType: "image/png")
}
}
if (thirdPhoto != nil) {
if let thirdImageData = UIImageJPEGRepresentation(thirdImage!, 0.6) {
print("uploading image");
multipartFormData.appendBodyPart(data: thirdImageData, name: "thirdImage", fileName: "thirdImage.png", mimeType: "image/png")
}
}
if let imageData = UIImageJPEGRepresentation(firstImage!, 0.6) {
print("uploading image");
multipartFormData.appendBodyPart(data: imageData, name: "image", fileName: "file.png", mimeType: "image/png")
}
if let imageData = UIImageJPEGRepresentation(firstImage!, 0.6) {
print("uploading image");
multipartFormData.appendBodyPart(data: imageData, name: "image", fileName: "file.png", mimeType: "image/png")
}
for (key, value) in parameters {
multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
}
}, encodingCompletion: {
encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
print("Запрос отправлен")
upload.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
completion(response.result.value)
}
case .Failure(let encodingError):
print(encodingError)
completion(nil)
}
})
}
Mở PHP của tôi Tôi không chắc chắn nếu tôi thiếu bất cứ điều gì ở đây:
<?php
require_once 'Functions/common_functions.php';
require_once 'Functions/engine.php';
require_once 'Functions/json.php';
require_once 'Functions/check_token.php';
if (count($_REQUEST) == 6) {
foreach($_REQUEST as $k => $v) {
if (strlen($k) > 0 && strlen($v) > 0) {
$_REQUEST[$mysqli->real_escape_string($k)] = $mysqli->real_escape_string($v);
} else {
die(errorJSON("Empty parameter", "666"));
}
}
$userID = checkAccessToken($_REQUEST['access_token'], $mysqli);
if ($userID == 0) {
die(errorJSON("Incorrect access token", "666"));
}
$mysqli->query('INSERT INTO Book (title, authorID, detail, price, categoryID, conditionText) VALUES ("'.$_REQUEST['title'].'", "'.$userID.'", "'.$_REQUEST['detail'].'", "'.$_REQUEST['price'].'", "'.$_REQUEST['categoryID'].'", "'.$_REQUEST['condition'].'")');
$insertID = $mysqli->insert_id;
if ($insertID > 0) {
savePhotos($mysqli, $insertID);
$message = book($insertID, $userID, $mysqli);
die(successJSON($message));
} else {
die(errorJSON("Failed insert book in database", "666"));
}
} else {
die(errorJSON("Incorrect count of parameter" . count($REQUEST) . count($_POST), "666"));
}
function savePhotos($mysqli, $bookID) {
if(!is_dir('/home/thebakpa/resources/book_photos/')) {
mkdir('/home/thebakpa/resources/book_photos/', 0777, true);
}
try {
$uploaddir = '/home/thebakpa/resources/book_photos/';
$file = $_FILES['userfile']['photo1'];
if ($file != 0) {
$uploadfile = $uploaddir . $file;
$name = "photo1-".$bookID;
$photoPath = '/home/thebakpa/resources/book_photos/'.$name.'.jpg';
$mysqli->query('UPDATE Book SET photo1='.$photoPath.' WHERE bookID='.$bookID.'');
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
}
$file = $_FILES['userfile']['photo2'];
if ($file != 0) {
$uploadfile = $uploaddir . $file;
$name = "photo2-".$bookID;
$photoPath = '/home/thebakpa/resources/book_photos/'.$name.'.jpg';
$mysqli->query('UPDATE Book SET photo1='.$photoPath.' WHERE bookID='.$bookID.'');
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
}
$file = $_FILES['userfile']['photo3'];
if ($file != 0) {
$uploadfile = $uploaddir . $file;
$name = "photo3-".$bookID;
$photoPath = '/home/thebakpa/resources/book_photos/'.$name.'.jpg';
$mysqli->query('UPDATE Book SET photo1='.$photoPath.' WHERE bookID='.$bookID.'');
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
}
} catch(Exception $ex){
echo "ERROR:".$ex->GetMessage()."\n";
exit(1);
}
}
?>
Tôi thực sự thích câu hỏi này. Chỉ cần tìm thấy nó với 35 phút còn lại trên bộ đếm tiền thưởng. Tôi nhận thấy không có câu trả lời nào của bạn có quyền bầu cử. Xin vui lòng thông báo cho tôi nếu nó đi lên cho tiền thưởng một lần nữa và không được giải quyết. Tôi muốn xây dựng một giải pháp xung quanh mã được đăng của bạn thêm một chút thời gian nữa. Nên thêm, tôi sẽ giải quyết bằng tiền thưởng. –
tôi nghĩ rằng người dùng đã đăng câu hỏi không hoạt động trên câu hỏi này. Bởi vì câu trả lời của tôi là chính xác và tôi đã thử với mã mẫu cũng nhưng không có phản hồi. –
Thật tuyệt khi tôi chỉ đọc sách của bạn. Tôi đã bỏ phiếu trước đó ... nhưng sau đó dừng bản thân mình vì tôi đã không thử nghiệm nó. Tôi sẽ cho bạn phiếu bầu lên;) Có vẻ đúng. –