multiple Image upload – uploadify

<?php

if (!empty($_FILES)) {
//make sure the files really are just image files
$ext = strtolower(end(explode(“.”,$_FILES[‘Filedata’][‘name’]))); //get extension
$allowed = array(“jpg”,”png”,”gif”);
if (!in_array($ext,$allowed)) {
die;
}
//

//get file information
$tempFile = $_FILES[‘Filedata’][‘tmp_name’];
$targetPath = $_SERVER[‘DOCUMENT_ROOT’] . $_REQUEST[‘folder’] . ‘/’;
$targetFile =  str_replace(‘//’,’/’,$targetPath) . $_FILES[‘Filedata’][‘name’];
//

//upload the file
move_uploaded_file($tempFile,$targetFile);
//

//insert the file into the database
mysql_query(“INSERT INTO images (image_code, image_file) VALUES

(
‘”.$_GET[id].”‘,
‘”.addslashes($_FILES[‘Filedata’][‘name’]).”‘
)

“);
//

//echo the file name
echo str_replace($_SERVER[‘DOCUMENT_ROOT’],”,$targetFile);
}

?>

—————-

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Add Entry</title>

<link href=”style.css” rel=”stylesheet” type=”text/css” />

<!–include uploadify files–>
<link href=”uploadify/uploadify.css” type=”text/css” rel=”stylesheet” />
http://uploadify/jquery-1.4.2.min.js
http://jquery.validate.js
http://uploadify/swfobject.js
http://uploadify/jquery.uploadify.v2.1.4.js
<!–end include uploadify files–>

$(document).ready(function() {

//uploadify
$(‘#file_upload’).uploadify({
‘uploader’  : ‘uploadify/uploadify.swf’,
//’script’    : ‘upload_images.php?id=’ + $(“#eid”).val(), //script to upload our images
//’script’    : ‘index.php?id=’ + $(“#eid”).val(), //script to upload our images
‘script’    : ‘index.php’, //script to upload our images
‘cancelImg’ : ‘uploadify/cancel.png’,
‘buttonImg’ : ‘uploadify/upload.png’,
‘folder’    : ‘images’,
‘queueSizeLimit’ : 7,
‘fileExt’     : ‘*.jpg;*.gif;*.png’, //only upload these file types
‘fileDesc’    : ‘Image Files’,
‘multi’    : true,
‘auto’      : false,
‘sizeLimit’   : 5102400,

//update hidden field to indicate a file has been selected
‘onSelect’: function(event,ID,fileObj) {
$(“#selected”).val(‘Yes’);
},
//

//submit form ONLY after all files have been uploaded.
‘onAllComplete’ : function(event,data) {
$(“.submit”).focus().click();
}
//

});
/////

//function to use when submitting the form
function validate_form() {
$(“#form1”).validate({
submitHandler: function() {

//if a file has been selected
//if ($(“#selected”).val() == ‘Yes’) {
$(“#file_upload”).uploadifyUpload();
//}

//if a file hasn’t been selected
/*if ($(“#selected”).val() == ‘No’) {
$(form).submit();
}*/
//
}

});
}
//

//in case user tries submitting the form by hitting enter
$(‘input’).keypress(function(e) {
if(e.which == 13) {
$(this).blur();
validate_form();
}
});
//

//validate, then submit the form
$(“.submit”).click(function() {
validate_form();
});

});

</head>

<body>

<!–form–>

Add Entry

 

” />


Back to Tutorial

<?php //} ?>
<!–end form–>

</body>
</html>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s