append("\r\n"); preg_match('/boundary\s*=\s*([^\s]+)/', $_ENV['CONTENT_TYPE'], $matches); $this->boundary_ = "\r\n" . "--" . $matches[1]; } /// /// @return /// TRUE found boundary and got 1 or more header lines /// FALSE otherwise /// function header() { $line = $this->read_until($this->boundary_, $found); if (!$found) { return array(); } $this->getline("\r\n"); $headers = array(); while (($line = $this->getline("\r\n")) !== FALSE and $line != '') { array_push($headers, $line); } return $headers; } function filename() { foreach ($this->header() as $line) { if (preg_match('/filename="([^"]+)"/', $line, $matches)) { return $matches[1]; } } return FALSE; } function upload($outdir) { for ($filename = $this->filename(); $filename !== FALSE; $filename = $this->filename()) { $fhw = NULL; $filename = preg_replace('/\x5c/', "/", $filename); $filename = preg_replace('|.*/|', "", $filename); $filename = "$outdir/$filename"; $total = $_ENV['CONTENT_LENGTH']; $tmpfile = "$filename.$total.uploading"; $fhw = fopen($tmpfile, "w"); $this->write_until($fhw, $this->boundary_, $found); fclose($fhw); if (filesize($tmpfile) < $total - 3000) { print("File too small. Maybe upload suspended?"); unlink($tmpfile); } else { if (file_exists($filename)) { unlink($filename); } rename($tmpfile, $filename); } } } } // End of file