将文件从IPhone / IPad上传到mysql DB?

时间:2011-10-22 01:13:58

标签: php iphone mysql ios upload

我一直在搜索关于管理IOS数据的内容,最后我构建了一个下载PDF档案的应用程序,当用户下载PDF时,档案显示在UITableView上,用户可以看到PDF。现在我需要对我的项目做最重要的事情,将这些档案上传到我的MYSQL数据库!我在网站上发现了一个非常繁忙的教程,但该应用只是上传图像,所以如果有人知道,我怎样才能上传PDF档案!教程或想法会有所帮助! 先谢谢!

PHP SCRIPT:

<?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>';
?>

1 个答案:

答案 0 :(得分:1)

尝试使用以下库上传PDF文档:http://allseeing-i.com/ASIHTTPRequest/How-to-use