raja
raja

Reputation: 41

File upload issue in codeigniter

Actually while uploading a file I am creating folder with timestamp and also saving folder name along with filename to the database this is working fine. While editing I have input type hidden in this I am posting value if user do not change files and also if folder exits already it should not create folder and If user change files that time it should create a new folder.Only it is going to if condition. not else part.Please help me someone.

Below is PHP code,

if(isset($_FILES['event_image']['name'])&& $_FILES['event_image']['name']!="")
{
    $folder_name =$_FILES['event_image']['name'];
    echo $folder_name;
}   
else
{
    $folder_name =$_POST['image_exists'];
    echo $folder_name;
}         
    //print_r($pathToUpload);die;
    $old = umask(0);
if (!file_exists($folder_name))
{
  //echo "djfgd";die;
  $folderName = $_POST['event_name'].time();
  $pathToUpload = 'images/events/' . $folderName;
  $create = mkdir($pathToUpload, 0777,true);
  //chmod($pathToUpload,0755,true);
  if ( ! $create )
  return;
  }
  else{
  //echo "dqqqqqq";die;
  //$folderName = $_POST['event_name'].time();
  //$pathToUpload = 'images/events/' . $folderName;
  //$create = mkdir($pathToUpload, 0777,true);
  echo "already exits";die;
  }
  umask($old); 

HTML code:

<div class="col-md-6">
   <div class="form-group">
    <label for="event_image">Event Banner</label>
     <input type="file" value="<?php echo $event_image; ?>" 
     class="form-control" id="eventimage"  name="event_image">
     <input type="hidden" name="image_exists" value="<?php echo $event_image;?>" 
     class="form-control" id="eventimage" placeholder="Enter Image Text" aria-describedby="fileHelp">
     <div><?php echo $event_image;?></div>
   </div>
</div>

Upvotes: 0

Views: 61

Answers (1)

Mudassar Khani
Mudassar Khani

Reputation: 1479

Alright I have simulated your case on my machine using Codeigniter and simple PHP is_dir function.

Here is my test view

enter image description here

Created with Following Markup

<div class="container" style="margin-top:100px; ">
    <div class="col-md-6" style="border: 1px dotted red; ">
        <?php if(isset($errors)){?>
            <div class="alert alert-danger">
                <?php print_r($errors);?>
            </div>
        <?php }?>
        <?php if(isset($success)){?>
            <div class="alert alert-success">
                <?php print_r($success);?>
            </div>
        <?php }?>
        <form action="" method="post" id="form1">
            <div class="form-group">
                <label > Directory Name</label>
                <input type="text" name="name" class="form-control" >
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-danger" >Create</button>
            </div>
        </form>
    </div>

</div>

My Controller

public function index()
{
    $data['title']='Multiple submit';
    if($_POST)
    {
        $path='uploads/'.$_POST['name'];
        if (!is_dir($path)) {
            mkdir($path);
            $data['success']='Directory Created Successfully';
            $this->load->view('form',$data);
        }
        else
        {
            $data['errors']='Directory Already Exist';
            $this->load->view('form',$data);
        }
    }
    else
    {
        $this->load->view('form',$data);
    }

}

I create a directory, which is success.

enter image description here

I try it again, get an error

![enter image description here

The Directory created in my path is

enter image description here

I hope you can modify this code as per your requirements.

Upvotes: 2

Related Questions