Skip to content

Instantly share code, notes, and snippets.

@rvpeterson
Forked from gpbmike/Streamer.php
Created March 12, 2016 21:12
Show Gist options
  • Save rvpeterson/ed5265f94e6b65d5f18a to your computer and use it in GitHub Desktop.
Save rvpeterson/ed5265f94e6b65d5f18a to your computer and use it in GitHub Desktop.
php backend for streaming file upload with XMLHttpRequest
<?php
class File_Streamer
{
private $_fileName;
private $_contentLength;
private $_destination;
public function __construct()
{
if (!isset($_SERVER['HTTP_X_FILE_NAME'])
&& !isset($_SERVER['CONTENT_LENGTH'])) {
throw new Exception("No headers found!");
}
$this->_fileName = $_SERVER['HTTP_X_FILE_NAME'];
$this->_contentLength = $_SERVER['CONTENT_LENGTH'];
}
public function isValid()
{
if (($this->_contentLength > 0)) {
return true;
}
return false;
}
public function setDestination($destination)
{
$this->_destination = $destination;
}
public function receive()
{
if (!$this->isValid()) {
throw new Exception('No file uploaded!');
}
$fileReader = fopen('php://input', "r");
$fileWriter = fopen($this->_destination . $this->_fileName, "w+");
while(true) {
$buffer = fgets($fileReader, 4096);
if (strlen($buffer) == 0) {
fclose($fileReader);
fclose($fileWriter);
return true;
}
fwrite($fileWriter, $buffer);
}
return false;
}
}
<?php
require_once('Streamer.php');
$ft = new File_Streamer();
$ft->setDestination('data/');
$ft->receive();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment