Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PHP
- <?php
- try{
- if(isset($_FILES['uploadfile'])){
- $total_files = count($_FILES['uploadfile']['name']);
- if( $total_files > 0){
- for($i=0; $i<$total_files; $i++) {
- $file_name = $_FILES['uploadfile']['name'][$i];
- $file_size = $_FILES['uploadfile']['size'][$i];
- $file_tmp = $_FILES['uploadfile']['tmp_name'][$i];
- $file_type = $_FILES['uploadfile']['type'][$i];
- $upload_Path = "storage/".$file_name;
- //var_dump($file_size);
- //die;
- if($file_size > 8000000){
- echo json_encode( array('status' => 'failure' , 'msg' => 'Total upload size must be less than 8 MB.') );
- die();
- }
- if($file_tmp == ""){
- echo json_encode( array('status' => 'failure' , 'msg' => 'There is no filepath.') );
- die();
- }
- else{
- if(!file_exists($upload_Path)){
- move_uploaded_file($file_tmp, $upload_Path);
- }
- else{
- $name = pathinfo($file_name, PATHINFO_FILENAME);
- $ext = pathinfo($file_name, PATHINFO_EXTENSION);
- $new_name = $name.rand().'.'.$ext;
- $new_Path = "storage/".$new_name;
- move_uploaded_file($file_tmp, $new_Path);
- }
- }
- }
- }
- echo json_encode( array('status' => 'success' , 'msg' => 'File uploaded succesfully.') );
- die();
- }
- else{
- echo json_encode(array("status" => "error" , "msg" => "No file was found when processing uploaded files" ) );
- die();
- }
- }
- catch(Exception $ex){
- echo json_encode(array('status' => 'error' , 'msg' => 'An unhandled exception raised: ' . $ex->getMessage() ) );
- die();
- }
- ?>
- //HTML
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Upload Files</title>
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet">
- <link href='https://fonts.googleapis.com/css?family=Raleway:400italic|Courgette' rel='stylesheet' type='text/css'>
- <link href="styles.css" rel="stylesheet">
- <link href="reset.css" rel="stylesheet">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- $('#upload').click(function(){
- $('input[type=file]').click();
- return false;
- });
- $("#uploadfile").change(function(){
- //submit the form here
- var files = $("#fileupload")[0];
- var formdata = new FormData(files);
- $.ajax({
- type:'POST',
- url: 'mupld.php',
- data: formdata,
- processData:false,
- contentType:false,
- success: function(response){
- if(response.status == 'success'){
- alert('success: '+response.msg);
- }
- else{
- alert('Error: '+response.msg);
- }
- }/*,
- error: function(xhr, status, error){
- alert('Error: '+status+' '+error);
- }*/
- });
- });
- });
- </script>
- </head>
- <body>
- <div id="header">
- <a href="./uploader.php"><i class="fa fa-cloud-upload fa-align-center" aria-hidden="true"></i> MUploader</a>
- </div>
- <div id="content">
- <div id="heading">Upload your files seamlessly</div>
- <a href="#"><div id="upload" class="button" title="Upload your files"><i class="fa fa-cloud-upload fa-align-center" aria-hidden="true"></i>
- </div></a>
- <a href="view.php"><div id="view" class="button" title="View all files on my cloud"><i class="fa fa-eye fa-align-center" aria-hidden="true"></i>
- </div></a>
- </div>
- <form id="fileupload" method="POST" enctype="multipart/form-data">
- <input type="file" multiple name="uploadfile[]" id="uploadfile" />
- </form>
- <div id="footer">
- Made with <span class="fa fa-heart" aria-hidden="true"></span> for you
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment