【问题标题】:Retrieve original uploaded filename using php on OpenShift在 OpenShift 上使用 php 检索原始上传的文件名
【发布时间】:2014-10-14 23:08:08
【问题描述】:

我想检查上传到我的 OpenShift 应用程序的文件是否具有文本扩展名(.txt 或 .tab)。根据given here 的一些建议,我编写了以下代码,并添加了回声以帮助调试:

$AllowedExts =  array('txt','tab');
 echo "AllowedExts: " . $AllowedExts[0] . " and " . $AllowedExts[1] . "<br>";
$ThisPath = $_FILES['uploadedfile']['tmp_name'];
 echo "ThisPath: " . $ThisPath . "<br>";
$ThisExt = pathinfo($ThisPath, PATHINFO_EXTENSION);
 echo "ThisExt: " . $ThisExt . "<br>";
if(!in_array($ThisExt,$AllowedExts) ) {
    $error = 'Uploaded file must end in .txt or .tab';
}
 echo "error echo: " . $error . "<br>";

在上传任何文件时,回显的响应是:

AllowedExts:txt 和制表符

此路径:/var/lib/openshift/************/php/tmp/phpSmk2Ew

这个分机:

错误回显:上传的文件必须以 .txt 或 .tab 结尾

这是否意味着 OpenShift 会在上传时重命名文件?如何获取原始文件名,然后检查其后缀?更一般地说,有没有更好的方法来检查文件类型?

【问题讨论】:

    标签: php openshift


    【解决方案1】:

    $_FILES['uploadedfile']['tmp_name'] 包含服务器上一个临时文件的名称(可以用move_uploaded_file() 移动)。如果您想在客户端机器上检查上传文件的原始名称,请使用$_FILES['uploadedfile']['name']

    这不是 Open Shift 问题,这是 PHP 的标准方式。

    更多详情请见http://php.net/manual/en/features.file-upload.post-method.php

    其他检测文件类型的方法见http://php.net/manual/en/ref.fileinfo.php

    【讨论】:

    • 非常感谢——我以前什至用过这个但忘记了——呵呵!您能否解释一下为什么 php 这样做并且不只是将 $_FILES['uploadedfile']['name'] 指向文件内容?我在手册中找不到这个解释。它是一项安全功能吗?
    • 很高兴我能帮上忙!我不知道tmp_name 存在的确切原因,但我认为这主要是因为某些文件系统无法处理每个字符。在 Linux 中,我可以在文件名中放置换行符或问号之类的东西,这在 Windows 服务器上不起作用。 tmp_name 仅包含适合每个常见文件系统的字符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    相关资源
    最近更新 更多