current position:Home>How to upload pictures without refresh with HTML5 + PHP
How to upload pictures without refresh with HTML5 + PHP
2022-04-29 17:25:52【Billion cloud speed】
html5+PHP How to upload pictures without refresh
This article mainly introduces html5+PHP How to achieve no refresh picture upload related knowledge , The content is detailed and easy to understand , The operation is simple and fast , It has certain reference value , I believe that after reading this article html5+PHP How to achieve no refresh image upload articles will be fruitful , Let's have a look .
front end html Code upload,html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title> Upload </title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<style>
.upload{
width:300px;
height:200px;
position:relative;
}
.upload input[type='file']{
position:absolute;
width:70px;
top:0;
left:0;
z-index:10;
opacity:0;
filter:alpha(opacity=0);
}
.upload input.selbutton{
width:70px;
height:30px;
background:#cf001b;
color:#FFF;
font-size:14px;
position:absolute;
top:0;left:0;
z-index:9;
border:none;
cursor:pointer;
}
.upload input.upbutton{
width:70px;
height:30px;
background:#cf001b;
color:#FFF;
font-size:14px;
position:absolute;
top:50px;left:0;
z-index:10;
border:none;
cursor:pointer;
}
</style>
</head>
<body>
<div class='upload'>
<input type="file" name="file" />
<input type="button" name="selbutton" class="selbutton" value=" Select File " />
<input type="button" name="upbutton" class="upbutton" value=" Upload " />
</div>
<div class='previews'>
<img src="#" class="image_thumb" alt=" Preview picture "/>
</div>
</body>
</html>
The pattern is as follows
Next is js Code
<script type="text/javascript">
$(".upbutton").click(function(){
// Define the image format allowed to upload You can judge directly at the front desk , Illegal formats will not be uploaded
var filetype = ['jpg','jpeg','png','gif'];
if($('.image').get(0).files){
fi = $('.image').get(0).files[0]; // Get file information
// Determine whether the file format is a picture If it is not a picture, return false
var fname = fi.name.split('.');
if(filetype.indexOf(fname[1].toLowerCase()) == -1){
alert(' File format does not support ');
return ;
}
// Instantiation h5 Of fileReader
var fr = new FileReader();
fr.readAsDataURL(fi); // With base64 Read picture file in encoding format
fr.onload = function(frev){
pic = frev.target.result; // Get the result data
// Before you start uploading , The preview image
$('.image_thumb').attr('src',pic);
// Use ajax utilize post How to submit data
$.post(
'handle.php',
{
message:pic,
filename:fname[0],
filetype:fname[1],
filesize:fi.size
},
function(data){
data = eval('('+data+')');
if(data.code == 1 || data.code == 2){
console.log(' Upload failed ')
}else if(data.code == 0){
console.log(' Upload successful ')
}
}
);
}
}
})
</script>
Next is PHP Processing code handle.php
$imgtype = array(
'gif'=>'gif',
'png'=>'png',
'jpg'=>'jpeg',
'jpeg'=>'jpeg'
); // In the process of transmitting information corresponding to the type of picture
$message = $_POST['message']; // Receive to base64 Encoded image data
$filename = $_POST['filename']; // Receiving file name
$ftype = $_POST['filetype']; // Receiving file type
// First, remove the header information , Then decode the rest base64 Encoded data
$message = base64_decode(substr($message,strlen('data:image/'.$imgtype[strtolower($ftype)].';base64,')));
$filename = $filename.".".$ftype;
$furl = "D:/now/";
// Start writing files
$file = fopen($furl.$filename,"w");
if(fwrite($file,$message) === false){
echo json_encode(array('code'=>1,'con'=>'failed'));
exit;
}
echo json_encode(array('code'=>0,'con'=>$filename));
Select a file and click upload. The effect is shown in the figure below
About “html5+PHP How to upload pictures without refresh ” That's all for this article , Thank you for reading ! I'm sure you're right “html5+PHP How to upload pictures without refresh ” Knowledge has a certain understanding , If you want to learn more , Welcome to the Yisu cloud industry information channel .
copyright notice
author[Billion cloud speed],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291539467726.html
The sidebar is recommended
- 07 Chrome browser captures hover element style
- WebGIS development training (ArcGIS API for JavaScript)
- Solution to the blank page of the project created by create react app after packaging
- 19. Html2canvas implements ArcGIS API for JavaScript 4 X screenshot function
- Introduction to JavaScript API for ArcGIS 13
- Development of ArcGIS API for JavaScript under mainstream front-end framework
- Nginx learning notes
- Less learning notes tutorial
- Vue2 cannot get the value in props in the data of the child component, or it is always the default value (the value of the parent component comes from asynchrony)
- LeetCode 217. Determine whether there are duplicate elements in the array
guess what you like
I'll write a website collection station myself. Do you think it's ok? [HTML + CSS + JS] Tan Zi
Front end browser debugging tips
Application of anti chattering and throttling in JavaScript
How to create JavaScript custom events
Several ways of hiding elements in CSS
node. Js-3 step out the use of a server and express package
CSS matrix function
Fastapi series - synchronous and asynchronous mutual conversion processing practice
How to extend the functionality of Axios without interceptors
Read pseudo classes and pseudo elements
Random recommended
- About node JS server related concepts
- Access control module (2)
- About virtual lists
- Developing figma plug-in using Vue 3 + vite
- Learn more about the garbage collection engine of chrome V8 JavaScript engine
- Vue3 uses vite instead of webpack
- How to upload applet code through node? Just take a look
- Using H5 video tag in Vue to play local video in pop-up window
- What is the difference between classes in Es5 and ES6?
- [Vue] play with the slot
- [Part 4 of front-end deployment] using docker to build cache and multi-stage construction to optimize single page applications
- Vue2 simple use of vant (based on Vue CLI)
- node. JS server
- React uses concurrent mode. When the rendering phase exceeds the time slice, high priority tasks jump the queue. How will the lanes on the fiber of the previous task be solved
- Vuecli2 multi page, how to remove HTML suffix
- Vue router dynamically modifies routing parameters
- How to use webpack or configure quasar after context isolation is turned on by electron?
- Vue3 how do parent components call child component methods
- Es learning notes (I): http request
- 【Java WEB】AJAX
- Java project: nursing home management system (java + springboot + thymeleaf + HTML + JS + MySQL)
- Java project: drug management system (java + springboot + HTML + layui + bootstrap + seals + MySQL)
- Java project: agricultural material management system (java + springboot + easyUI + HTML + Maven + MySQL)
- How do Vue, native JS and jQuery feel about development
- The Ajax backend accepts post data and writes it directly to the database
- Java project: agricultural material management system (java + springboot + easyUI + HTML + Maven + MySQL)
- Brother Lao Yu takes you to play with esp32:14 and personally make a two-way wireless remote control (I)
- How to create JavaScript custom events
- A king's time, I learned nginx
- Vue quick start (with actual small items: Notepad, weather forecast, music player)
- Vue: convert user input to numeric type
- - Status code: 404 for http://mirrors.cloud.aliyuncs.com/centos/8/AppStream/x86_64/os/repodata/repom
- vue. config. Understanding of agent in JS
- After the node is successfully installed, CMD can be executed, but the compiler webstorm runs NPM install and prompts that there is no solution to this command
- How to develop and deploy front-end code in large companies
- Vue assigns permissions to buttons through instructions
- [development diary from 22 years to April] Vue problems encountered in actual projects and their solutions
- [methodology 1] CSS development skills - global style setting and local style
- vue3. 0 dynamically bind and obtain DOM through ref;
- How to use HTML for touch event in mobile terminal