I am attempting to upload a video file in chunks using PHP. (and we all know how finnicky PHP is with this) Below is my test code:
echo "<form action='' method='post' enctype='multipart/form-data'>";
echo "<input name='video' type='file' />";
echo "<input type='submit' value='UPLOAD' />";
echo "</form>";
if (isset($_POST['video']))
{
$putdata = fopen("php://input", "r");
$fp = fopen("assets/video/test.mp4", "w");
while ($data = fread($putdata, 1024))
{
echo $data;
fwrite($fp, $data);
}
echo "<h1>DONE! (hopefully)</h1>";
fclose($fp);
fclose($putdata);
}
When I echo $data;
, I don't get the contents of the file--instead, I only get video=video.mp4
, with video.mp4
being the name of the file I attempted to upload. What's going on? :( How do I get the actual file's contents?
In PHP, we can access the actual name of the file which we are uploading by keyword $_FILES[“file”][“name”]. The $_FILES is the by default keyword in PHP to access the details of files that we uploaded.
enctype="multipart/form-data": This value refers to the content type of the files that will be accepted for uploading. It also indicates the type of encoding that the PHP script will use for uploading. The multipart/form-data value enables us to upload files using the POST method.
php stores all temporary files, that includes uploaded files, in the temporary files directory as specified in the php. ini. Note that for uploads, those files might be removed as soon as the script the file was uploaded to was terminated (so unless you delay that script, you probably won't see the uploaded file).
PHP | $_FILES Array (HTTP File Upload variables)
Client-side libraries should be used for chunked uploads like this, such as PLUpload.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With