2017-06-21 85 views
0

我想用Swift和PHP將視頻上傳到Godaddy服務器 。我可以發送一個「2秒」的視頻到文件管理器,但不再有任何失敗。用純Swift上傳視頻到服務器,PHP代碼

// This function combining the unquie ID and the string -VIDEO.mov to make a file name for the body.appendString in cresteBodyWithPramsVideo function. 
func videoID() -> String{ 
    let unquieID = theDictionary.value(forKey: "unquieID") as! String 
    let filename = "-VIDEO.mov" 
    let filename2 = "\(unquieID)" + "\(filename)" 
    return filename2 
} 



func createBodyWithParamsVideo(_ parameters: [String: String]?, filePathKey: String?, imageDataKey: Data, boundary: String) -> Data { 

    let body = NSMutableData(); 

    if parameters != nil { 
     for (key, value) in parameters! { 
      body.appendString(string: "--\(boundary)\r\n") 
      body.appendString(string: "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n") 
      body.appendString(string: "\(value)\r\n") 
     } 
    } 


    let mimetype = "video/mov" 

    body.appendString(string: "--\(boundary)\r\n") 
    body.appendString(string: "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(videoID())\"\r\n") 
    body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n") 
    body.append(imageDataKey) 
    body.appendString(string: "\r\n") 
    body.appendString(string: "--\(boundary)--\r\n") 

    return body as Data 

} 




func uploadVideo(){ 

    let unquieID = theDictionary.value(forKey: "unquieID") as! String 

    // url path to php file 
    let url = URL(string: "http://www.myWebSite.com/Video.php")! 

    // declare request to this file 
    var request = URLRequest(url: url) 

    // declare method 
    request.httpMethod = "POST" 

    // param to be sent in body of request 
    let param = ["id" : unquieID] 

    // body 
    let boundary = "Boundary-\(UUID().uuidString)" 
    request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") 

    // Issign video to videoData var 
    let videoData = theDictionary.value(forKey: "theVideo") as! NSData 

    // ... body 
    request.httpBody = createBodyWithParamsVideo(param, filePathKey: "file", imageDataKey: videoData as Data, boundary: boundary) 

    // launch session 
    URLSession.shared.dataTask(with: request) { data, response, error in 
     if let response = response { 
     } 

     // get main queue to communicate back to user 
     DispatchQueue.main.async(execute: { 


      if error == nil { 

       do { 
        // json containes $returnArray from php 
        let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary 
        print(data!) 
        print(json!) 

        // declare new parseJSON to store json 
        guard let parseJSON = json else { 
         print("Error while parsing") 

         return 
        } 

        // get id from $returnArray["id"] - parseJSON["id"] 
        let id = parseJSON["id"] 

        // successfully uploaded 
      if id != nil { 
      UserDefaults.standard.set(parseJSON, forKey: "parseJSON") 
        // unquieID = UserDefaults.standard.value(forKey: "parseJSON") as? NSDictionary 

         // did not give back "id" value from server 
        } else { 
         //Do somthing 
         // get main queue to communicate back to user 
         // DispatchQueue.main.async(execute: { 
         // let message = parseJSON["message"] as! String 
         // appDelegate.infoView(message: message, color: colorSmoothRed) 
         //      }) 

        } 

        // error while jsoning 
       } catch { 

        // get main queue to communicate back to user 
        // DispatchQueue.main.async(execute: { 
        //// let message = error as! String 

        // appDelegate.infoView(message: message, color: colorSmoothRed) 
        //   }) 
        //     return 
       } 


       // error with php 
      } else { 

       // get main queue to communicate back to user 
       // DispatchQueue.main.async(execute: { 
       //// let message = error!.localizedDescription 
       print("message") 
       // appDelegate.infoView(message: message, color: colorSmoothRed) 
       //  }) 
       // return 
      } 


     }) 

     }.resume() 

} 

//Creating protocol of appending string to var of type data 
extension NSMutableData { 
func appendString(string : String) { 
    let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true) 
     append(data!) 
    } 
} 

的PHP

<?php 


//STEP 1. Check passed data to this php file 
if (empty($_REQUEST['id'])) { 
$returnArray["massage"] = "Missing required information"; 
return; 
} 


// Pass POST via htmlencryot and assign to $id 
$id = htmlentities($_REQUEST['id']); 



//STEP 2. Folder for uploaded Video. 
$folder = "VBCVideo/"; 



//STEP 3. Move uploaded file 
$folder = $folder . basename($_FILES["file"]["name"]); 

if (move_uploaded_file($_FILES["file"]["tmp_name"], $folder)) { 
    $returnArray["status"] = "200"; 
    $returnArray["massage"] = "The Video file has been uploaded"; 
} else { 
    $returnArray["status"] = "300"; 
    $returnArray["massage"] = "Error while uploading the Video"; 
} 


// STEP 8. Feedback array to app user 
echo json_encode($returnArray); 

?> 

現在,Godaddy的php.ini中的upload_max_filesize校正 「見下面我的意見」 的問題是一樣的。我現在可以上傳長達12秒的文件,不再上傳任何文件。我該從哪裏出發?

+0

好的,這裏是我在這裏的地方。 Godaddy中的PHP.ini文件被設置爲2米,這就是爲什麼較大的文件不會上傳的原因。我改變了這一點,轉到cPanel底部的標題下面。軟件點擊Select PHP version,然後點擊「up around the right right」切換到PHP選項,在底部你會找到upload_max_filesize,將其改爲你需要的大小,以及保存就是這樣。 – dscrown

回答

0

我必須在public_html下向Godaddy FileDirector添加一個PHP.ini文件。轉到「+文件」創建一個新文件並將其命名爲「php.ini」

將這些行添加到文件並保存。

max_execution_time = 120;

upload_max filesize = 60M;

post_max_size = 70M;

相關問題