splitting a file for upload via php

风格不统一 提交于 2021-02-11 13:21:18

问题


in php.ini and nginx configurations, there are limits that can be set which restrict the total amount of size a request can contain. 1mb for example.

so with that requirement, if a user wants to upload a file that is 10mb , i want to split the file into 1mb chunks and send it to multiple backend application servers where they all use shared storage. from here, i want to be able to merge the chunks or re-assemble them back to the final 10mb file.

  1. is this possible with PHP front end / back end? I have a REST API which supports POST and will inspect all post requests for $_FILE[] -- I'm unsure how this could be managed on the backend

  2. on the front end, when a user hits submit, the file starts to upload ... which means, if it's 10mb ... it'll fail -- SO, does this mean i need some fancy javascript to do the chunking / form submission -- if so, how is that then structured / communicated so that the backend knows what to do ...


回答1:


This won't be possible with Javascript or PHP. Maybe with Flash or some other client technology.




回答2:


you could use this code:

<html>
<body>
  <label >
  <div align="right" >
  </div>
</label>
<form method="POST" action="">
    <table>
        <tr>
            <td>Here you Files to spilited</td>
            <td>Value</td>

        </tr>
        <tr>
            <td> For spilt Zip File to  paste Its Url</td>
            <td><input type="text" name="zipurl" /></td>

        </tr>
        <tr>
            <td>Spilt to Valume Size:  part</td>
            <td><input type="text" name="spitsize" value="50"/></td>

        </tr>
        <tr>
            <td>Mega bite (1 On/ 0 off) </td>
            <td><input type="text" name="megabyte" value="1"/></td>

        </tr>
        <tr>
            <td>Kilo Byte (1 On/ 0 off)</td>
            <td><input type="text" name="kilobyte" value="0"/></td>

        </tr>
    </table>
    <input type="submit" name="ssurlsubmit" value="submit" />
    </form>



</body>
<?php 
$mypath = getcwd();
  $mypath = preg_replace('/\\\\/', '/', $mypath);

$targetfolder = $mypath.'/tmp';
echo 'targetfolder is:   '.$targetfolder.'<br />';
    if (!is_dir("$mypath/tmp")) {
mkdir("$mypath/tmp");
echo 'Dir'.$targetfolder.'created'.'<br />';

} 

$targetfolder = $mypath.'/tmp'.'<br />';

/*
if(!file_exists($targetfolder)) {
    if(mkdir($targetfolder)) {
        echo "Created target folder $targetfolder".br();
    }
    else{
    echo '$targetfolder can not be created'.br();
}
}else
{
echo $targetfolder.'$targetfolder exist '.br();
}
*/
?>
</html>

<?php
/*
 --------------------------------------------------
 filesplit.php HJSplit compatiple PHP file splitter
 --------------------------------------------------

 File name: 
    filesplit.php                
 Author: 
    Umit Tirpan 2007-03-22          
    Umit Tirpan 2008-03-12 [remote file support added]         
 Author's Website: 
    http://forum.iyinet.com/

 Description:
    This PHP script, splits the files into smaller pieces in binary.
    It is compatiple with HJSplit, so you can re-join splitted files using HJSplit's Join utility.
    It can split a file up to 999 pieces.

 What is the use, why will I use this?
    Some webmasters do not have shell access to their websites. This prevents them splitting big files, ie. MySQL backups, into smaller files. Splitting a 200Mb file into 10 x 20Mb file may be easy on webmaster to download in pieces. 

 How to run:
    Make sure your webserver has write permission on the target folder.
    Edit variables. 
    Upload (ftp) this file to your webserver or run on your PC.
    For security reason, filename is hardcoded. You can modify code to accept $_GET['filename']

 Run with your favorite browser.
    http://www.<your-web-site>.com/filesplit.php
 Or run on shell (ie. ssh)
    php filesplit.php

 It is Ok to share and modify this code and use in/with your applications. No restrictions.

 */




// ---------------------------
// Edit variables (3 variables)
// ---------------------------

// File to split, is its not in the same folder with filesplit.php, full path is required.

