我正在尝试编写一个用户可以上传文件的小脚本,但出于某种原因,我遇到了问题。出于某种原因,当单击提交按钮时,_$FILES
数组将返回NULL(使用var_dump()
和var_export()
进行测试),所以当然其他所有内容都被跳过,我得到“错误:没有上传文件“我无法确定这是为什么。下面是表单和PHP脚本的HTML代码段。
HTML
<form class='admin_form' action='' method='POST' enctype='multipart/form-data'>
<fieldset>
<label for='comic_page'>Filename</label>
<input type='file' name='uploaded_file' id='uploaded_file'>
<input type='submit' name='comic_page_btn' value='Upload'>
<span id='comic_page_upl_err_msg'>".$err_msg."</span>
</fieldset>
</form>
PHP
if(isset($_POST['comic_page_btn'])){
echo var_dump($_FILES['uploaded_file']['name']).'<br>';
echo var_export($_FILES["uploaded_file"]);
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
//Check if the file is JPEG image and it's size is less than 350Kb
$filename = basename($_FILES['uploaded_file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") &&
($_FILES["uploaded_file"]["size"] < 350000)) {
//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'/uploads/'.$filename;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname)) {
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
$err_msg = "The file has been saved as: ".$newname;
} else {
$err_msg = "Error: A problem occurred during file upload!";
}
} else {
$err_msg = "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
} else {
$err_msg = "Error: Only .jpg images under 350Kb are accepted for upload";
}
} else {
$err_msg = "Error: No file uploaded";
}
}