我无法理解为什么这段代码不起作用。 load_docs_2.php的输出是: Array()文件类型不正确。这是在XAMPP上运行。
HTML是:
<form id="import_form" method="post" action="load_docs_2.php" enctype="multipart/form-data">
<label for "importfile">Import PDF File</label><br>
<input id="import_name" name="load" type="file" value="Upload File"><br><br>
<input id="import" name="import" type="submit" value="Import">
</form>
load_docs_2.php是:
<?php
// Capture file data
print_r($_FILES);
$loadname = $_FILES["load"]["name"]; // name contains original name
$loadname_temp = $_FILES["load"]["tmp_name"]; // tmp_name contains the temporary file name
// Check file meta data
$import_size = $_FILES["load"]["size"];
$import_type = $_FILES["load"]["type"];
// Setting target folder
$target_dir = "./Staff Docs";
$target_file = $target_dir . $loadname; // Create target file path
// Move to import folder
$allowed = array('pdf');
$ext = pathinfo($loadname, PATHINFO_EXTENSION);
if (!in_array($ext,$allowed))
{
echo 'Incorrect file type';
}
else
{
move_uploaded_file($loadname_temp, $target_file);
}
?>
为什么文件加载不起作用?
答案 0 :(得分:0)
您可以更改此代码
$target_dir = "Staff Docs/";
现在工作正常
答案 1 :(得分:0)
您的目标文件不正确。目标目录和文件之间没有目录分隔符,因此结果本质上是错误的。
<?php
// Capture file data
print_r($_FILES);
$loadname = $_FILES["load"]["name"]; // name contains original name
$loadname_temp = $_FILES["load"]["tmp_name"]; // tmp_name contains the temporary file name
// Check file meta data
$import_size = $_FILES["load"]["size"];
$import_type = $_FILES["load"]["type"];
// Setting target folder
//$target_dir = "./Staff Docs"; //<== THIS IS WHERE THE PROBLEM WAS...
$target_dir = "./Staff Docs/"; //<== ADD A DIRECTORY SEPARARATOR BEFORE CONCATENATING WITH $loadname...
$target_file = $target_dir . $loadname; // Create target file path
// Move to import folder
$allowed = array('pdf');
$ext = pathinfo($loadname, PATHINFO_EXTENSION);
if (!in_array($ext,$allowed)){
echo 'Incorrect file type';
}else{
move_uploaded_file($loadname_temp, $target_file);
}
或者,您可以直接在描述$ target_file的行上添加目录分隔符,如下所示:
<?php
// Capture file data
print_r($_FILES);
$loadname = $_FILES["load"]["name"]; // name contains original name
$loadname_temp = $_FILES["load"]["tmp_name"]; // tmp_name contains the temporary file name
// Check file meta data
$import_size = $_FILES["load"]["size"];
$import_type = $_FILES["load"]["type"];
// Setting target folder
$target_dir = "./Staff Docs"; //<== THIS IS WHERE THE PROBLEM WAS...
$target_file = $target_dir . DIRECTORY_SEPARATOR . $loadname; // Create target file path
// Move to import folder
$allowed = array('pdf');
$ext = pathinfo($loadname, PATHINFO_EXTENSION);
if (!in_array($ext,$allowed)){
echo 'Incorrect file type';
}else{
move_uploaded_file($loadname_temp, $target_file);
}