使用ajaxfileupload.js上传文件(php)

html部分:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
  <head>
    <meta charset="utf-8">
    <title>Fine Uploader Demo</title>    
<script type="text/javascript" src="/test/jquery.js"></script> 
<script type="text/javascript" src="/test/ajaxfileupload.js"></script>
  </head>  
<body>
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<input type="text" id="fileurl" style="width:100px;">
<br />
<input type="button" value="upload" οnclick="upload()" />
</body>
<script type="text/javascript">
function upload()
{
$.ajaxFileUpload(
{
  url:'upload_file.php',
  secureuri:false,
  fileElementId:'file',
  dataType: 'text',
  success: function(data){
fileurl = document.getElementById("fileurl");
alert(data);
  }
  }
);
}
</script>
</html>


upload_file.php代码: 主要是保存文件至本地,并返回保存的文件的url

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 1000000)){//100KB
    $extend = explode(".",$_FILES["file"]["name"]);
    $key = count($extend)-1;
    $ext = ".".$extend[$key];
    $newfile = time().$ext;
$fileurl = "upload/" . $newfile;
    if(!file_exists('upload')){mkdir('upload');}
    $ok = move_uploaded_file($_FILES["file"]["tmp_name"], $fileurl);
if($ok === FALSE){ 
echo "上传失败"; 
}else{ 
$PHP_SELF=$_SERVER['PHP_SELF'];
$url='http://'.$_SERVER['HTTP_HOST'] . substr($PHP_SELF,0,strrpos($PHP_SELF,'/')+1);
echo $url . $fileurl;
     } 
}else {
    echo "文件超过大小";
}
?>


例子下载:http://pan.baidu.com/share/link?shareid=963166408&uk=942345598

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值