简体   繁体   English

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

[英]Retrieve original uploaded filename using php on OpenShift

I would like to check that a file uploaded to my OpenShift app has a text extension (.txt or .tab). 我想检查上传到我的OpenShift应用程序的文件是否具有文本扩展名(.txt或.tab)。 Following some advice given here I wrote the following code, with echoes added to help debug: 遵循此处给出的一些建议我编写了以下代码,并添加了回声以帮助调试:

$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>";

On uploading any file, the echoed response was: 上传任何文件时,回显的响应是:

AllowedExts: txt and tab AllowedExts:txt和制表符

ThisPath: /var/lib/openshift/************/php/tmp/ phpSmk2Ew ThisPath:/ var / lib / openshift / ************ / php / tmp / phpSmk2Ew

ThisExt: ThisExt:

error echo: Uploaded file must end in .txt or .tab 错误回显:上传的文件必须以.txt或.tab结尾

Does this mean that OpenShift is renaming the file upon upload? 这是否意味着OpenShift会在上传时重命名文件? How do I get the original filename and then check its suffix? 如何获取原始文件名,然后检查其后缀? More generally, is there a better way to check the file type? 更一般而言,是否有更好的方法来检查文件类型?

$_FILES['uploadedfile']['tmp_name'] contains the name of a temporary file on the server (which can be moved with move_uploaded_file() ). $_FILES['uploadedfile']['tmp_name']包含服务器上临时文件的名称(可以使用move_uploaded_file()进行移动)。 If you want to check the original name of the uploaded file on the client machine use $_FILES['uploadedfile']['name'] . 如果要在客户端计算机上检查上载文件的原始名称,请使用$_FILES['uploadedfile']['name']

That's not an Open Shift issue, it's the standard way of PHP. 这不是Open Shift问题,而是PHP的标准方法。

For further details see http://php.net/manual/en/features.file-upload.post-method.php 有关更多详细信息,请参见http://php.net/manual/en/features.file-upload.post-method.php

For other ways to detect the file type see http://php.net/manual/en/ref.fileinfo.php 有关检测文件类型的其他方法,请参见http://php.net/manual/en/ref.fileinfo.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM