Home  >  Article  >  Backend Development  >  php+jquery Ajax asynchronous upload image (ajaxSubmit) example_PHP tutorial

php+jquery Ajax asynchronous upload image (ajaxSubmit) example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:47:101459browse

Let’s take a look at an example of php+jquery Ajax asynchronous image upload (ajaxSubmit). This time we really use ajax instead of using iframe or the like.

The effect is as follows

The code is as follows Copy code

index.php file

php combines jquery to asynchronously upload images (ajaxSubmit). The following is the submission page code:





Ajax asynchronous upload of images








  上传文件
   

   
     
       
       
            
   
       


       
       

doupfiles.php file

The code is as follows Copy code
 代码如下 复制代码

/*
* 1:成功上传
*-1:文件超过规定大小
*-2:文件类型不符
*-3:移动文件出错
*/
if(is_uploaded_file($_FILES['upfile']['tmp_name'])){

$photo_types=array('image/jpg', 'image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png');//定义上传格式
$max_size=700000; //上传照片大小限制,默认700k
$photo_folder="upload/".date("Y")."/".date("m")."/".date("d")."/"; //上传照片路径
///////////////////////////////////////////////////开始处理上传
if(!file_exists($photo_folder))//检查照片目录是否存在
{
mkdir($photo_folder, 0777, true); //mkdir("temp/sub, 0777, true);
}

$upfile=$_FILES['upfile'];
$name=$upfile['name'];
$type=$upfile['type'];
$size=$upfile['size'];
$tmp_name=$upfile['tmp_name'];

$file = $_FILES["upfile"];
$photo_name=$file["tmp_name"];
//echo $photo_name;
$photo_size = getimagesize($photo_name);

if($max_size < $file["size"])//检查文件大小
echo "-1"; //echo "<script>alert('对不起,文件超过规定大小!');history.go(-1);</script>";
if(!in_array($file["type"], $photo_types))//检查文件类型
           echo "-2";       //echo "<script>alert('对不起,文件类型不符!');history.go(-1);</script>";
if(!file_exists($photo_folder))//照片目录
                  mkdir($photo_folder);
$pinfo=pathinfo($file["name"]);
$photo_type=$pinfo['extension'];//上传文件扩展名
$photo_server_folder = $photo_folder.time().".".$photo_type;//以当前时间和7位随机数作为文件名,这里是上传的完整路径


if(!move_uploaded_file ($photo_name, $photo_server_folder))
            {
             echo "-3"; //echo "移动文件出错";
                exit;
            }
$pinfo=pathinfo($photo_server_folder);
$fname=$pinfo['basename'];
echo "1";   //echo " 已经成功上传:".$photo_server_folder."
";

 


}
?>

/*
* 1: Successfully uploaded
*-1: The file exceeds the specified size
*-2: File type does not match
*-3: Error moving file
*/
if(is_uploaded_file($_FILES['upfile']['tmp_name'])){ $photo_types=array('image/jpg', 'image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png '); //Define upload format
                                    $max_size=700000;                                                                                                                                                                                                                                                                     $photo_folder="upload/".date("Y")."/".date("m")."/".date("d")."/"; //Upload photo path
////////////////////////////////////////////////////// /Start processing upload
If(!file_exists($photo_folder))//Check whether the photo directory exists
                                  {
              mkdir($photo_folder, 0777, true); //mkdir("temp/sub, 0777, true);
} $upfile=$_FILES['upfile'];
$name=$upfile['name'];
$type=$upfile['type'];
$size=$upfile['size'];
$tmp_name=$upfile['tmp_name']; $file = $_FILES["upfile"];
$photo_name=$file["tmp_name"];
//echo $photo_name;
$photo_size = getimagesize($photo_name); if($max_size < $file["size"])//Check file size
Echo "-1"; // echo "& lt; script & gt; alert ('Sorry, files exceed the specified size!'); History.go (-1); & lt; /script & gt;"
if(!in_array($file["type"], $photo_types))//Check file type
                    echo "-2";                                                                                                                                                  //echo "<script>alert('Sorry, the file type does not match!'); history.go(-1);</script>";
if(!file_exists($photo_folder))//Photo directory
                      mkdir($photo_folder);
$pinfo=pathinfo($file["name"]);
$photo_type=$pinfo['extension'];//Upload file extension
$photo_server_folder = $photo_folder.time().".".$photo_type;//Using the current time and a 7-digit random number as the file name, here is the full path to upload
if(!move_uploaded_file ($photo_name, $photo_server_folder))
                                                        {
                echo "-3"; //echo "Error moving file";
exit;
             }
$pinfo=pathinfo($photo_server_folder);
$fname=$pinfo['basename'];
echo "1"; //echo " Uploaded successfully: ".$photo_server_folder."
";
}
?>

Finally, download the complete php+jquery Ajax asynchronous image upload (ajaxSubmit) example source code download address

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632888.htmlTechArticleLet’s take a look at an example of php+jquery Ajax asynchronous image upload (ajaxSubmit), which we really used Ajax instead of using iframe or something like that. The effect is as follows. The code is as follows...
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