我一直在搜索有关在 IOS 上管理数据的大量信息,最后我构建了一个下载 PDF 文件的应用程序,当用户下载 PDF 时,文件显示在 UITableView 上并且用户可以看到 PDF。现在我需要对我的项目做最重要的事情,将这些文件上传到我的 MYSQL 数据库!我在网站上找到了一个非常繁忙的教程,但该应用程序只上传图像,所以如果有人知道,我如何上传 PDF 文件!任何教程或想法会有所帮助! 提前致谢!
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>';
?>
最佳答案
尝试使用以下库上传您的 PDF 文档:http://allseeing-i.com/ASIHTTPRequest/How-to-use
关于php - 将文件从 IPhone/IPad 上传到 mysql 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7856791/