Today, when the project ran into a problem: before a colleague left to do a site, there is a upload product detailed picture of the function, was completed, but due to the late program changes and changes in the level of the programmer is also uneven, resulting in a lot of program bugs, due to the use of a framework, Finally did not find the documentation, and then I re-wrote a combination of Ajax upload files upload.classs.php Although the interface is not beautiful, but easy to understand and maintenance.
The first is the page.
index.php
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>upload</title>
<script src= "Source/jquery.js" ></script>
<script src= "Source/jquery.form.js" ></script>
<body>
<form method= "POST" id= "Picform" enctype= "Multipart/form-data" >
<input type= "File" name= "file" >
</form>
<div id= ' pics ' ></div>
<form action= "model.php" id= "Infofrom" method= "POST" >
<input type= "Submit" value= "Submit" >
</form>
</body>
<script>
$ (": File"). Change (function () {
if (!$ (this). Val ())
{
Alert (' Please select picture! ');
return false;
}
$ ("#picform"). Ajaxsubmit ({
Type: ' Post ',
URL: ' upload.php ',
Success:function (data) {
Switch (data)
{
Case ' 0 ':
Alert (' File storage directory does not exist ');
Break
Case ' 1 ':
Alert (' System error, please check environment configuration ');
Break
Case ' 2 ':
Alert (' File type not allowed ');
Break
Case ' 3 ':
Alert (' File size is incorrect ');
Break
Default
var obj= $.parsejson (data);
Generate thumbnail images
$ ("#pics"). Append ("
var len= $ ("#infofrom: Hidden"). Length;
if (len==0)
{
$ ("#infofrom"). Append ("<input type= ' hidden ' value= '" +obj.picname+ "' id= ' picname ' name= ' Picname ' >");
$ ("#infofrom"). Append ("<input type= ' hidden ' value= '" +obj.picpath+ "' id= ' Picpath ' name= ' Picpath ' >");
$ ("#infofrom"). Append ("<input type= ' hidden ' value= '" +obj.relpicpath+ "' id= ' Relpicpath ' name= ' Relpicpath ' >");
}
Else
{
$ ("#picname"). Val ($ ("#picname"). Val () + ', ' +obj.picname ';
$ ("#picpath"). Val ($ ("#picpath"). Val () + ', ' +obj.picpath ';
$ ("#relpicpath"). Val ($ ("#relpicpath"). Val () + ', ' +obj.relpicpath ';
}
Break
}
}
})
})
</script>
The page that receives Ajax from the foreground
upload.php
<?php
Include_once ' Upload.class.php ';
$obj =new Upload (' uploads ', array ('. jpg '), 3145728, ' file ');
$arr = $obj->uploadfiles ();
if (Is_array ($arr))
{
echo Json_encode ($arr);
}
Else
{
Echo $arr;
}
?>
Simple File Upload class
upload.class.php
<?php
File Upload Class
Class upload{
Private $path; Upload path
Private $allowtype =array (); Resource type
Private $maxsize; File size allowed to upload
Private $file; Name of the file form
Private $error = 1; Define an error where the path does not exist
Construction method
Public function __construct ($path, $allowtype, $maxsize, $file)
{
$this->path = $path;
First check if the file path exists
if (!file_exists ($this->path))
{
$this->error=0;
Exit (' ERROR: File path does not exist! ');
}
$this->allowtype = $allowtype;
$this->maxsize = $maxsize;
$this->file = $file;
}
How to upload a resource
Public Function Uploadfiles ()
{
if ($this->error==0)
{
return 0;
}
First determine if the system is allowed to upload, and if there is an error
$eor = $_files[$this->file][' error '];
if ($eor!=0)
{
Return 1;//Asynchronous Transfer dedicated
Exit ("Error: Error number: {$eor}!");
}
Determine the file extension
$suf = STRRCHR ($_files[$this->file][' name '], '. ');
if (!in_array ($suf, $this->allowtype))
{
return 2;
Exit (' ERROR: File type denied! ');
}
Determine the size of a file
$size = $_files[$this->file][' size ');
if ($size > $this->maxsize)
{
return 3;
Exit (' ERROR: File too big! ');
}
/* When all of the above conditions are met, file upload
and returns the absolute path of the resource
Relative path
and file name */
$tempname = $_files[$this->file][' tmp_name ');
Date_default_timezone_set (' PRC ');
$newname = Date (' Ymdhis '). ' -'. Mt_rand (100,999). $suf;
$rootpath =dirname (__file__). ' /'. $this->path. ' /'. $newname;
if (Move_uploaded_file ($tempname, $this->path. ' /'. $newname))
{
The return array (' picname ' = $newname, ' picpath ' and ' = ' $rootpath, ' relpicpath ' + $this->path. ' /'. $newname);
}
}
}
?>
Inbound page after submitting a form on the front page
model.php
<?php
$DBH =new PDO (' mysql:host=localhost;dbname=test; ', ' root ', ');
$DBH->exec (' Set names UTF8 ');
$_post[' Picpath ']=addslashes (str_replace ('/', ' \ \ ', $_post[' Picpath '));
$sql = "INSERT into Mytab (Picname,picpath,relpicpath) values
(' $_post[picname] ', ' $_post[picpath] ', ' $_post[relpicpath] ') ";
$info = $dbh->exec ($sql);
?>
Explanation: The page is very simple, of course it's for testing.
Logical process:
1. Front page index.php: The first form is dedicated to adding pictures. Triggers the current form submission event through the Change event.
2. On the submitted upload.php page, introduce upload.class.php. You can get each of the different error returns (in the JS section at the bottom of the page in index.php) by using a digital annotation, and return the absolute address of the image when the upload is successful ( Update or delete the data in order to delete the useless pictures), the name of the picture, and the relative address of the picture (page display use).
The return value of 3.upload.class.php is further judged by upload.php, if the array description is successful, if it is a single number, the upload fails. and return the information to the foreground.
4.index.php receives the information returned by upload.php and, if it is a single number, makes a corresponding error alert. If it is a JSON-formatted data, that is converted to a JSON object by $.parsejson and called in the following JS code. This is: Create a input:hidden in the second form form on the page and store the information you need to use when you submit the form. Also here is a judgment, if the second form is already in the form of the music hidden is not the first picture, So directly after the picture is stitched in the hidden value behind. Also, if successful, a 100*100 thumbnail appears in the div with ID pics.
5. Finally click Submit in index.php to submit all the picture information ready to be put into storage. In model.php, the PDO was instantiated directly, and a SQL statement was written to test the results without problems.
Note: index.php to introduce: jquery.form.js
In model.php the Addslashes () function is important, and a single slash "\" in the database is missing in my test in the database. The same problem was also touched before, when it was not inserted. So here's an escape.
This entire process is complete.
as a new PHP, I hope to make progress here and everyone, this program may be safe, performance or anything I have not considered, use may also have some limitations. I hope you have a lot of advice.
This article is from the "My PHP path" blog, so be sure to keep this source http://phpwzh.blog.51cto.com/6651035/1712622
Ajax combines a file upload class for a single upload of multiple files