0

I would like to check that a file uploaded to my OpenShift app has a text extension (.txt or .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

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

ThisExt:

error echo: Uploaded file must end in .txt or .tab

Does this mean that OpenShift is renaming the file upon upload? How do I get the original filename and then check its suffix? More generally, is there a better way to check the file type?

Community
  • 1
  • 1
dudb
  • 45
  • 6

1 Answers1

0

$_FILES['uploadedfile']['tmp_name'] contains the name of a temporary file on the server (which can be moved with move_uploaded_file()). If you want to check the original name of the uploaded file on the client machine use $_FILES['uploadedfile']['name'].

That's not an Open Shift issue, it's the standard way of PHP.

For further details see 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

Reeno
  • 5,720
  • 11
  • 37
  • 50
  • Thanks very much - I had even used this before but forgotten about it - duh! Could you maybe explain *why* php does this and doesn't just point $_FILES['uploadedfile']['name'] at the file contents? I couldn't find this explained in the manual. Is it a security feature? – dudb Oct 16 '14 at 12:59
  • Glad I could help! I don't know exactly why `tmp_name` exists, but I think it's mainly because of some file systems cannot handle every character. In Linux I can put things like a line break or question marks in filenames which wouldn't work on a windows server. `tmp_name` only contains characters which fit every common file system. – Reeno Oct 16 '14 at 13:12