在OpenShift上使用php检索原始上传的文件名


Retrieve original uploaded filename using php on OpenShift

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

$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

ThisExt:

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

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

$_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