donpb2823 2014-03-19 13:02
浏览 75
已采纳

在网页上上传和显示文件

The code below is uploading and displaying files on the upload.php page and it's working fine. The problem I have is that it's not displaying the files if I copy and paste the url of the upload.php page into a new webpage.

upload.php code

<?php
if (isset($_FILES['file_upload'])) {
    $file = $_FILES['file_upload'];
    $name = $file['name'];
    $type = $file['type'];
    $tmp_location = $file['tmp_name'];
    $upload = 'uploads';
    $final_destination = $upload.'/'.$name;
    $error = $file['error'];
    $max_upload_size = 2097152;
    $size = $file['size'];

    $allowedImageTypes = array( 'image/png', 'image/jpeg', 'image/gif',  );

    function imageTypeAllowed($imageType){
        global $allowedImageTypes;

        if(in_array($imageType, $allowedImageTypes)){
            return true;
        }
        else{
            return false;
        }
    }

    //Check for errors
    if($error > 0 || is_array($error)){
        die("Sorry an error occured");
    }

    //Check if file is image
    //Only required if image is only whjat we need
    if(!getimagesize($tmp_location)){
        die("Sorry, you can only upload image types");
    }

    if(!imageTypeAllowed($type)){
        die("Sorry, file type is not allowed");
    }

    if(file_exists($final_destination)){
        $final_destination = $upload.'/'.time().$name;
    }

    if(!move_uploaded_file($tmp_location, $final_destination)){
        die("Cannot finish upload, something went wrong");
    }

    $handle = opendir('uploads');

    if($handle){
        while(($entry = readdir($handle)) !== false){
            if($entry != '.' && $entry != '..'){
                echo "<a href=\"uploads/$entry\">$entry</a><br>";
            }
        }
        closedir($handle);
    }
}
?>

<h2>File Successfully uploaded!</h2>
  • 写回答

1条回答 默认 最新

  • duanjianshen4871 2014-03-19 13:09
    关注

    If you indent your code to be human-readable, you'll find that the entire server-side code block is wrapped in this conditional:

    if (isset($_FILES['file_upload'])) {
        // all of your code
    }
    

    This means that all of that server-side code will execute only if a file_upload value is POSTed to the form. When you copy/paste the URL into a new browser window and invoke that request, you're invoking a GET request with no form values. Since you're not uploading a file in this request, the isset() condition evaluates to false and your code isn't executed.

    You should separate your functionality into two groups:

    1. Handling the upload.
    2. Displaying the current state of the data.

    The code for handling the upload should execute only when an upload is present. The code for displaying the data should execute always.

    If I'm reading your code correctly, all you should need to do is split out the last few parts:

    if (isset($_FILES['file_upload'])) {
        // the rest of your code
    }
    
    $handle = opendir('uploads');
    
    if($handle){
        while(($entry = readdir($handle)) !== false){
            if($entry != '.' && $entry != '..'){
                echo "<a href=\"uploads/$entry\">$entry</a><br>";
            }
        }
        closedir($handle);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料