问题
I have a form input type="file" element and it accepts a file . When I upload it and pass this on to the server side php script .
How do I write the temporary file stored in $_FILES["file"]["tmp_name"] byte by byte ? The below code does not work . It seems to write at the end of the request . IF for eg a connection is lost in between i would like to see if 40 % was complete so that i can resume it .
Any pointers ?
$target_path = "uploads/";
$target_path = $target_path . basename($name);
if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
// Open temp file
$out = fopen($target_path, "wb");
if ($out) {
// Read binary input stream and append it to temp file
$in = fopen($_FILES['file']['tmp_name'], "rb");
if ($in) {
while ($buff = fread($in, 4096))
fwrite($out, $buff);
}
fclose($in);
fclose($out);
}
}
回答1:
PHP does not hand over control to the file upload target script until AFTER the upload is complete (or has failed). You don't have to do anything to 'accept' the file - PHP and Apache will take care of writing it to the filename specified in the ['tmp_name'] parameter of the $_FILES array.
If you're trying to resume failed uploads, you'll need a much more complicated script.
来源:https://stackoverflow.com/questions/7840503/how-to-upload-a-file-byte-by-byte-in-php