当我点击我的链接访问上传的PDF时,我找不到正确的PDF

时间:2014-07-27 15:26:18

标签: php file-upload upload

我有一本只有两张唱片的桌子。此表将始终只存储两个PDF。

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

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

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

我遇到的问题是:

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

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

真的很奇怪,我不理解这种情况,似乎我总是访问保存在缓存中的PDF文件,但是当我打开上传的PDF时,在我的books文件夹中,始终是正确的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>

1 个答案:

答案 0 :(得分:1)

你的&#34;新&#34;文件与&#34; old&#34;具有相同的文件名文件?如果是这样,它可能是您建议的缓存问题。您是否尝试过清除浏览器缓存?

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

(之前的回答成为评论?)