In PHP (Version 5.4.39-0+deb7u2) I upload files. This went smooth for about 3 years, until today.
After hours of testing I noticed that the $_FILES['Filedata']['name'] array is not readable when:
- the filename is written by a Mac OSX system when the PATHINFO_EXTENSION is '.jpg'
- files written as '.JPG' have no problems.
- when the filename contains '-_' (eg 20151126-_HG634163.JPG)
- files written on PC containing .jpg, .JPG, -_, ... are working perfect
Below the code that worked perfect until this morning. There were no server-changes. Both Chrome and Safari gave the same results. The $this->file_name variable is stored in a DB to prevent doubles.
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$ext = pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION);
$this->file_name = $_FILES['Filedata']['name'];
$fileTypes = array('jpg','JPG','jpeg'); // File extensions
$this->new_filename = $_POST['i']."_".date(YmdHis).$i."_".sha1($this->file)."_".rand(0, 1000)."_".rand(0, 1000).".JPG";
$this->new_file = $this->targetFile."/".$this->new_filename;
if (in_array($ext,$fileTypes)) {
move_uploaded_file($tempFile,$this->new_file);
} else {
$this->upload_error = 1;
}
}