Multiple Upload isnt working - El Forum - 05-13-2010
[eluser]flyenig[/eluser]
How can i make this code upload multiple photos? I atempted to try this but It did not work at all so I posted my orignial Code if you are wondering if i even tried to do this myself or not, can someone help me out?
Thank you in advance. Here is the code
The CONTROLLER
Code: function upload()
{
$this->data['error'] = array('error' => ' ' );
$this->load->view('bf2/upload',$this->data);
}
function upload_pic()
{
$this->load->model('photo_model');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000000';
$config['max_width'] = '10244';
$config['max_height'] = '7688';
//$config['file_name'] = $this->photo_model->newPhoto($stupdata['file_ext'])."_".$this->session->userdata('user_id');
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
//gather the data
$photodata = array('upload_data' => $this->upload->data());
//the original image file
$imgfile = $photodata['upload_data']['full_path'];
//Insert the photo_id into database
$this->photo_model->newPhoto($photodata['upload_data']['file_ext']);
//create the var to rename the photo
$new_name_img = $config['upload_path'].$this->db->insert_id()."_".$this->session->userdata('user_id').'.jpg';
//finally rename the photo
rename($photodata['upload_data']['full_path'],$new_name_img);
//create the thumbnail
$config['image_library'] = 'GD2';
$config['source_image'] = $new_name_img;
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = '_th';
$config['maintain_ratio'] = TRUE;
$config['width'] = 130;
$config['height'] = 130;
$config['master_dim'] = 'width'; // this sets the resizer to default to height when preserving the ratio
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
//User can add caption and select the album the photo will be in the user Details and Stuff
redirect('photo/editPhoto/'.$this->db->insert_id().'/');
}
}
The MODEL
Code: function newPhoto($stupdata)
{
$newPhotoSQL = 'INSERT INTO photos
(user_id)
VALUES
(
' . $this->db->escape($this->session->userdata('user_id')) . '
)';
$this->db->query($newPhotoSQL);
if ($this->db->affected_rows() == 1)
{
return $this->db->insert_id();
}
}
and the VIEW
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('photo/upload_pic');?>
<input type="file" name="userfile[]" size="20" />
<input type="file" name="userfile[]" size="20" />
<input type="file" name="userfile[]" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
Thank you
Multiple Upload isnt working - El Forum - 05-13-2010
[eluser]Fabdrol[/eluser]
Well, first of all.. your 'userfile' POST data is an array, so your code should run it through some foreach or for loop somewhere.
Something like "foreach($this->input->post('userfile') as $userfile) { $this->upload_pic($userfile); }"
have fun!
Fabian
Multiple Upload isnt working - El Forum - 05-13-2010
[eluser]flyenig[/eluser]
ok that didnt work and i tried this and it didnt work either:
Code: function upload_pic()
{
$this->load->model('photo_model');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000000';
$config['max_width'] = '10244';
$config['max_height'] = '7688';
//$config['file_name'] = $this->photo_model->newPhoto($stupdata['file_ext'])."_".$this->session->userdata('user_id');
$this->load->library('upload', $config);
$arr = $_FILES['userfile']['tmp_name'];
foreach($arr as $key)
{
if (!$this->upload->do_upload($key))
{
echo $error = $this->upload->display_errors();
//$this->load->view('upload_form', $error);
}
else
{
//$photo_data = $this->upload->data('userfile');
//$dataP = array('photo_ext' => $photo_data['file_ext']);
//gather the data
$photodata = array('upload_data' => $this->upload->data());
//the original image file
$imgfile = $photodata['upload_data']['full_path'];
//Insert the photo_id into database
$this->photo_model->newPhoto($photodata['upload_data']['file_ext']);
//create the var to rename the photo
$new_name_img = $config['upload_path'].$this->db->insert_id()."_".$this->session->userdata('user_id').'.jpg';
//finally rename the photo
rename($photodata['upload_data']['full_path'],$new_name_img);
//create the thumbnail
$config['image_library'] = 'GD2';
$config['source_image'] = $new_name_img;
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = '_th';
$config['maintain_ratio'] = TRUE;
$config['width'] = 130;
$config['height'] = 130;
$config['master_dim'] = 'width'; // this sets the resizer to default to height when preserving the ratio
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
/*if($this->input->post('upldbutton'))
{
$this->photo_model->create_thumbs($this->db->insert_id());
$this->session->set_userdata(array('user_status' => 'uc'));
//$this->db->update('users', array('user_status' => 'uc'));
$userId = $this->session->userdata('user_id');
$data = array(
'profile_img' => $this->db->insert_id()."_".$userId
);
$this->db->where('id',$userId);
$this->db->update('users',$data);
redirect('home');
}
else{
redirect('photo/editPhoto/'.$this->db->insert_id().'/');
}*/
}
}
}
Multiple Upload isnt working - El Forum - 05-13-2010
[eluser]Fabdrol[/eluser]
You're making one big mistake. See this line:
Code: $arr = $_FILES['userfile']['tmp_name'];
PHP can't find the key 'tmp_name' on 'userfile', since userfile is an array. Userfile looks something like this:
Code: $_FILES['userfile'] = array(
[0] => array(
[tmp_name] => "blah blah",
[...] => "etc"
),
[1] => array( ... )
)
see where you're going wrong? If you want to test the contents of $_FILES, try to 'print_r' it, like this:
Code: public function upload_pic() {
echo "<pre>";
print_r($_FILES['userfile']);
echo "</pre>";
}
that way you can see how userfile is layed out. If you post the printout of that code here, I can help you along with some code examples.
Multiple Upload isnt working - El Forum - 05-13-2010
[eluser]flyenig[/eluser]
Yeah i tried $_FILES['userfile']['name']; before and it didnt work so i put "tmp_name" to see if it worked but it didnt lol. But yea i see what you are saying and this is what came out from the "print_r" function:
Code: Array
(
[name] => Array
(
[0] => 2307663194_2cded6874e.jpg
[1] => 250px-Jesse_Owens1.jpg
[2] => 41E3AW1D0NL._SS400_.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
[2] => image/jpeg
)
[tmp_name] => Array
(
[0] => C:\xampp\tmp\php1C8.tmp
[1] => C:\xampp\tmp\php1C9.tmp
[2] => C:\xampp\tmp\php1CE.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 87781
[1] => 36248
[2] => 34832
)
)
Multiple Upload isnt working - El Forum - 05-13-2010
[eluser]Fabdrol[/eluser]
Yeah, thats it.. Listen, as you can see this array is a bit strangely formatted, so I'd rebuild that first if I were you. Example;
Code: $file_array = $_FILES['userfile'];
$total_files = count($file_array['name']);
// now we know the total number of files to upload.
// let's built a fresh upload array...
$files = array();
for($i = 0; $i < $total_files; $i += 1) {
$files[] = array(
'name' => $file_array['name'][$i],
'tmp_name' => $file_array['tmp_name'][$i],
'size' => $file_array['size'][$i],
'error' => $file_array['error'][$i]
);
}
now you have a nicely formatted array you can loop through. Think you can take it from here?
Have fun!
Multiple Upload isnt working - El Forum - 05-13-2010
[eluser]flyenig[/eluser]
thank you for that, but im still having trouble looping through all the pictures and excuting the code. This is what ive done so far and it gives me multiple errors saying "you did not select a file to upload"
Code: function upload_pic()
{
$this->load->model('photo_model');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000000';
$config['max_width'] = '10244';
$config['max_height'] = '7688';
$this->load->library('upload', $config);
$file_array = $_FILES['userfile'];
$total_files = count($file_array['name']);
// now we know the total number of files to upload.
// let's built a fresh upload array...
$files = array();
for($i = 0; $i < $total_files; $i += 1) {
$files[] = array(
'name' => $file_array['name'][$i],
'tmp_name' => $file_array['tmp_name'][$i],
'size' => $file_array['size'][$i],
'error' => $file_array['error'][$i]
);
}
foreach($files as $file)
{
if (!$this->upload->do_upload($file['name']))
{
echo $error = $this->upload->display_errors();
//$this->load->view('upload_form', $error);
}
else
{
//gather the data
$photodata = array('upload_data' => $this->upload->data());
//the original image file
$imgfile = $photodata['upload_data']['full_path'];
//Insert the photo_id into database
$this->photo_model->newPhoto($photodata['upload_data']['file_ext']);
//create the var to rename the photo
$new_name_img = $config['upload_path'].$this->db->insert_id()."_".$this->session->userdata('user_id').'.jpg';
//finally rename the photo
rename($photodata['upload_data']['full_path'],$new_name_img);
//create the thumbnail
$config['image_library'] = 'GD2';
$config['source_image'] = $new_name_img;
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = '_th';
$config['maintain_ratio'] = TRUE;
$config['width'] = 130;
$config['height'] = 130;
$config['master_dim'] = 'width'; // this sets the resizer to default to height when preserving the ratio
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
}
}
Multiple Upload isnt working - El Forum - 05-13-2010
[eluser]asmedrano[/eluser]
I recently had to do something like that and I found this
http://ellislab.com/forums/viewthread/80610/
extremely helpful
Multiple Upload isnt working - El Forum - 05-14-2010
[eluser]Fabdrol[/eluser]
Hmg I don't know if you can pass the variable right into 'do_upload()'. Why don't you write a php native upload function? Personally, I find that the CI upload class doesn't really add anything to it beyond restricting your coding ;-)
Multiple Upload isnt working - El Forum - 05-14-2010
[eluser]flyenig[/eluser]
ok ill try both of you guys methods and ill post back after.
|