原因是我单独整了个子域名来托管文件提供下载之类的, 然后传文件要上FTP特别麻烦, 于是自己写了个简单的小程序.
实现的功能:
- 输入正确的用户名和密码后进入上传页面
- 无刷新上传
- 上传的文件保存到文件夹
包括三个文件:
- index.html
- check_usr.php
- upload_file.php
index.html
<html> <body> <form action="/upload/check_usr.php" method="post"> Usr: <input type="text" name="usr" size="40" maxlength="80" value="" /> Passwd: <input type="text" name="passwd" size="40" maxlength="80" value="" /> <input type="submit" name="submit" value="SUBMIT"/> </form> </body> </html>
check_usr.php
<?php session_start(); $usr = $_POST['usr']; $passwd = $_POST['passwd']; $_SESSION['usr'] = "yourUsrName";//your name here $_SESSION['passwd'] = "yourPasswd";//your password here if ($passwd != $_SESSION['passwd'] && $usr != $_SESSION['usr']){echo "ERROR";} elseif ($passwd == $_SESSION['passwd'] && $usr == $_SESSION['usr']){ ?> <!--if usrname & passwd are correct--> <form action="upload_file.php" method="post" target="post_frame" enctype="multipart/form-data" id="editor-form" name="form1"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <input type="hidden" name="filepath" /> <input type="submit" name="submit" value="Submit" /> </form> <div id="ooo"> </div> <!--jq ajax upload, require jquery.form.js--> <script type="text/javascript" src="/jquery.min.js"></script> <script src="/jquery.form.js"></script> <script type="text/javascript"> $(document).ready(function() { $('#editor-form').submit(function() { var options = {target: '#ooo',url: 'upload_file.php',type: 'post',success: function() {$('#filepath').val($('#ooo').text());}}; $(this).ajaxSubmit(options); return false; }); }); </script> <?php }?>
upload_file.php
<?php $dir = 'files/'; if (! file_exists ( $dir )) { mkdir ( $dir );//create a folder if not exists } if (( ($_FILES["file"]["type"] == "application/octet-stream")//.rar/.exe/.7z etc. || ($_FILES["file"]["type"] == "application/x-zip-compressed")//.zip || ($_FILES["file"]["type"] == "application/javascript"))//.js && ($_FILES["file"]["size"] < 200000) ) { if ($_FILES["file"]["error"] > 0) {echo "Return Code: " . $_FILES["file"]["error"] ; } else { if (file_exists("files/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file( $_FILES["file"]["tmp_name"], "files/" . $_FILES["file"]["name"] );// save file } } } else {echo "Invalid file";} ?>
php_upload_system is coded by http://xiaohudie.net. Any posts here is one hundred percent original, so please keep my link so as not to hurt this pretty girl. -
来学习了 不错 支持 你的回复框是最麻烦的
看起来十分厉害的样子
你那个lib就是这么写的么?我也想搞一个lib 不知道麻烦不???
@rainzee 不算麻烦,就拿这个上传,然后写几个静态页面行了,如果要功能多点就麻烦
我觉得这玩意挺好的。有一个不限容量的超大附件机,一直在想怎么利用起来,可惜啊可惜啊,附件机不支持php,你这个perfect代码我还是用不上
主题整体很好看,有点像国外的某些轻博客程序自带的 。。做的好官方 。。
然后就是这个评论框为啥要弄成这样,看起来有点特殊 。。
@babytomas 仿github…..
好简单…