Tuesday, 15 May 2012

file - PHP return uploaded filename for verification -



file - PHP return uploaded filename for verification -

i need point me direction following.

once upload file /uploads on server; php iteratively changes filename adding number if same file exists (basic override protection). after file uploaded, need php function can homecoming filename , (this hard part) page triggered php uploader script. capture filename variable , pass next page via cookie (this latter 2 things know how do).

code php script:

<?php $allowedexts = array("zip", "rar"/*, "bmp", "jpg", "png", "tiff"*/); $temp = explode(".", $_files["file"]["name"]); $name = pathinfo($_files["file"]["name"], pathinfo_filename); $extension = pathinfo($_files['file']['name'], pathinfo_extension); $i = ''; if ((($_files["file"]["type"] == "application/zip") || ($_files["file"]["type"] == "application/x-zip") || ($_files["file"]["type"] == "application/octet-stream") || ($_files["file"]["type"] == "application/x-zip-compressed") || ($_files["file"]["type"] == "application/x-rar-compressed") /*|| ($_files["file"]["type"] == "image/bmp") || ($_files["file"]["type"] == "image/jpg") || ($_files["file"]["type"] == "image/png") || ($_files["file"]["type"] == "image/tiff")*/) && ($_files["file"]["size"] < 50000000) && in_array($extension, $allowedexts)) { if ($_files["file"]["error"] > 0) { echo "return code: " . $_files["file"]["error"] . "<br>"; } else { if (file_exists("upload/" . $name . $i . '.' . $extension)) { while(file_exists("upload/" . $name . $i . '.' . $extension)) { $i++; } $basename = $name . $i . '.' . $extension; move_uploaded_file($_files["file"]["tmp_name"], "upload/" . $basename); } else { move_uploaded_file($_files["file"]["tmp_name"], "upload/" . $_files["file"]["name"]); } } } else { echo "invalid file"; } ?>

html5 upload:

class="snippet-code-html lang-html prettyprint-override"><!doctype html> <!-- saved url=(0044)http://html5demos.com/dnd-upload#view-source --> <html lang="en"><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <meta charset="utf-8"> <title>html5 demo: drag , drop, automatic upload</title> <body> <section id="wrapper"> <header> <h4>drag , drop</h4> </header> <style> #holder { border: 1px dashed lightgrey; width: 300px; min-height: 300px; margin: 20px auto;} #holder.hover { border: 1px dashed #ffcc00; } #holder img { display: block; margin: 10px auto; } #holder p { margin: 10px; font-size: 14px; } progress { width: 50%; } progress:after { content: ''; } .fail { background: #c00; padding: 2px; color: #fff; } .hidden { display: none !important;} </style> <article> <div id="holder"> </div> <p id="upload" class="hidden"><label>drag &amp; drop not supported, can still upload via input field:<br><input type="file"></label></p> <p id="filereader" class="hidden">file api &amp; filereader api not supported</p> <p id="formdata" class="hidden">xhr2's formdata not supported</p> <p id="progress" class="hidden">xhr2's upload progress isn't supported</p> <p>upload progress: <progress id="uploadprogress" min="0" max="100" value="0">0</progress></p> </article> <script> var holder = document.getelementbyid('holder'), tests = { filereader: typeof filereader != 'undefined', dnd: 'draggable' in document.createelement('span'), formdata: !!window.formdata, progress: "upload" in new xmlhttprequest }, back upwards = { filereader: document.getelementbyid('filereader'), formdata: document.getelementbyid('formdata'), progress: document.getelementbyid('progress') }, acceptedtypes = { //'image/bmp': true //'image/jpg': true, //'image/png': true, }, progress = document.getelementbyid('uploadprogress'), fileupload = document.getelementbyid('upload'); "filereader formdata progress".split(' ').foreach(function (api) { if (tests[api] === false) { support[api].classname = 'fail'; } else { // ffs. have done el.hidden = true, ie doesn't back upwards // hidden, tried create polyfill extend // element.prototype, ie10 doesn't give me access // element object. brilliant. support[api].classname = 'hidden'; } }); function previewfile(file) { /*if (tests.filereader === true && acceptedtypes[file.type] === true) { var reader = new filereader(); reader.onload = function (event) { var image = new image(); image.src = event.target.result; image.width = 250; // false resize holder.appendchild(image); }; reader.readasdataurl(file); } else {*/ holder.innerhtml += '<p>uploaded ' + file.name + ' ' + (file.size ? (file.size/1024|0) + 'k' : ''); console.log(file); /*}*/ } function readfiles(files) { //debugger; var formdata = tests.formdata ? new formdata() : null; (var = 0; < files.length; i++) { if (tests.formdata) formdata.append('file', files[i]); previewfile(files[i]); } // post new xhr request if (tests.formdata) { var xhr = new xmlhttprequest(); xhr.open('post', 'upload_artwork.php'); xhr.onload = function() { progress.value = progress.innerhtml = 100; }; if (tests.progress) { xhr.upload.onprogress = function (event) { if (event.lengthcomputable) { var finish = (event.loaded / event.total * 100 | 0); progress.value = progress.innerhtml = complete; } } } xhr.send(formdata); } } if (tests.dnd) { holder.ondragover = function () { this.classname = 'hover'; homecoming false; }; holder.ondragend = function () { this.classname = ''; homecoming false; }; holder.ondrop = function (e) { this.classname = ''; e.preventdefault(); readfiles(e.datatransfer.files); } } else { fileupload.classname = 'hidden'; fileupload.queryselector('input').onchange = function () { readfiles(this.files); }; } </script> </body></html>

submit form ajax form submit homecoming values of file path . hope help's you..

<?php if($_files["file"]["name"] != '') { $output_dir="uploads/"; if(move_uploaded_file($_files["file"]["tmp_name"],$output_dir.$_files["file"]["name"])) { chmod($output_dir.$_files["file"]["name"],0777); echo $_files["file"]["name"]; exit; } } ?> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> <script src="http://malsup.github.com/jquery.form.js"></script> <script> // wait dom loaded $(document).ready(function() { var jvar=""; // bind 'myform' , provide simple callback function $('#myform').ajaxform({ clearform: 'true', success: function(data){ if(data != '') { jvar=data; window.location.href="test.php?path="+jvar; //assign variable , pass file } } }); }); </script> </head> <form id="myform" action="test.php" method="post"> <input type="file" name="file"> <input type="submit" value="submit comment" /> </form>

php file upload return filenames

No comments:

Post a Comment