我一直在搜索很多關於管理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>';
?>
你問到我的教程。如何搜索堆棧溢出? http://stackoverflow.com/search?q=Upload+files+IPhone+to+mysql+DB%3F – vikingosegundo