CodeIgniter - 图片提交按钮不工作 [英] CodeIgniter - Image submit button not working

查看:104
本文介绍了CodeIgniter - 图片提交按钮不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CI为客户端创建项目,我有一个提交按钮作为图像,但似乎没有提交表单,我现在的代码是

 < input type =imagename =trialSubmitid =trialSubmitsrc =<?php echo base_url /subscribe_free.jpg';?> style =height:29px; width:207px; border-width:0px; /> 

我现在必须使用的代码如下

 < input type =submitname =trialSubmitvalue =Subscribe Freeid =class =button/& 

如果任何人可以说明为什么它不工作的图像, / p>

干杯,

解决方案

  **视图内(如我的案例中的商业)**,



< form action =<?php echo site_url()?& / douploadmethod =postenctype =multipart / form-data>
< b>选择要上传的图片(最大尺寸:500 kb,jpg,jpeg):< / b>
< input style =color:#00A76Ftype =filename =fileToUploadid =fileToUpload>
< div class =input-groupstyle =left:10%; width:85%;>

< input class =btn btn-success pull-rightstyle =background:#00A76Ftype =submitvalue =上传图片name =submit



< / div>


< / form>
< div class =modal-body>
< div id =user_data>
<?php
if(!empty($ massage)){
echo $ massage;

}
?>
< / div>

**在控制器中定义一个方法**

public function doupload(){
$ root1 = $ _SERVER ['DOCUMENT_ROOT'] ;;

$ target_dir = $ root1。/ nawaloka / uploads /;

// $ target_dir = $ root1。/ nawaloka / application /;
$ target_file = $ target_dir。 basename($ _ FILES [fileToUpload] [name]);

$ uploadOk = 1;
$ imageFileType = pathinfo($ target_file,PATHINFO_EXTENSION);

//检查文件是否已经存在
//检查文件大小
if($ _FILES [fileToUpload] [size]> 500000){
$ data ['massage'] =对不起,您的文件太大了。
$ partials = array('content'=>'Admin / comerciales');
$ this-> template-> load('template / admin_template',$ partials,$ data);

$ uploadOk = 0;
}
if(is_dir($ target_dir)&& is_writable($ target_dir)){

//上传逻辑
} else {
echo'上传目录不可写,或不存在。
}



//允许某些文件格式
if($ imageFileType!=jpg& $ imageFileType!=jpeg ){
$ data ['massage'] =对不起,只允许JPG,JPEG文件。
$ partials = array('content'=>'Admin / comerciales');
$ this-> template-> load('template / admin_template',$ partials,$ data);

$ uploadOk = 0;
}
//检查$ uploadOk是否由于错误而设置为0
if($ uploadOk == 0){
$ data ['massage'] =对不起,您的文件未上传。
$ partials = array('content'=>'Admin / comerciales');
$ this-> template-> load('template / admin_template',$ partials,$ data);

//如果一切正常,尝试上传文件
} else {
array_map('unlink',glob(/ var / www / html / nawaloka / uploads / *));

if(move_uploaded_file($ _ FILES [fileToUpload] [tmp_name],/ var / www / html / nawaloka / uploads / shamith.jpg)){
$ data ['massage'] =图像。 basename($ _FILES [fileToUpload] [name])。 已上传。
$ partials = array('content'=>'Admin / comerciales');
$ this-> template-> load('template / admin_template',$ partials,$ data);


// echo文件。 basename($ _FILES [fileToUpload] [name])。 已上传。
} else {

$ data ['massage'] =很抱歉,上传文件时出错。
$ partials = array('content'=>'Admin / comerciales');
$ this-> template-> load('template / admin_template',$ partials,$ data);
}
}




}


I am using CI to create a project for a client, I have a submit button as an image but it doesn't seem to be submitting the form, the code I have at the moment is.

<input type="image" name="trialSubmit" id="trialSubmit" src="<?php echo base_url().'images/subscribe_free.jpg'; ?>" style="height:29px;width:207px;border-width:0px;" />

The code I have to use at the moment is as follows

<input type="submit" name="trialSubmit" value=" Subscribe Free " id="" class="button" />

If anyone could shed some light on why it's not working with the image, that would be tight.

Cheers,

解决方案

**inside the view(as comerciales in my case)**, 



    <form action= "<?php echo site_url()?>/admin/doupload"  method="post" enctype="multipart/form-data" >
  <b>  Select the image to upload( Maximum size 500 kb, jpg, jpeg): </b>
    <input   style="color:#00A76F" type="file" name="fileToUpload" id="fileToUpload">
   <div  class="input-group" style="left:10%;width:85%;">

 <input class="btn btn-success pull-right" style="background:#00A76F" type="submit" value="Upload Image" name="submit">



</div>


</form>
   <div class="modal-body">
                    <div id="user_data">
                      <?php
                      if (!empty($massage)){
                          echo $massage ;

                      }
                        ?>
                    </div>

**inside the controller define a method**

    public function doupload(){
       $root1 = $_SERVER['DOCUMENT_ROOT'];;

        $target_dir =  $root1."/nawaloka/uploads/";

             // $target_dir =  $root1."/nawaloka/application/";
          $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if file already exists
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
   $data['massage']= "Sorry, your file is too large.";
            $partials = array('content' => 'Admin/comerciales');
            $this->template->load('template/admin_template', $partials,$data);

    $uploadOk = 0;
}
if (is_dir($target_dir) && is_writable($target_dir)) {

    // do upload logic here
} else {
    echo 'Upload directory is not writable, or does not exist.';
}



// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "jpeg" ) {
     $data['massage']= "Sorry, only JPG, JPEG files are allowed.";
            $partials = array('content' => 'Admin/comerciales');
            $this->template->load('template/admin_template', $partials,$data);

    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
   $data['massage']= "Sorry, your file was not uploaded.";
            $partials = array('content' => 'Admin/comerciales');
            $this->template->load('template/admin_template', $partials,$data);

// if everything is ok, try to upload file
} else {
            array_map('unlink', glob("/var/www/html/nawaloka/uploads/*"));

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],"/var/www/html/nawaloka/uploads/shamith.jpg")) {
           $data['massage']= "The image ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
            $partials = array('content' => 'Admin/comerciales');
            $this->template->load('template/admin_template', $partials,$data);


        //echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {

             $data['massage']= "Sorry, there was an error while uploading your file.";
            $partials = array('content' => 'Admin/comerciales');
            $this->template->load('template/admin_template', $partials,$data);
    }
}




}

这篇关于CodeIgniter - 图片提交按钮不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