Tutorial Extjs: Upload File and Image
Today, we will learn about how to make upload form using Extjs and php such as image (photo) upload and text file (article) upload. My opinion, upload form is very important on website, for example when saving the data to database such as guestbook and submitting article. I think this tutorial have been created by another person, but here i will try to explain it by my self, you can also look this tutorial on extjs.com. i am using combination of extjs and php.
This is the screen shot of this tutorial.

Steps of this tutorial are as folow
First step is create table in database, name of table of this tutorial is uploadf
CREATE TABLE IF NOT EXISTS `uploadf` ( `id` int(11) NOT NULL auto_increment, `namalaporan` varchar(100) NOT NULL, `namaFile` varchar(100) NOT NULL, `photo` varchar(100) NOT NULL, `date` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM ;
Second step is Create a php file to connection to the database, name of database of this tutorial is defafe
$nameserver = "localhost";
$username = "root";
$password = "";
$dbname = "defafe";
mysql_connect($nameserver,$username,$password)or die('error'.mysql_error());
mysql_select_db($dbname);
?>
Third step is create html file to call or load javascript and css files
//file-upload.css and FileUploadField can be found here ext-2.2\examples\form
After that create a javascript file to make upload form, the fields of this form are Filename, Fileconten, photo, and date. The most important of this script is “xtype: ‘fileuploadfield’,”
Ext.onReady(function(){
Ext.QuickTips.init();
var fp = new Ext.FormPanel({
fileUpload: true,
width: 320,
frame: true,
//height:170,
autoheight:true,
bodyStyle: 'padding: 10px 10px 0 10px;',
labelWidth: 60,
defaults: {
anchor: '95%',
//allowBlank: false,
msgTarget: 'side'
},
items: [
{
xtype: 'textfield',
name: 'Filename',
fieldLabel: 'File Name '
},{
xtype: 'fileuploadfield',
id: 'form-file',
emptyText: 'Select an file',
fieldLabel: 'File',
name: 'Fileconten',
buttonCfg: {
text: '',
iconCls: 'upload-icon'
}
},
{
xtype: 'fileuploadfield',
id: 'form-file1',
emptyText: 'Select an image',
fieldLabel: 'Image',
name: 'photo',
buttonCfg: {
text: '',
iconCls: 'upload-icon' //usuing icon on browse button.
}
},
{
xtype: 'datefield',
fieldLabel: 'Date',
anchor: '95%',
name: 'date',
format: 'm/d/Y'
}
],
buttons: [{
text: 'Save',
handler: function(){
if(fp.getForm().isValid()){
fp.getForm().submit({
url: 'phpupload.php',
waitMsg: 'Uploading your photo...',
success: function(fp, o){ //if success
Ext.MessageBox.alert('Creation OK','uplaoding data success..');
},
failure: function(fp, o){ //if not success
Ext.MessageBox.alert('Warning','failure uploading data...');
}
});
}
}
},{
text: 'Reset',
handler: function(){
fp.getForm().reset();
}
}]
});
var createwindow = new Ext.Window({
title:'Form Upload',
width:315,
height:190,
closable: false,
items: fp
});
createwindow.show();
});
Finally create php file to process data, to post to the databse, the image types of this tutorial are gif, jpeg, x-png.
include "koneksi.php";
$Filename = $_POST['Filename'];
$Fileconten= $_FILES['Fileconten']['name'];
$Photoname = $_FILES['photo']['name'];
$date = $_POST['date'];
if ($_FILES['photo']['type'] == "image/gif" || $_FILES['photo']['type'] == "image/jpeg" || $_FILES['photo']['type'] == "image/x-png")
{
move_uploaded_file ($_FILES['photo']['tmp_name'],"image/".$_FILES['photo']['name']);
move_uploaded_file ($_FILES['Fileconten']['tmp_name'],"artikel/".$_FILES['Fileconten']['name']);
$sql_query= mysql_query("INSERT INTO uploadF (`id`,`Filename`,`Fileconten`,`photo`,`date`) VALUES (null,'$Filename','$Fileconten','$Photoname',now())");
if ($sql_query)
{
echo '{"success":"true"}';
}
else
{
echo '{"success":"false"}';
}
}
?>
Tags: Anchor, Auto Increment, Css Files, Database Name, extjs, Image File, Image Photo, Image Upload, JavaScript, Nameserver, Photo File, Photo Upload, PHP, Php File, Php Image, Php Upload, Programming, Root Password, True Height, True Width, upload file, upload image, Upload Php, Uploading and downloading, Varchar
Thank you for sharing this nice example. But there seems to be a problem in IE (8). When I select a file, default text in input box “Select a file” still remains there. And After clicking “Save” I get javascript error, saying “Object doesn’t support this property or method”.
hi,
how do i resolve this?
Thanks for the post. I tried out your exmaple.. but i have some trouble! i can see two browse buttons and text fields. one is rendered using ExtJs CSS and other is plain HTML’s browse control!
Thanks in advance,
Abdel Olakara
@Abdel
I was stuck with the same problem. Importing file-upload.css from the examples/form folder solves the problem.
@Abdel
I was stuck with the same problem. Importing the file file-upload.css from the examples/form folder solves the problem.
mas.. kok saia coba di server gagal yah
.. klo do lokal malah berhasil
Hi, I’ve tried your example out, I’ve only 1 upload field more. I cann upload 2 different files or every file only then it works, but when I upload all 3 files it dosen’t upload the files anymore. I don’t have any error. Later I’ve tried this with 4 uploadfiles and the same is happen, when I upload 3 files it works and all files alone it works too but when I would upload all 4 files it dosn’t work?
Do you have any advise? Do you know anything about that?
Best regards
Valentina
Hello.
When I ran this after I downloaded, $_POST and $_FILES didn’t contain any value of ‘Filename’, ‘photo’, ‘Fileconten’, ‘date’ in phpupload.php. Do I need to configure in php first?
masak sich??????
“hanks for the post. I tried out your exmaple.. but i have some trouble! i can see two browse buttons and text fields. one is rendered using ExtJs CSS and other is plain HTML’s browse control! how do i resolve this?”
You must include the css file.
@Jonathan Lee
cek name field must be match with insert statement in php code…
Can you provide a demo of your upload image
Hi,
Just want to ask how can you specify the file types (mime types) to upload in the Fileupload component.
Thanks.
hi,
i’ve embedded the script in my application and it works great
but my problem is, if i do not add any attachments i get the following error
XML tag name mismatch (expected img)
has someone the same prob ?
Pay attention when checking “file type”, because IE send image/pjpeg for progressive jpeg. So this mime type has to be added.