2011-10-22 106 views
1

我一直在搜索很多關於管理IOS上的數據,最後我構建了一個下載PDF檔案的應用程序,當用戶下載PDF時,檔案顯示在UITableView上,用戶可以看到PDF。現在我需要做對我的項目來說最重要的是,將這些檔案上傳到我的MYSQL數據庫!我在網站上發現了一個非常繁忙的教程,但該應用程序只是上傳圖像,所以如果有人知道,我該如何上傳PDF檔案!或想法會有所幫助! 感謝先進!將文件從iPhone/iPad上傳到mysql數據庫?

php腳本:

<?php 
// Check if a file has been uploaded 
if(isset($_FILES['uploaded_file'])) { 
    // Make sure the file was sent without errors 
    if($_FILES['uploaded_file']['error'] == 0) { 
     // Connect to the database 
     $dbLink = new mysqli('localhost', 'root', 'root', 'fileUP'); 
     if(mysqli_connect_errno()) { 
      die("MySQL connection failed: ". mysqli_connect_error()); 
     } 

     // Gather all required data 
     $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']); 
     $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']); 
     $data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name'])); 
     $size = intval($_FILES['uploaded_file']['size']); 

     // Create the SQL query 
     $query = " 
      INSERT INTO `file` (
       `name`, `mime`, `size`, `data`, `created` 
      ) 
      VALUES (
       '{$name}', '{$mime}', {$size}, '{$data}', NOW() 
      )"; 

     // Execute the query 
     $result = $dbLink->query($query); 

     // Check if it was successfull 
     if($result) { 
      echo 'Success! Your file was successfully added!'; 
     } 
     else { 
      echo 'Error! Failed to insert the file' 
       . "<pre>{$dbLink->error}</pre>"; 
     } 
    } 
    else { 
     echo 'An error accured while the file was being uploaded. ' 
      . 'Error code: '. intval($_FILES['uploaded_file']['error']); 
    } 

    // Close the mysql connection 
    $dbLink->close(); 
} 
else { 
    echo 'Error! A file was not sent!'; 
} 

// Echo a link back to the main page 
echo '<p>Click <a href="form.php">here</a> to go back</p>'; 
?> 
+0

你問到我的教程。如何搜索堆棧溢出? http://stackoverflow.com/search?q=Upload+files+IPhone+to+mysql+DB%3F – vikingosegundo

回答

1

嘗試使用以下庫上傳PDF文檔:http://allseeing-i.com/ASIHTTPRequest/How-to-use

+0

我試過了,但沒有任何反應!可以使用PHP腳本嗎?就像我選擇的文件和UIActionSheet出現,所以我選擇PDF並點擊上傳,我的請求被髮送到一個PHP腳本和文件上傳!這是可能的嗎?我很困惑這些問題,如果你知道任何教程,請發佈鏈接! –

+0

是的,將PDF文件發佈到PHP腳本。 PHP腳本應該將插入處理到MySQL數據庫中。 – Ziminji

+0

我使用一個普通的php腳本在pc上發佈文件,用戶點擊'選擇文件'並顯示一個窗口,但這不適用於iPhone,所以我失去了如何執行我的想法,我應該添加和行動到'didSelectedRowAtIndex',在那裏我執行PHP代碼? –

相關問題