Home  >  Article  >  Backend Development  >  PHP+JavaScript realizes uploading images without refreshing

PHP+JavaScript realizes uploading images without refreshing

高洛峰
高洛峰Original
2017-02-24 17:39:461413browse

html file code


##

<!-- ajax文件上传开始 -->
<script type="text/javascript" src="/imageupload/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="/imageupload/layer/layer.js"></script>
<script type="text/javascript" src="/imageupload/ajaxupload.js"></script>
<!--ajax文件上传结束-->
<!--上传文件按钮列表开始-->
<input id="requesturl" type="hidden" value="{:U(&#39;admin/upload/uploadfile&#39;)}" />
<input id="ajaxuploadfile" type="file" onchange="filechange()"/>
<input id="filepathurl" type="hidden" value="" />
<input type="button" value="第一张" pathurl="./Uploads/admin/trailer/" class="uploadclass" />
<input type="button" value="第二张" pathurl="./Uploads/admin/fdfdfd/" class="uploadclass" />
<input type="button" value="第三张" pathurl="./Uploads/admin/cdcdfd/" class="uploadclass" />
<!--上传文件按钮列表结束-->


php file code


/**
* 文件上传方法
*/
public function uploadfile(){
//单文件上传
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 100000000 ;// 设置附件上传大小
$upload->exts = array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;);// 设置附件上传类型
$url = $_POST[&#39;filepathurl&#39;];
if (!file_exists($url)){
mkdir ($url,0777,true);
}
$upload->rootPath = $url; // 设置附件上传根目录
// 上传单个文件
$info = $upload->uploadOne($_FILES[&#39;postfilename&#39;]);
if(!$info) {
echo json_encode(array(&#39;bool&#39;=>false,&#39;error&#39;=>$upload->getError()));
}else{
$path = $info[&#39;savepath&#39;].$info[&#39;savename&#39;];
echo json_encode(array(&#39;bool&#39;=>true,&#39;path&#39;=>$path));
}
}

For more articles related to PHP+JavaScript to achieve refresh-free image uploading, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn