Topic: Need urgent help: File corrupted after upload

Hello,

I need urgent help to fix a problem i have no idea how to fix it. The upload works so far and the file is on the server. I have uploaded an mp4. After checking the uploaded file i saw that the file is corrupted and not working anymore. I opend the file to check it and i found the problem. The firstlines of the video are this:

-----------------------------281292573725845
Content-Disposition: form-data; name="name"

VID-20161116-WA0008.mp4
-----------------------------281292573725845
Content-Disposition: form-data; name="file"; filename="VID-20161116-WA0008.mp4"
Content-Type: video/mp4

For unknown reason the Uploader script put a part of the header in the file and i have no idea why. I use the standard Script just with some little changes to provide my needs. But the basic code is still from Plupload.

I also should tell you that this informations are not available in the uploader script. The script dont knows the Content-Type and not the filename. I had no idea why but i accepted it and made a work around.

At the end of the video file is this:

-----------------------------281292573725845--

If i remove this posted lines, the file is valid and works again. So the upload works. Please help me.

My uploader.php:

require('settings.php');

error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 'Off');
ini_set('error_log', ROOTPATH.'/uploader_error.log');
date_default_timezone_set("Europe/Berlin");
session_start();

require('function.php');

if(!$_SESSION['login']){
    exit;
}

@set_time_limit(3600);

$stamp = getdate();

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

// Settings
$targetDir = "tmp";
//$targetDir = 'uploads';
$cleanupTargetDir = true; // Remove old files
$maxFileAge = 86400; // Temp file age in seconds


// Create target dir
if (!file_exists($targetDir)) {
    @mkdir($targetDir);
}

// Get a file name
if (isset($_REQUEST["name"])) {
    $fileName = $_REQUEST["name"];
} elseif (!empty($_FILES)) {
    $fileName = $_FILES["file"]["name"];
} else {
    $fileName = uniqid("vid_");
}

$filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;

// Chunking might be enabled
$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
$chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;


// Remove old temp files    
if ($cleanupTargetDir) {
    if (!is_dir($targetDir) || !$dir = opendir($targetDir)) {
        die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
    }

    while (($file = readdir($dir)) !== false) {
        $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;

        // If temp file is current file proceed to the next
        if ($tmpfilePath == "{$filePath}.part") {
            continue;
        }

        // Remove temp file if it is older than the max age and is not the current file
        if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) {
            @unlink($tmpfilePath);
        }
    }
    closedir($dir);
}    


// Open temp file
if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb")) {
    die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
}

if (!empty($_FILES)) {
    if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
        die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
    }

    // Read binary input stream and append it to temp file
    if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
        die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
    }
} else {    
    if (!$in = @fopen("php://input", "rb")) {
        die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
    }
}

while ($buff = fread($in, 4096)) {
    fwrite($out, $buff);
}

@fclose($out);
@fclose($in);

// Check if file has been uploaded
if (!$chunks || $chunk == $chunks - 1) {
    // Strip the temp .part suffix off 
    rename("{$filePath}.part", $filePath);
}

// Return Success JSON-RPC response
//die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');

//Dateigröße auslesen
$filesize = round((filesize($filePath) / 1024) / 1024, 2);

//Video abspeichern
DBConnect();
$sql = "INSERT INTO `video_upload_queue` (`file`, `creation`, `userid`, `filesize`) VALUES ('$fileName', '$stamp[0]', '$_SESSION[userid]', '$filesize')";
mysql_query($sql) or error_log('Error: '.mysql_error(), 0);
$videoid = mysql_insert_id();
echo $videoid;
mysql_close($link);

Re: Need urgent help: File corrupted after upload

After adding this to the options:

    send_file_name: false,
    chunk_size: 0,

The header changed into:

-----------------------------20019704817480
Content-Disposition: form-data; name="file"; filename="VID-20161116-WA0008.mp4"
Content-Type: video/mp4

Now i only need to get rid of the rest but currently i have not found an idea how.

3 (edited by Yokul 2017-03-17 09:14:01)

Re: Need urgent help: File corrupted after upload

I was able to solve the problem with a workaround. After upload i check the file. If it contains wrong code it gets removed. Here is the code:

$videofile = file("tmp/$fileName");
$rows = count($videofile);

if(substr($videofile[0], 0, 4) == "----"){
    unset($videofile[0]);
    unset($videofile[1]);
    unset($videofile[2]);
    unset($videofile[3]);
    unset($videofile[$rows - 1]);
    unset($videofile[$rows]);
    
    $file_handle=fopen("tmp/$fileName","w");
    fputs($file_handle,implode("",$videofile));
    fclose($file_handle);
}