我正在尝试上传文件,所以这是我的 html:
<form action="upload_file.php" method="post" enctype="multipart/form-data">
Select a file: <input type="file" name="upload">
<input type="submit">
</form>
这是我的php:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
session_start();
$allowedExts = array("doc", "docx");
$extension = pathinfo( $_FILES["upload"]["name"],PATHINFO_EXTENSION);
if (($_FILES["upload"]["size"] < 200000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["upload"]["error"] > 0)
{
echo "Return Code: " . $_FILES["upload"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["upload"]["name"] . "<br />";
echo "Type: " . $_FILES["upload"]["type"] . "<br />";
echo "Size: " . ($_FILES["upload"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["upload"]["tmp_name"] . "<br />";
$dir_exists = is_dir("Proposals/". $_SESSION["FirstName"] ."/");
$file_exists = file_exists("Proposals/".$_SESSION["FirstName"] ."/" . $_FILES["upload"]["name"]);
$folderName=$_SESSION["FirstName"];
$baseDir = "Proposals" ;
//var_dump($_FILES);
//die();
// Create directory if it does not exist
if (! $dir_exists) {
if(!is_dir($baseDir))
mkdir($baseDir);
mkdir($baseDir . DIRECTORY_SEPARATOR . $_SESSION["FirstName"]);
}
if ($file_exists) {
echo $_FILES["upload"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["upload"]["tmp_name"],
"Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"]);
echo "Stored in: " . "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"];
}
}
} else {
echo "Invalid file";
}
?>
文件输出:
Type: application/msword
Size: 124.9296875 Kb
Temp file: /tmp/phpUWDc3s
var_dump 在测试文件中输出了这个:
array(1) { ["upload"]=> array(5) {
["name"]=> string(18) "Test.doc"
["type"]=> string(18) "application/msword"
["tmp_name"]=> string(14) "/tmp/phpUWDc3s"
["error"]=> int(0)
["size"]=> int(127928) } }
它给我的警告/错误是:
Strict Standards: Only variables should be passed by reference in /disks/vhosts/***/***/upload_file.php on line 6 Upload: Test.doc
没有它给我的 mkdir 代码:
Warning: move_uploaded_file(Proposals/Test.doc): failed to open stream: Permission denied in /disks/vhosts/***/***/upload_file.php on line 40 Warning: move_uploaded_file(): Unable to move '/tmp/phpQmgxdO' to 'Proposals/Test.doc' in /disks/vhosts/***/***/upload_file.php on line 40 Stored in: Proposals/Test.doc
出于某种原因,无论是否使用 mkdir,提案文件中都没有放入任何文件,即使它是正确的文件类型。这只是服务器的权限问题,还是我必须添加/更改才能使这一切正常工作?