global $ssurl,$filename;
if (isset($_POST['zipurl'])) {
$url=$_POST['zipurl'];
$filename=$_POST['zipurl'];
echo "URL:".$url.'<br />';
}

//  $filename = "http://www.iyinet.com/my-big-file.zip"; 

// Target folder. Splitted files will be stored here. Original file never gets touched.
// Do not append slash! Make sure webserver has write permission on this folder.
$mypath = getcwd();
  $mypath = preg_replace('/\\\\/', '/', $mypath);

$targetfolder = $mypath.'/tmp';
echo 'targetfolder is:   '.$targetfolder.'<br />';
    if (!is_dir("$mypath/tmp")) {
mkdir("$mypath/tmp");
echo 'Dir'.$targetfolder.'created'.'<br />';

} 

$targetfolder = $mypath.'/tmp';

// File size in Mb per piece/split. 
// For a 200Mb file if piecesize=10 it will create twenty 10Mb files
$piecesize = 50; // splitted file size in MB
if (isset($_POST['spitsize'])) {
$piecesize=$_POST['spitsize'];

}

//$piecesize = 10; // splitted file size in MB



// ---------------------------
// Do NOT edit this section
// ---------------------------

$buffer = 1024;
if (isset($_POST['megabyte'])and $_POST['megabyte']=='1') {
//$piece=$_POST['megabyte'];
$piece = 1048576*$piecesize;

}
if (isset($_POST['kilobyte']) and $_POST['kilobyte']==1) {
//$piece=$_POST['kilobyte'];
$piece = 1024*$piecesize;

}
//$piece = 1048576*$piecesize;
$current = 0;
$splitnum = 1;

if(!file_exists($targetfolder)) {
    if(mkdir($targetfolder)) {
        echo "Created target folder $targetfolder".br();
    }
    echo '$targetfolder folder exist'.br();
}

if(!$handle = fopen($filename, "rb")) {
    die("Unable to open $filename for read! Make sure you edited filesplit.php correctly!".br());
}

$base_filename = basename($filename);
/*
$ss=substr($base_filename,-4);
echo substr($base_filename,-4).'<br />';
if ($ss=='.zip') 
 { 
 $end='.zip';

 $base_filename= removeFromEnd($base_filename, '.zip');
 }
 */
$piece_name = $targetfolder.'/'.$base_filename.'.'.str_pad($splitnum, 3, "0", STR_PAD_LEFT). $end;
if(!$fw = fopen($piece_name,"w")) {
    die("Unable to open $piece_name for write. Make sure target folder is writeable.".br());
}
echo "Splitting $base_filename into $piecesize Mb files ".br()."(last piece may be smaller in size)".br();
echo "Writing $piece_name...".br();
while (!feof($handle) and $splitnum < 999) {
    if($current < $piece) {
        if($content = fread($handle, $buffer)) {
            if(fwrite($fw, $content)) {
                $current += $buffer;
            } else {
                die("filesplit.php is unable to write to target folder. Target folder may not have write permission! Try chmod +w target_folder".br());
            }
        }
    } else {
        fclose($fw);
        $current = 0;
        $splitnum++;
        $piece_name = $targetfolder.'/'.$base_filename.'.'.str_pad($splitnum, 3, "0", STR_PAD_LEFT);
        echo "Writing $piece_name...".br();
        $fw = fopen($piece_name,"w");
    }
}
fclose($fw);
fclose($handle);
echo "Done! ".br();
exit;

function br() {
    return (!empty($_SERVER['SERVER_SOFTWARE']))?'<br>':"\n";
}
function removeFromEnd($string, $stringToRemove) {
     $stringToRemoveLen = strlen($stringToRemove);
     $stringLen = strlen($string);

     $pos = $stringLen - $stringToRemoveLen;    

     $out = substr($string, 0, $pos);

     return $out;
 }
?> 

for test check this address:

http://restss2.frogcp.com/tmp/filesplit%2520%282%29.php



来源:https://stackoverflow.com/questions/5700307/splitting-a-file-for-upload-via-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!