A very simple example of jQuery mobile + PHP upload via Hyper-global $_files, and then use the Move_uploaded_file () method to move the uploaded image to a folder under the local server,
Here is the HTML code
The code is as follows |
Copy Code |
<! DOCTYPE html> <meta charset = "Utf-8" > <link rel= "stylesheet" href= "Http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" > <script src= "Http://code.jquery.com/jquery-1.8.3.min.js" ></script> <script src= "Http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js" ></script> <body> <div data-role= "page" id= "Upload" > <div data-role= "Header" > <a href= "#pageone" DATA-ROLR = button data-icon= "Home" class= "Ui-btn-left" > Homepage </a> </div> <div data-role= "Content" > <form action= "upload_file.php" method= "post" enctype= "Multipart/form-data" data-ajax= "false" > <input id= "uploadimg" name= "file" type= "file" runat= "server" method= "POST" Enctype= "Multipart/form-data" data-inline= "true" data-ajax= "false"/> <center><button data-inline= "true" > Upload </button></center> </form> </div> <div data-role= "Footer" data-position= "fixed" data-fullscreen= "true" > </div> </div> </body> |
Code for PHP
The code is as follows |
Copy Code |
<?php if ($_files["file"] ["error"] > 0) { echo "Return Code:". $_files["File" ["Error"]. "<br/>"; } Else { echo "Upload:". $_files["File" ["Name"]. "<br/>"; echo "Type:". $_files["File" ["type"]. "<br/>"; echo "Size:". ($_files["File"] ["size"]/1024). "Kb<br/>"; echo "Temp file:". $_files["File" ["Tmp_name"]. "<br/>"; if (file_exists ("upload/". $_files["File" ["Name"]) { echo $_files["File" ["Name"]. "already exists." } Else { Move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/". $_files["File" ["name"]); echo "Stored in:". Upload/". $_files["File" ["Name"]; } } } ?> |
The code is very simple, but the use of the process found a problem, I tried for a long time can not upload. Asked the small partner, found that the problem is insufficient file permissions, thereby restricting the page upload pictures to the folder. So the solution is to remove the permissions on the folder.
The code is as follows |
Copy Code |
$ cd/var/www $ sudo chmod-r 777 HTML |
OK, you can upload the file to the server folder now.