【问题标题】:When I click on my link to access an uploaded PDF, I am not getting the correct one当我点击我的链接访问上传的 PDF 时,我没有得到正确的
【发布时间】:2014-07-27 15:26:18
【问题描述】:

我有一个只能有两条记录的表格书籍。此表将始终只存储两个 PDF。

一个 PDF 对应于 1st class 的 PDF 书。其他 PDF 对应 2º 和 3º 类的 PDF 书。我有一个管理面板,我可以在其中更新这两个 PDF。

要更新这些 PDF,我需要在下拉列表中选择一个类别(1º 类别或 2º 和 3º 类别),并且我还需要在我的输入文件中选择与所选课程对应的 PDF。

如果所选类的记录尚不存在,我将进行插入,否则我将进行更新。 (基本上只是我第一次插入)

我遇到的问题是:

我在我的书籍文件夹中成功上传了我的 PDF,当我在我的书籍文件夹中打开 PDF 时,我可以访问正确的 PDF。

但是我在输入文件下面有两个指向我当前的两个 PDF 的链接,当我单击访问每个 pdf 时,我没有得到正确的 pdf,我得到了一个主要用于测试的旧 PDF,现在我做了甚至在我的项目目录中都没有。

真的很奇怪,我不理解这种情况,似乎我总是在访问保存在缓存或其他东西中的 PDF 文件,但是当我打开上传的 PDF 时,在我的书籍文件夹中,总是正确的 PDF .

当我访问我的开发人员工具时,我的 PDF 链接具有正确的路径:<a style="margin-right:10px;" href="../../uploads/books/2014/07/1_class.pdf" title="Pdf of book of 1st class">Se current book of 1st class.</a>

您知道为什么会发生这种情况吗?这是我的书籍文件中的代码:

<div>
<?php
    //First I see if user submit my form
    if(isset($_POST['sendForm'])){
        //I store class that users chooses (can be 1º class or 2º and 3º class)
        $f['class'] = $_POST['class'];   
        //I get a title depending of each selected class   
        $title = ($f['class'] == '1' ? '1º class' : '2º and 3º class');
        //I see if there are empty fields
        if(in_array('',$f) || empty($_FILES['pdf']['tmp_name'])){
            echo 'Please fill all fields';
        }
        //If all fields were submited I Will upload selected pdf
        else{
            if(!empty($_FILES['pdf']['tmp_name'])){
                $folder = '../../uploads/books/';
                $year = date('Y');
                $month = date('m');
            if(!file_exists($folder.$year)){
                mkdir($folder.$year,0755);
            }
            if(!file_exists($folder.$year.'/'.$month)){
                mkdir($folder.$year.'/'.$month,0755);
            }
            $f['class'] = ($f['class'] == '1' ? '1_class' : '2_3_classs');
            $pdf = $_FILES['pdf'];
            $ext = substr($pdf['name'],-3);
            $name = $f['class'];
            $f['pdf'] = $year.'/'.$month.'/'.$name.'.'.$ext;
            move_uploaded_file($pdf['tmp_name'], $folder.$f['pdf']);
            }
            //If already exist a pdf book for selected class I will do an update with last selected pdf
            $readBooks = $pdo->prepare("SELECT * FROM books WHERE class = ?");
            $readBooks->bindParam(1,$f['class'],PDO::PARAM_STR);
            $readBooks->execute();
            $resultBooks = $readBooks->fetch(PDO::FETCH_ASSOC);
            if($readBooks->rowCount() >=1){
                $updEmt = $pdo->prepare("UPDATE books set title = ?, pdf =?, class=?, WHERE class = ?");
                $updBook->bindParam(1,$title);
                $updBook->bindParam(2,$f['pdf']);
                $updBook->bindParam(3,$f['class']);
                $updBook->bindParam(4,$f['class']);
                $updBook->execute();
                echo 'Book inserted with sucess';
            }
            //If dont exist a pdf book for selected class I will do an insert with last selected pdf
            else{
                $insEmt = $pdo->prepare("INSERT INTO books (title, pdf, class) VALUES (?,?,?)");
                $insEmt->bindParam(1,$title);
                $insEmt->bindParam(2,$f['pdf']);
                $insEmt->bindParam(3,$f['class']);
                $insEmt->execute(); 
                echo 'Book inserted with sucess';
            }
        }
    }
    ?>
    <form name="editpost" method="post" enctype="multipart/form-data">
        <!--I have a field to select class--> 

        <!--And then I have a field to select my book pdf:-->    
        <div class="label">
            <span class="field">Book pdf:</span>
            <input type="file"  name="pdf" accept="application/pdf" />
            <?php
            $readB = $pdo->prepare("SELECT * from books");
            $readB->execute(); 
            //below this field I want to show the current book for 1st class and for 2nd and 3rd class  
            echo '<div class="viewcapa">';
                while($resultBook = $readB->fetch(PDO::FETCH_ASSOC)){ 
                    echo '<a style="margin-right:10px;" href="../../uploads/books/'.$resultBook['pdf'].'"';
                            echo 'title="Pdf book of '.$resultBook['title'].'">';
                            echo 'See current book '.$resultBook['title'].'';
                    echo '</a>';
                }
            echo '</div>';  
            ?>                              
        </div>  
        <input type="submit" value="Insert" name="sendForm"/>
    </form>
</div>

【问题讨论】:

  • 您的“新”文件是否与“旧”文件具有相同的文件名?如果是这样,这可能是您建议的缓存问题。您是否尝试过清除浏览器缓存?解决此问题的一种方法可能是以某种方式为文件名加上时间戳。
  • 谢谢用户 3371942。问题确实如此,而您的解决方案,为我上传的文件提供一些 id 似乎有效。你可以给出你的答案,所以我可以接受。谢谢:)

标签: php file-upload upload


【解决方案1】:

您的“新”文件是否与“旧”文件具有相同的文件名?如果是这样,这可能是您建议的缓存问题。您是否尝试过清除浏览器缓存?

解决此问题的方法可能是以某种方式为文件名加上时间戳。

(之前的回答变成了评论?)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多