Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include 'connect.php';
- if(!empty($_FILES['file'])) {
- $allowed = array('jpg', 'gif', 'png', 'jpeg');
- $count = 0;
- foreach($_FILES['file']['name'] as $key => $name) {
- $image_name = $name;
- $tmp = explode('.', $image_name);
- $image_extn = strtolower(end($tmp)); //can only reference file
- $image_temp = $_FILES['file']['tmp_name'][$count];
- $count = $count + 1;
- if(in_array($image_extn, $allowed) === true) {
- $image_path = 'images/'.substr(md5(uniqid($name, true)),0,10).$image_extn;
- move_uploaded_file($image_temp, $image_path);
- mysql_query("INSERT INTO store VALUES ('','$image_name','$image_path')") or
- die(mysql_error());
- $lastid = mysql_insert_id();
- $image_link = mysql_query("SELECT * FROM store WHERE id = $lastid");
- $image_link = mysql_fetch_assoc($image_link);
- $image_link = $image_link['image'];
- $uploaded[] = $image_link;
- }
- }
- }
- function showFileSize() {
- var input, file, size, total, fileName, ext;
- var val=true;
- var imageExts = ['png', 'jpg', 'gif', 'jpeg'];
- if (typeof window.FileReader !== 'function') {
- bodyAppend("p", "The file API isn't supported on this browser yet.");
- return;
- }
- input = document.getElementById('file');
- if (!input) {
- bodyAppend("p", "Um, couldn't find the fileinput element.");
- bodyAppend("br","");
- }
- else if (!input.files) {
- bodyAppend("p", "This browser doesn't seem to support the `files` property of file inputs.");
- bodyAppend("br","");
- }
- else if (!input.files[0]) {
- bodyAppend("err","Please select a file before clicking 'Load'");
- bodyAppend("br","");
- }
- else if(input.files.length>3){
- bodyAppend("err","You can upload a maximum of three files");
- bodyAppend("br","");
- }
- else{
- for(var i=0; i<input.files.length; ++i) {
- total+=input.files[i].size;
- }
- if(total>1024*1024*2) {
- bodyAppend("err","Total uploaded file sizes must not exceed 2MB");
- bodyAppend("br","");
- }
- else {
- for(var i=0; i<input.files.length; ++i) {
- file = input.files[i];
- size = file.size;
- if(size>1024*1024*1) {
- bodyAppend("err",file.name+ " is too big, can be a maximum of 1MB");
- bodyAppend("br","");
- }
- fileName = file.name;
- ext = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
- if (imageExts.indexOf(ext) === -1) {
- bodyAppend("err",file.name+"is an incorrect file type");
- bodyAppend("br","");
- val = false;
- }
- }
- if(val==false) {
- bodyAppend("err","Allowed file types are: jpg, png, jpeg and gif");
- bodyAppend("br","");
- }
- }
- }
- }
- function bodyAppend(tagName, innerHTML) {
- var elm;
- elm = document.createElement(tagName);
- elm.innerHTML = innerHTML;
- document.body.appendChild(elm);
- }
- <form action="index.php"onsubmit="return showFileSize();" method="post" enctype="multipart/form-data">
- <div>
- <input type="file" id="file" name="file[]" multiple="multiple" />
- <input type="submit" id="submit" value="Upload" onclick='showFileSize();'/>
- </div>
- </form>
Add Comment
Please, Sign In to add comment