upload multiple gambar

Mely · Jul 11, 2011
halo, saya ingin upload multiple gambar dan diupdate ke database sekaligus,misal ada 2 gambar yg diupload:a.jpg,b.jpg,kemudian akan terupdate ke tabel menjadi:a.jpg;b.jpg. saya memakai codigniter verse 2.ini penggalan codenya
CONTROLLER
class Product extends CI_Controller{
$mixpic="";
for($k=1;$k<$rowc;$k++)
{
$file = $this->do_upload();//panggil function do_upload untuk upload gambar

if($file != ""){
foreach($file as $item){
$pic = $item;
}
}
else{
$pic = "";
}$mixpic=$mixpic.$pic.";";
}$this->Product_Model->updateMaster(array('code' => $code), $mixcolor);
}//Ud pake cara ini kok ga bisa y
VIEW
<script language="javascript">
function submitform()
{
var tableActive = document.getElementById("tblsize");
var row = parseInt(tableActive.rows.length);

var tableActivec = document.getElementById("tblcolor");
var rowc = parseInt(tableActivec.rows.length);

var tableActivep = document.getElementById("tblpic");
var rowp = parseInt(tableActivep.rows.length);
//document.frm.action = "product/insert/"+row+"/"+rowc;
document.frm.action = "product/insert/"+row+"/"+rowc+"/"+rowp;

document.frm.submit();
}
function addRowpic(){
var tableActive = document.getElementById("tblpic");
var rowp = parseInt(tableActive.rows.length);
if(rowp < 5){
var newTR = tableActive.insertRow(rowp);

var newTD = newTR.insertCell(0);
newTD.noWrap=1;
newTD.innerHTML = "<input type='file' size='20' name='userfile"+rowp+"'>&nbsp;&nbsp;&nbsp;";

}
else{
alert("Row can not be added");
}
}
function delRowpic(){
var tableActive = document.getElementById("tblpic");
var rowp = parseInt(tableActive.rows.length);
if(rowp > 2){
var newTR = tableActive.deleteRow(rowp-1);
}
else{
alert("Row can not be deleted");
}
}
</script>
<tr>
<td valign="top"><?=langName('eYJPicture')?></td>
<td valign="top">&nbsp;:&nbsp;</td>
<td><table name="tblpic" id="tblpic">
<? $rowp=1;?><tr></tr><tr>
<td>//upload gambar dengan multiple baris
<input type="file" name="userfile<?=$rowp?>" size="20" />
</td>
<td> //tombol untuk menambah baris upload
<img src="images/add.png" onmouseover="this.style.cursor='pointer'" onclick='addRowpic(<?=$rowp?>)' >
</td>
<td>//tombol untuk mengurangi baris upload
<img src='images/del.png' onmouseover='this.style.cursor=\"pointer\" 'onclick='delRowpic(<?=$rowp?>)'></td>
</tr>
</table></td>
</tr>

MODEL
class Product_Model extends CI_Model{
function updateMaster($where, $data)
{
$this->db->where($where);
$this->db->update($this->_table, $data);
return $this->db->affected_rows();
}
Silahkan login untuk menjawab!
0
Loading...
Ellyx Christian · Jul 11, 2011 · 0 Suka · 0 Tidak Suka
Note: Untuk menyisipkan koding/skrip gunakan cara yang ada di Cara menggunakan syntax highlighting.
Coba pake cara yang ada di http://codeigniter.com/forums/viewthread/91422/, jika tidak berhasil pake php murni saja seperti:
<?php
if($_POST){
	foreach($_FILES['file']['name'] as $i => $name){
		if($_FILES['file']['error'][$i] != 0){
			continue;
		}
		move_uploaded_file($_FILES['file']['tmp_name'][$i], 'path/to/upload/'.$name);
	}
}
?>
<form method="post" enctype="multipart/form-data" action="">
file1 <input type="file" name="file[]" id="file1"/></br/>
file2 <input type="file" name="file[]" id="file2"/><br/>
file3 <input type="file" name="file[]" id="file3"/><br/>
<input type="submit" name="upload" value="Upload"/>
</form> 
1
Loading...
Mely · Jul 12, 2011 · 1 Suka · 0 Tidak Suka
Ok, tx. Trus klo buat dapat size width n height dari image yg diupload gimana caranya?
foreach ($_FILES["pic"]["error"] as $key => $error)
{
$tmp_name = $_FILES["pic"]["tmp_name"][$key];
$name = $_FILES["pic"]["name"][$key];
$mix=$mix.$name.";";
$uploadfile = $uploaddir . basename($name);
$this->image = imagecreatefromstring($uploadfile);
$width = imagesx($this->image); ;
$height = imagesy($this->image);

}
Saya dapat error:
Message: imagecreatefromstring() [function.imagecreatefromstring]: Data is not in a recognized format
Message: imagesx() expects parameter 1 to be resource, boolean given
tx.
0
Loading...
Ellyx Christian · Jul 12, 2011 · 0 Suka · 0 Tidak Suka
Note: Untuk menyisipkan koding/skrip gunakan cara yang ada di Cara menggunakan syntax highlighting.

parameter function imagecreatefromstring bukanlah string nama file, tapi string yang berisi data gambar.
coba pake ini:
$tmpFile = fopen($tmp_name, 'rb');
$pictureData = fread($tmpFile, filesize($tmp_name));
fclose($tmpFile);
$this->image = imagecreatefromstring($pictureData);
0
Loading...
Mely · Jul 13, 2011 · 0 Suka · 0 Tidak Suka
waduh ud dicoba, malah banyak error:
<?php
$tmpFile = fopen($tmp_name, 'rb');

$pictureData = fread($tmpFile, filesize($tmp_name));

fclose($tmpFile);

$this->image = imagecreatefromstring($pictureData);
$width = imagesx($this->image);
$height = imagesy($this->image);
?>
Oya, 'rb' itu apa y??

Message: fopen(C:\xampp\tmp\php3F.tmp) [function.fopen]: failed to open stream: No such file or directory
Message: filesize() [function.filesize]: stat failed for C:\xampp\tmp\php3F.tmp
Message: fread() expects parameter 1 to be resource, boolean given
Message: fclose() expects parameter 1 to be resource, boolean given
Message: imagecreatefromstring() [function.imagecreatefromstring]: Empty string or invalid image
Message: imagesx() expects parameter 1 to be resource, boolean given
Message: imagesy() expects parameter 1 to be resource, boolean given
0
Loading...
Ellyx Christian · Jul 13, 2011 · 0 Suka · 0 Tidak Suka
coba kasi saya koding kamu yang lengkap dibagian upload gambar
1
Loading...
Mely · Jul 20, 2011 · 1 Suka · 0 Tidak Suka
sbb,akhirnya saya pake cara yang simple ini:
list($width,$height)=getimagesize($uploadfile);
berhasil
0
Loading...
Ellyx Christian · Jul 20, 2011 · 0 Suka · 0 Tidak Suka
bagus kalau begitu.