Welcome Guest, Not a member yet? Register   Sign In
Having issues getting uploader to upload multiple files to seperate directories
#1

[eluser]Unknown[/eluser]
Hey guys,

So the issue isn't as bad as it seems. What i'm trying to do is set up my uploader so that when a user chooses a file (say a zip file) and a picture, it uploads the file to one folder, and the picture to a seperate directory.

I've kinda got it working. where it'll accept both files. but it'll only upload one.

the error i'm getting is that it'll say the upload worked, but only the one that was uploaded first will upload( I.E if it uploads the file first, the picture will fail. and vice versa)

Has anybody had a similar issue? is it even possible to upload 2 items to seperate directories with the uploader class?

I've attached a copy of the functions used in the controller as well as the view:

Uploader controller
Code:
$this->form_validation->set_rules('add[name]', 'Name', 'required');
      $this->form_validation->set_rules('add[type]', 'Type', 'required');

      $this->form_validation->set_rules('add[created]', 'Created', 'required');
      $this->form_validation->set_rules('add[description]', 'Description', 'required');

      if($this->form_validation->run())
      {
   $path = './files/download/' . $_POST['add']['type'];
   $types = 'zip';
   $name = $_POST['add']['name'] . '_' . $_POST['add']['created'] . '.zip';
   $field = 'upload_file';

   if($this->_upload_file($path, $types, $field, $name))
   {
       $this->load->view('alertify/success', array('message' => 'File Uploaded Successfully'));
   }//end if
   else
   {
       $this->load->view('alertify/error', array('message' => $error));
   }//end else
      }//end if


//upload_file function
public function _upload_file($path, $types, $field, $name = null)
    {
$config['upload_path'] = $path;
$config['allowed_types'] = $types;
if($name !== null)
{
     $config['file_name'] = $name;
}//end if

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

if($this->upload->do_upload($field))
{
     return true;
}//end if
$error = $this->upload->display_errors();
return $error;
    }//end upload_file

Upload View
Code:
<?php defined('BASEPATH') or exit; ?>
<div class="row">
    <div class="main_content">
&lt;?php print form_open_multipart('Admin/add_content/portfolio'); ?&gt;
     <fieldset>
  <legend>Add to your portfolio</legend>
      &lt;?php
   if(validation_errors('', ''))
   { ?&gt;
       <div class="form_errors">
       &lt;?php print validation_errors('<div class="errors">', '</div>'); ?&gt;
       </div>

   &lt;?php }//end if
      ?&gt;

  <div class="row">
      <div class="small-7 columns">
   <label for="add[name]">Name</label>
   &lt;input type="text" name="add[name]" placeholder="Name" /&gt;
      </div>
  </div>

  <div class="row">
      <div class="small-7 columns">
   <label for="add[type]">Type</label>
   <select name="add[type]">
       <option value="web">Web</option>
       <option value="java">Java</option>
       <option value="c++">C++</option>
   </select>
      </div>
  </div>

  <div class="row">
      <div class="small-7 columns">
   <label for="add[created]">Created On</label>
   &lt;input type="text" name="add[created]" id="datepicker" /&gt;
      </div>
  </div>

  <div class="row">
      <div class="small-7 columns">
   <label for="upload_file">Add a file</label>
   &lt;input type="file" name="upload_file" /&gt;
      </div>
  </div>

  <div class="row">
      <div class="small-7 columns">
   <label for="upload_picture">Add a Screenshot</label>
   &lt;input type="file" name="upload_picture" /&gt;
      </div>
  </div>

  <div class="row">
      <div class="small-7 columns">
   <div class="textarea">
       <label for="add[description]">Description</label>

       &lt;textarea rows="50" cols="50" name="add[description]" placeholder="What does it do?"&gt;&lt;/textarea>
   </div>
      </div>
  </div>

  <div class="row">
   <div class="small-4 columns">
       &lt;input type="submit" name="article[submit]" value="Add" class="button" /&gt;
   </div>
      </div>
     </fieldset>
&lt;?php print form_close(); ?&gt;
    </div>
</div>

Sorry if this is in the wrong section or if i've left something out. i've been trying to find a way to solve this issue for a while. but nothing seems to be working. Also thanks in advance to any help received. it's greatly appreciated
#2

[eluser]CroNiX[/eluser]
I'm not sure if you can easily do that using CI's upload helper, but it would be simple to use php's rename() after it's uploaded to just "move" it to the other folder.
Like assuming you are uploading an image and a zip, but they both get saved to your images directory...
rename("/file_path/images/file.zip", "/file_path/zip/file.zip");
#3

[eluser]Unknown[/eluser]
The fact that I actually never thought of doing this shows how dependent on CI i've become. lmao, i think I should take a break and practice the basics again.

Thanks CroNiX, the uploader works perfectly now.




Theme © iAndrew 2016 - Forum software by © MyBB