代码修改来自w3school
如果服务器配置问题,会有上传不成功的情况,可以试试将文件夹属性改为777. (sudo chmod (-R同时设置所有子目录) 777 OOXX )
(仔细把代码打一遍还是能学到很多事情的..为什么以前的apache不需要修改权限呢?不明白…)
上传控件
<form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form>
获取文件判断转移
$filename = $_FILES["file"]["name"]; $filetype = $_FILES["file"]["type"]; $filesize = $_FILES["file"]["size"]; if ((($filetype=="image/gif") ||($filetype=="image/jpeg") ||($filetype=="image/pjpeg")) &&($filesize < 20000)) { if ($_FILES["file"]["error"] > 0 ) { echo "Error: " . $_FILES["file"]["error"] . "
"; } else { echo "upload: " .$filename . "
"; echo "Type: " .$filetype . "
"; echo "Size: " .($filesize /1024) . "KB
"; echo "Stored in: " .$_FILES["file"]["tmp_name"] ."
"; if (file_exists("upload/" . $filename)) { echo $filename . " already exists, try change a name."; } else { $res=move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"] ); if ($res) { echo "Stored in: " . "upload/" . $filename; } else { echo "Error in move!"; } } } } else { echo "Invalid file"; }