dtvfshi5248 2015-08-01 08:21
浏览 137
已采纳

数组没有返回任何内容(但var_dump显示数据存在)

I'm trying to download a file from a database but my file is always empty. My vardump output shown below indicates that my data is correctly uploaded but when I use the header to download, I get a html file whose name is that of my download script instead of my intended file and its respective name. Please help me to point out my mistake.

This is my download script:

$Download = new Download();
$result = array(); //store results of output
$row = array();
try {
//execute retrieval of files from database
$Download-> showDBFiles();
//pass results to output array 
$output = $Download->getMessages();
//var_dump($output);
//if id is set then get file from database
if (isset($_GET['id'])) {
    $id = $_GET['id'];
    //pump id into function getDBFiles to pull file with matching id
    $Download->getDBFiles($id);
    $row = $Download->getMessages();
    $fileName = $row[0]['name'];
    $fileType = $row[1]['type'];
    $fileContent = $row[2]['content']; //content of file
    $fileSize = $row[3]['size']; //file size
    header('Content-Type:"' . $type . '"');
    header('Content-length:"' . $size . '"');
    header('Content-Disposition: attachment; filename="' . $fileName . '"');
    echo $content;
    //var_dump($row);
    exit;
  }
} catch (Exception $e) {
     $result[] = $e->getMessages();
}

This where the functions are called:

        protected $messages = array();

        public function showDBFiles() {
            global $database;
            //$sql = "SELECT resume_id, resume_title FROM ".self::$table_name." ORDER BY resume_id";
            $sql = "SELECT id, name FROM ".self::$table_name." ORDER BY id";
            $result = $database->query($sql);
            if(mysqli_num_rows($result)==0){
                 $this->messages[] = "Database is empty";
            }else{
                 while(list($id, $name) = mysqli_fetch_array($result, MYSQLI_BOTH)){
                     $this->messages[] = array('id'=>$id, 'name'=>$name);
                } 
            }
        }

        public function getDBFiles($id) {
            global $database;
            $sql = "SELECT * FROM ".self::$table_name." WHERE id = $id";
            $result = $database->query($sql);
            if ($row = $result->fetch_array(MYSQLI_ASSOC)) {     
                $name = $row['name'];
                $type = $row['type'];
                $content = $row['content']; //content of file
                $size = $row['size']; //file size
                $this->messages[] = array('name'=>$name, 'type'=>$type, 'content'=>$content, 'size'=>$size);
            }
        }

        public function getMessages(){
                return $this->messages;
        }

This is my vardump output:

array(1) { [0]=> array(4) { ["name"]=> string(14) "testing456.doc" ["type"]=> string(18) "application/msword" ["content"]=> string(86016) "...

  • 写回答

1条回答 默认 最新

  • douzhushen_9776 2015-08-01 09:00
    关注

    Ok ( after some judicial formatting .. I know I hate the wsywig too.) looks like you are building a standard multidimensional array

    array( 
       array( 'name' => 'blah' )
    )
    

    And then accessing it like this

    $row['name']
    

    When you really want this

    $row[0]['name']
    

    One row, not the many.

    You see the problem look at how you return return $messages; and set is $this->messages[] = array('name'=>... Also evident in your var_dump as array(1) { [0]=> array(4){ ["name"]=> therefor if you had display errors on you would probably get a warning undefined index, and that output would destroy your download headers.

    Then you would hear Wont woont woont from your computer, but seriously it's a very easy mistake to make, and sometimes a fresh pair of eyes go a long way.

    UPDATE

    Make note of how the array data is presented

     array(
          [0]=> array(
                ["name"]=> "testing456.doc",
                ["type"]=> "application/msword",
            )
     )
    

    So you want array[0]['name'] and array[0]['type']. But as I mentioned in the comments it's better to make it only one row, and limit it. That way it clears up the confusion and issue all together. Can you have more then one Row, that's a good question.

    You can limit it like this

        $sql = "SELECT id, name FROM ".self::$table_name." ORDER BY id LIMIT 1";
    

    You happen to have only 1 row right now, but in the future could you find more then that with your query. Not if you use limit.

    And where you save the data

      $this->messages = array('name'=>$name, 'type'=>$type, 'content'=>$content, 'size'=>$size);
    

    remove the [] altogether, this is only good if you ever have only one result from the database. Then it makes sense to change $this->messages to $this->message.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看