How to implement jQuery's Ajax file upload, PHP faithful file upload.
AJAX upload file, PHP upload file.
【PHP file upload】
Before we begin, I think it is necessary to briefly explain the principle of uploading files through the WEB.
Actually, whether it is PHP, JSP, or ASP that processes the uploaded files here, the WEB has already uploaded the files to the server. We just use the upload processing function to process the uploaded files.
The processing functions are generally implemented using server-side languages such as PHP, JSP, and ASP. So how to upload files through WEB (HTTP protocol?) You need HTML code similar to the following:
test.html
Note: enctype="multipart/form-data" is required. It tells the FORM table that this is a file upload type. Once the request is successful, the file will be uploaded to the temporary folder of the server.
As for how the file will be processed after arriving at the destination, it is a matter of PHP, JSP, and ASP.
(However, don’t be too happy too early. If the file is not moved to other places or renamed, the file will be deleted at the end of the form request. So we have to write a script to handle uploaded files)
Here we use PHP to handle it
do_file_upload.php
$error = "" ; //Upload file error message
$msg = "";
$fileElementName = 'picture';
$allowType = array(".jpg",".gif",".png"); //File types allowed to be uploaded
$num = strrpos($_FILES['picture']['name'],'.');
$fileSuffixName = substr($_FILES['picture'][' name'],$num,8);//This number is variable
$fileSuffixName = strtolower($fileSuffixName); //Determine the type of uploaded file
$upFilePath = 'd:/'; //Final storage path
if(!empty($_FILES[$fileElementName]['error']))
{
switch($_FILES[$fileElementName]['error'])
{
case '1':
$error = 'The transferred file exceeds the limit of the upload_max_filesize option in php.ini';
break;
case '2':
$error = 'The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form';
break;
case '3':
$error = 'Only part of the file was uploaded';
break;
case '4':
$error = 'No file uploaded';
break;
case '6':
$error = 'Temp folder not found';
break;
case '7':
$error = 'File writing failed';
break;
default:
$error = 'Unknown error';
}
}elseif(empty($_FILES['fileToUpload']['tmp_name']) || $_FILES[ 'fileToUpload']['tmp_name'] == 'none')
{
$error = 'No file uploaded.';
}else if(!in_array($fileSuffixName,$allowType))
{
$error = 'File type not allowed to upload';
}else{
);
if($ok === FALSE){
$error = 'Upload Failed';
}
}
?>
Another note: About the $_FILES array
This array contains all uploaded file information, which records the relevant information when uploading files.
The contents of the $_FILES array in the above example are as follows. Let's assume that the name of the file upload field is userfile as shown in the example above. The name can be whatever you want.
$_FILES['userfile']['name']
The original name of the client machine file.
$_FILES['userfile']['type']
The MIME type of the file, if the browser provides this information. An example is "image/gif". However, this MIME type is not checked on the PHP side, so don't take it for granted.
$_FILES['userfile']['size']
The size of the uploaded file, in bytes.
$_FILES['userfile']['tmp_name']
The temporary file name stored on the server after the file is uploaded.
$_FILES['userfile']['error']
Error code related to the file upload. This project was added in PHP version 4.2.0.
【AJAX file upload】
In fact, it is to achieve refresh-free file upload. The IFRAME file upload principle can be used.
Actually when uploading files with PHP. . . Only the $_FILES format can be used, but if we just use JS to get its ID, such as ..document.getElementById('img').value or jquery $("#img") in the form cannot be actually uploaded (but there are still many people doing this, including me at the beginning).
But the function also requires the implementation of the so-called "asynchronous upload", what should I do? ? You can only use third-party components, or write one yourself (embed an IFRAME in the web page). But if you are considering development time, you can use a third party. Here is a good jQuery Ajax file upload component, which is "ajaxfileupload.js". Its component download address is: http://www.phpletter.com/ , there is a PHP application demo inside after downloading, which is easy to understand.
Process:
(1) Code of the file on the front end: test.php
相应的HTML为:
这样客户端就完成了。
(2) 再服务器端时 doajaxfileupload.php
此处为了简便的检测是否真正的传值过来了,你可以将它存起来了。
$file_infor = var_export($_FILES,true);
file_put_contents("d:file_infor.php".$file_infor);
这样你打来刚生成的file_infor.php文件时,你又看到了熟悉的信息了:
array(
'name'=>'lamp.jpg',
'type'=>'image/pjpeg',
'tmp_name'=>'c: windowstempphpFA.tmp',
'error'=>0,
'size'=>3127
)
Of course, the real processing is similar to this of:
$upFilePath = "d:/";
);
if($ok === FALSE){
echo json_encode('file_infor'=>'Upload failed');
}else {
echo json_encode('file_infor'=>'Upload successful');
}
?>
Another note: Actually, you can Embed an IFRAME in a page, and then submit it using the native POST form in the IFRAME. This JQUERY plug-in also uses this method. It’s just that it is a dynamically generated IFRAME and form
Original text: http://fc-lamp.blog.163.com/blog/static/1745666872009519310153/

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools
