2,upload_file.php
-
- //php普通文件上传
- //by bbs.it-home.org
- if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000)){
- if ($_FILES["file"]["error"] > 0) {
- echo "Return Code: " . $_FILES["file"]["error"] . "
";
- }else {
- echo "Upload: " . $_FILES["file"]["name"] . "
";
- echo "Type: " . $_FILES["file"]["type"] . "
";
- echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";
- echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";
-
- 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"];
- }
- }
-
- }else {
- echo "Invalid file";
- }
- ?>
复制代码
二、异步文件上传
使用iframe异步上传文件。
1,前端html
2,Js代码
-
- function startUpload() {
- var spanObj = document.getElementById("info");
- spanObj.innerHTML = " 开始上传";
- document.getElementById("upForm").sumbit();
- }
- //回调
- function stopUpload(responseText){
- var spanObj = document.getElementById("info");
- spanObj.innerHTML = "上传成功";
- spanObj.innerHTML = responseText;
- }
-
复制代码
2), server-side code
-
-
$file = $_FILES['myfile']; - $fileName = uploadFile($file);
- //$result = readFromFile("../upload /" . $fileName);
- echo "";
-
- function uploadFile($ file) {
- // Upload path
- $destinationPath = "../upload/";
- if (!file_exists($destinationPath)){
- mkdir($destinationPath , 0777);
- }
- //Rename
- $fileName = date('YmdHis') . '_' . iconv('utf-8' , 'gb2312' , basename($file['name']));
- if (move_uploaded_file($file['tmp_name'], $ destinationPath . $fileName)) {
- return iconv('gb2312' , 'utf-8' , $fileName);
- }
- return '';
- }
//Code Comments
- / *
- 1, about the basename method
- $path = "/testweb/home.php";
- //Display the file name with the file extension
- echo basename($path);
- //Display without the file extension The file name
- echo basename($path,".php");
-
- 2, about iconv
- iconv('gb2312', 'utf-8', $fileName);//Convert $fileName from gb2312 to utf- 8 format.
- Note: This function needs to open php_iconv.dll in php.ini
-
- 3, about $_FILES['myfile']
- $_FILES is equivalent to a two-dimensional array, and $_FILES['myfile'] is equivalent to a one-dimensional array array. So
- $f = $_FILES['myfile'];
- echo $f['name'];
-
- If you directly access the $_FILES['myfile'], Undefined index: myfile will be reported. At this time add
- if(!isset($_FILES['myfile'])){
- die('The uploaded file does not exist!');
- }
- */
-
Copy code
|