javascript - 使用 PHP 上传 PDF 的问题

标签 javascript php html pdf

我的 PDF 上传脚本有 2 个问题。

  1. 我注意到只有某些 PDF 文件会上传,而某些 PDF 文件则不会。我注意到有效的文件大小不到 100 kb。没有的则在 MB 中。有办法改变这个吗?

  2. 如果 PDF 文件上传后名称中包含空格,则会导致链接损坏。有没有办法对其进行编码,以便 html 能够正确引导它?尝试通过谷歌搜索,但找不到任何东西。希望你们能对此有所了解......

这是我的 PHP:

<?php
include_once 'includes/db_connect.php';
include_once 'includes/psl-config.php';

$title = $_POST['title'];
$id = $_POST['id'];

if ($_FILES["file"]["size"] > 0)
{
    $targetfolder = "uploads/";

    $targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;

    $fileName =  basename( $_FILES['file']['name']) ;

    $url = $_POST['url'];

    $file_type=$_FILES['file']['type'];

    echo "hiii";

        if ($file_type=="application/pdf" || $file_type=="application/x-pdf") 
        {
            unlink($url);

            if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))
            {

                if ($insert_stmt = $mysqli->prepare("UPDATE content SET title = ?, fileName = ?, url = ? WHERE id = ?;")) 
                {
                    $insert_stmt->bind_param('sssd', $title, $fileName, $targetfolder, $id );
                        if (! $insert_stmt->execute()) 
                        {
                            echo "Error processing your request";
                        }
                    echo "<p>The file is updated</p>";
                    echo"<p>Return to the <a href='protected_page.php'>content page</a></p>";
                }
                else
                {
                    echo "<p> Failed to upload to the server</p>
                    <p> Return back to <a href='edit_content.php'>edit page</a></p>";
                }

            }

            else 
            {
                echo "<p>Problem uploading file</p>";
            }

        }

        else 
        {
            echo "<p>You may only upload PDFs</p>
                <p> Return back to <a href='edit_content.php'>edit page</a></p>
                ";
        }
}
else
{
    if ($insert_stmt = $mysqli->prepare("UPDATE content SET title = ? WHERE id = ?;")) 
    {
        $insert_stmt->bind_param('sd', $title, $id );
        if (! $insert_stmt->execute()) 
        {
            header('Location: ../error.php?err=Registration failure: update');
        }
            echo "<p>The file was updated</p>";
            echo"<p>Return to the <a href='protected_page.php'>content page</a></p>";
    }
    else
    {
        echo "<p> Failed to upload to the server</p>
            <p> Return back to <a href='add_content.php'>edit page</a></p>";
    }

}
?>

PHP代码中有一个echo "hiii";。如果有一个 PDF 文件不会被接受,它不会回显,所以我假设它与文件大小有关? 这是 HTML:

<?php
include_once 'includes/db_connect.php';
include_once 'includes/psl-config.php';
include_once 'includes/functions.php';

sec_session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Secure Login: Edit</title>
        <script type="text/JavaScript" src="js/sha512.js"></script> 
        <script type="text/JavaScript" src="js/forms.js"></script>
        <script type="text/Javascript" src="js/jQuery-1.7.js"></script>
        <link rel="stylesheet" href="styles/main.css" />
    </head>
    <body>
        <?php if (login_check($mysqli) == true) : ?>
            <?php
                $username = htmlentities($_SESSION['username']);
                $privileges = mysqli_query($mysqli, "Select privileges From members Where privileges = 'Admin' AND username = '$username' LIMIT 1");
                $rowPriv =mysqli_fetch_array($privileges);

                if ($rowPriv[0] == "Admin")
                {
                    $id = $_GET['id'];
                    $editRow = mysqli_query($mysqli, "Select ID, title, url, fileName From content Where ID = $id;");
                    echo "<h1>Edit Content</h2>";

                        while ($rowEdit = mysqli_fetch_array($editRow))
                        {
                        echo "<form action='edit_file.php' method='post' id='editForm' enctype='multipart/form-data'>

                            <table cellspacing='10'>
                                <tr>
                                    <td>
                                        Title:
                                    </td>
                                    <td>
                                        <input name='title' id='title' type='text' value='".$rowEdit['title']."' size='40' >
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Upload Different File
                                    </td>
                                    <td>
                                        <input type='checkbox' id='checkbox' checked>
                                    </td>
                                </tr>
                                <tr id='file'>
                                    <td>
                                        File:
                                    </td>
                                    <td>
                                        <input type='file' name='file' size='50'/>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input name='id' type='hidden' value='".$rowEdit['ID']."'>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input name='url' type='hidden' value='".$rowEdit['url']."'>
                                    </td>
                                </tr>
                            </table>";
                            }
                            echo "<br />                        
                            <input type='button' id='edit' value='Submit' />
                        </form>
                        <script>

                            $('#edit').click(function(){
                                var title = $('#title').val();
                                if (title != '')
                                {
                                    $('#editForm').submit();
                                    return true;
                                }
                                else{
                                    alert('Must enter a Title');
                                    return false;
                                }
                            });
                            $('#checkbox').live('change', function(){
                                    if ( $(this).is(':checked') ) {
                                         $('#file').show();
                                    } else {
                                         $('#file').hide();
                                     }
                            });

                        </script>";



                }
                else
                {
                    echo "<p><span>Sorry, you do not have privileges</span></p>";
                }


            echo"<p>Return to the <a href='protected_page.php'>content page</a>.</p>
            <p>Return to the <a href='login.php'>login page</a>.</p>";
        ?>
        <?php else : ?>
            <p>
                <span class="error">You are not authorized to access this page.</span> Please <a href="login.php">login</a> with a privileged user.
            </p>
        <?php endif; ?>
    </body>
</html>

最佳答案

对于问题#2,您可以重命名上传的文件,用下划线替换空格,这样就不会出现损坏的链接

$targetfolder = $targetfolder . basename( $_FILES['file']['name']);

将会

$targetfolder = $targetfolder . basename( str_replace(' ', '_', $_FILES['file']['name']) );

关于javascript - 使用 PHP 上传 PDF 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24961958/

相关文章:

php - 不能将类型的对象用作数组

php - 如何使用 Yii2 架构构建器进行迁移?

javascript - TikTok 嵌入式视频不断消失

javascript - jQuery 数据表 : Individual column searching on table header

javascript - 获取所有数字的除数对,达到一定限制

javascript - 单击按钮将光标置于搜索表单中

html - 全宽/高度 div css 细节

javascript - 使用外部配置加载 lodash 时出现 Webpack 错误

PHP + mySQL - 精确匹配单词

html - 具有全宽元素的跨度样式间距