检查是否选择了文件或否,在输入:文件中

发布于 2024-12-09 10:08:19 字数 2218 浏览 0 评论 0原文

如何知道 input:file 中的 select(Choice Or Browse...) 文件是否使用 php 运行一些代码?

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


if(select file){
    run my code
}

更新:

我使用多个使用 JQuery 和 Code Igniter 上传,当我不选择文件时,输出 print_r($_FILES['userfile']); 是:

数组 ( [错误] => 4 [名称] => [大小] => 0 [tmp_name] => [类型] => [关键] =>用户文件)

[错误] => 4 是 ->您没有选择要上传的文件。

如果我选择文件,如果我在 input:file 中有以下错误:name="userfile[]":

消息:未定义索引:userfile< /code> 在这里:print_r($_FILES['userfile']);

更新 2: 我从这些控制器中使用,但如果不选择文件,则此处的 if ( ! $files ) 返回 false 并给出错误。我希望如果用户没有选择文件,则不会出现 if 错误

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');
    }        
}

How can know, if select(Choice Or Browse...) file in input:file run some code with php?

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


if(select file){
    run my code
}

Update:

I use of Multiple Uploads with JQuery and Code Igniter , when that i don't select file, output print_r($_FILES['userfile']); is:

Array ( [error] => 4 [name] => [size] => 0 [tmp_name] => [type] =>
[key] => userfile )

[error] => 4 is -> You did not select a file to upload.

If i select file, have following error in case i have in input:file this: name="userfile[]":

Message: Undefined index: userfile in here:print_r($_FILES['userfile']);

Update 2:
I use from these Controller but if don't select a file, in the here if ( ! $files ) return is false and give error. i want if user did not select file does not get error with a if

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');
    }        
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想挽留 2024-12-16 10:08:19
<?php
  if (isset($_FILES['my_file_input'])) {
    // your code
  }
?>

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

注意: 文件输入所在的表单应将其 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); 和了解文件的存储方式。

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

NOTE: you descripted a wrong input type if i get you right - file input should use this signature:

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

NOTE: the form the file input is located within should have its enctype set to "multipart-form-data" (or something like that).

UPD: i may be wrong, but here's come controller from the module you have specified:

<?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);
        }
    }    
}
?>

If you still do not get any satisfying results, try var_dump($_FILES); and find out how the files are stored.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文