从PHP Switch调用变量

时间:2012-07-07 08:45:09

标签: php sqlite

我可以将文档上传到我的主测试目录。但我希望调出我的变量,即staffNo并将其放在特定的staffNo目录中。但即使我已经为我的输入声明了合适的名称,我也无法解析变量。

例如:

员工页面

$staffNo = 1;
<form id="Staff" name="Staff" method="post" action="upload.php" enctype="multipart/form-data">

//My staffno variable
<input type="hidden" id="staffNo" name="staffNo" value="<?php echo $staffNo ?>"/>

//Upload document
<input name="upload" id="upload" type="file"/>
<input type="hidden" name="upload" value="upload"/>

//Submit button
<input type="submit" name="submit" value="Submit">
</form>

upload.php的

switch($_POST['submit']) 
{
case 'Submit':
    if ($_FILES["upload"]["error"] > 0)
    {
    echo "Error: " . $_FILES["upload"]["error"] . "<br />";
    }
    else
    {
    echo "Upload: " . $_FILES["upload"]["name"] . "<br />";
    echo "Type: " . $_FILES["upload"]["type"] . "<br />";
    echo "Stored in: " . $_FILES["upload"]["tmp_name"];

    move_uploaded_file($_FILES["upload"]["tmp_name"],
  "documents/"."staffNo"."/".$_FILES["upload"]["name"]);

            echo "Stored in: " ."./documents/"."staffNo"."/".  $_FILES["upload"]["name"];
}

break;

case 'others':
break;

default;

我的staffOo变量无法调出,即使它已经在表单下。我在某处做错了吗?我还想为staffNo创建一个新文件夹,如果它没有在里面找到的话。但是现在基本的staffNo变量无法调用。好心提醒。

1 个答案:

答案 0 :(得分:2)

我修复了你提交的upload.php中的几个错误。 您需要将隐藏字段的值分配给变量: $staffNo = $_POST['staffNo'];

upload.php的

switch($_POST['submit']) 
{
    case 'Submit':
        if ($_FILES["upload"]["error"] > 0)
        {
            echo "Error: " . $_FILES["upload"]["error"] . "<br />";
        }
        else
        {
            $staffNo = $_POST['staffNo']; // preform validation here
            echo "Upload: " . $_FILES["upload"]["name"] . "<br />";
            echo "Type: " . $_FILES["upload"]["type"] . "<br />";
            echo "Stored in: " . $_FILES["upload"]["tmp_name"];
            // check if directory exists
            if(!is_dir("documents/".$staffNo)){
                // if it doesn't - create it
                mkdir("documents/".$staffNo);
            }
            move_uploaded_file($_FILES["upload"]["tmp_name"],
            "documents/".$staffNo."/".$_FILES["upload"]["name"]);
            echo "Stored in: " ."./documents/".$staffNo."/".  $_FILES["upload"]["name"];
        }

    break;

    case 'others':
    break;

    default;
}

您应该验证staffNo以防止通过firebug更改其值,以及弄乱上传路径。