使用PHP在服务器上重新加入拆分文件

时间:2012-08-19 23:35:39

标签: php

我想知道是否可以执行以下操作:

  • 将本地计算机上的文件拆分为多个部分,以便上传 它们到FTP服务器上(避免这种方式限制文件大小)
  • 将所有部分上传到我的网络服务器
  • 使用PHP脚本将它们一起重新加入一个文件

我知道HJSplit,但它是相反的情况(在服务器上拆分然后在本地计算机上重新加入)

有没有人知道任何可能的解决方案?

2 个答案:

答案 0 :(得分:1)

This site claims to have some packages for split and then join.
但是我不确定它们是否有效。(未尝试)

答案 1 :(得分:-1)

这可能会导致分裂部分:

    <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;
 }
?> 

你可以通过以下方式测试它:

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