检查确实选择文件或否,输入:文件

时间:2011-10-12 09:37:27

标签: php arrays codeigniter upload

如果Choice中的select(Browse...input:file)文件用php运行某些代码,怎么知道?

<form action="#" method="post" enctype="multipart/form-data">
    <input type="file" name="userfile[]" valign="baseline">
</form>


if(select file){
    run my code
}

更新

我使用Multiple Uploads with JQuery and Code Igniter,当我没有选择文件时,输出print_r($_FILES['userfile']);是:

  

Array([error] =&gt; 4 [name] =&gt; [size] =&gt; 0 [tmp_name] =&gt; [type] =&gt;   [key] =&gt; userfile)

[error] => 4是 - &gt;您没有选择要上传的文件。

如果我选择文件,如果我在input:file这个name="userfile[]"Message: Undefined index: userfile,则会出现以下错误:

print_r($_FILES['userfile']);在这里:if ( ! $files )

更新2: 我使用这些Controller,但如果不选择文件,在这里class Upload extends Controller { function Upload() { parent::Controller(); $this->load->helper(array('form', 'url')); } function index() { $this->load->view('upload_form'); } function do_upload() { if($_FILE('userfile')){ $config['upload_path'] = './uploads/'; // server directory $config['allowed_types'] = 'gif|jpg|png'; // by extension, will check for whether it is an image $config['max_size'] = '1000'; // in kb $config['max_width'] = '1024'; $config['max_height'] = '768'; $this->load->library('upload', $config); $this->load->library('Multi_upload'); $files = $this->multi_upload->go_upload(); if ( ! $files ) { $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); } else { $data = array('upload_data' => $files); $this->load->view('upload_success', $data); } } redirect('inse/show'); } } 返回为false并给出错误。我想如果用户没有选择文件没有得到错误与if

{{1}}

1 个答案:

答案 0 :(得分:1)

<?php
  if (isset($_FILES['my_file_input'])) {
    // your code
  }
?>

注意:如果我说得对,你描述了错误的输入类型 - 文件输入应使用此签名:

<input type="file" name="my_file_input" />

注意:文件输入所在的表单应将其enctype设置为“multipart-form-data”(或类似内容)。

UPD:我可能错了,但这是来自您指定的模块的控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends Controller {
    function Upload()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {    
        $this->load->view('upload_form');
    }

    function do_upload()
    {
        $config['upload_path'] = './uploads/'; // server directory
        $config['allowed_types'] = 'gif|jpg|png'; // by extension, will check for whether it is an image

        $config['max_size']    = '1000'; // in kb
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);
        $this->load->library('Multi_upload');

        $files = $this->multi_upload->go_upload();

        if ( ! $files )        
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('upload_form', $error);
        } else {
            $data = array('upload_data' => $files);
            $this->load->view('upload_success', $data);
        }
    }    
}
?>

如果仍然没有获得任何令人满意的结果,请尝试var_dump($_FILES);并找出文件的存储方式。