将文件从iPhone/iPad上传到mysql DB? [英] Upload files from IPhone/IPad to mysql DB?
问题描述
我一直在搜索有关在IOS上管理数据的很多东西,最后我构建了一个下载PDF档案的应用程序,当用户下载PDF时,档案会显示在UITableView上,用户可以看到PDF.现在我需要对我的项目最重要的事情做,将这些档案上传到我的MySQL数据库!我在网站上发现了一个非常繁忙的教程,但是该应用只是上传图像,所以如果有人知道,我怎么能上传PDF档案!教程或想法会有所帮助!在此先谢谢您!
I've been searching a lot about managing data on IOS and finally i build an app that downloads PDF archives,when the user download a PDF the archive is displayed on a UITableView and the user can see the PDF.Now i need do to the most important thing to my project,upload those archives to my MYSQL DB!I've found an very busy tutorial on a website,but the app just upload images,so if anyone know,how can i upload PDF archives!Any tutorial or idea will be helpful! Thanks in advanced!
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>';
?>
推荐答案
Try using the following library to upload your PDF documents: http://allseeing-i.com/ASIHTTPRequest/How-to-use
这篇关于将文件从iPhone/iPad上传到mysql DB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!