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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line