【问题标题】:Why won't this php upload script work?为什么这个 php 上传脚本不起作用?
【发布时间】:2012-05-12 22:06:27
【问题描述】:

我有一个带有名为 image 的文件输入的 html 表单,它指向一个带有此代码的 php 文件:

$date =  date( "Y_m_d_H_i_s_u" );

function upload() {

$info = pathinfo($_FILES['image']['name']);
$target = "uploads/" . $date . $info['extension'];

if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
    return true;
} else{
    return false;
}
}

我希望文件名包含时间而不是原始文件名。我不明白为什么这行不通!所有上传的文件都被命名为扩展名。不知何故,日期不起作用。

【问题讨论】:

  • 可变范围。 $date 是在函数外部定义的:将它作为参数传递就可以了。
  • 问题是变量范围,但在元级别上,您假设上传成功,这不是一个好主意。始终检查 $_FILES 数组中的 ['error'] 参数之前您对文件执行任何操作。

标签: php html file date upload


【解决方案1】:

您的scope$date 错误。 您需要将 $date 传递给您的函数或将其设为全局变量

$date =  date( "Y_m_d_H_i_s_u" );

function upload($date) {
    $info = pathinfo($_FILES['image']['name']);
    $target = "uploads/" . $date . $info['extension'];

    if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
        return true;
    } else{
        return false;
    }
}

$date =  date( "Y_m_d_H_i_s_u" );

function upload() {
    global $date;
    $info = pathinfo($_FILES['image']['name']);
    $target = "uploads/" . $date . $info['extension'];

    if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
        return true;
    } else{
        return false;
    }
}

【讨论】:

  • 如何让它再次全球化?
  • 我建议不要将其设为全局,而是将其传递给函数upload($data){}
  • 我想让它成为全球性的,这样我就可以在任何功能中随意使用它
  • 为什么不把它传递给任何需要它的函数呢?
  • 全球化是最糟糕的做法
【解决方案2】:

$date 超出了您的功能范围。 有两种方法可以解决这个问题:

选项 1

$date = date( "Y_m_d_H_i_s_u" );

function upload() {
    globel $date;
    $info = pathinfo($_FILES['image']['name']);
    $target = "uploads/" . $date . $info['extension'];

    if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
        return true;
    }
    else{
        return false;
    }
}

选项 2

$date = date( "Y_m_d_H_i_s_u" );

function upload($date) {
    $info = pathinfo($_FILES['image']['name']);
    $target = "uploads/" . $date . $info['extension'];

    if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
        return true;
    }
    else{
        return false;
    }
}

upload ($date);

【讨论】:

  • 我猜你希望它是global $date;,而不是global data;
【解决方案3】:

您也可以考虑直接返回move_uploaded_file

return move_uploaded_file($_FILES['image']['tmp_name'], $target)

【讨论】:

    【解决方案4】:

    这是我的观察,您遇到scope 问题

    $date =  date( "Y_m_d_H_i_s_u" );
    

    试试日期是否总是会改变

    function upload() {
        $date =  date( "Y_m_d_H_i_s_u" );
        $info = pathinfo ( $_FILES ['image'] ['name'] );
        $target = "uploads/" . $date . $info ['extension'];
        if (move_uploaded_file ( $_FILES ['image'] ['tmp_name'], $target )) {
            return true;
        } else {
            return false;
        }
    }
    

    【讨论】:

    • 为什么要把它设为常数???我看不到目的。常量不能替代全局变量(尽管它们可以被全局访问。但从逻辑上讲,这是错误的)
    • 我想 with 希望他们所有人都有一个恒定的时间,但版本会有所不同
    猜你喜欢
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 2020-12-14
    • 2013-10-20
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多