拆分文件以通过php上传

时间:2011-04-18 08:29:48

标签: php curl

在php.ini和nginx配置中,可以设置限制,限制请求可以包含的总大小。例如1mb。

因此,根据该要求,如果用户想要上传10mb的文件,我想将文件拆分为1mb块并将其发送到多个后端应用程序服务器,在那里它们都使用共享存储。从这里开始,我希望能够合并块或将它们重新组装回最终的10mb文件。

  1. 这可能是PHP前端/后端吗?我有一个支持POST的REST API,并将检查$ _FILE []的所有发布请求 - 我不确定如何在后端管理它

  2. 在前端
  3. ,当用户点击提交时,文件开始上传...这意味着,如果它是10mb ......它将失败 - 那么,这是否意味着我需要一些花哨的javascript进行分块/表单提交 - 如果是,那么如何进行结构化/沟通,以便后端知道该做什么......

2 个答案:

答案 0 :(得分:1)

使用Javascript或PHP无法实现这一点。也许使用Flash或其他一些客户端技术。

答案 1 :(得分:0)

你可以使用这段代码:

<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

相关问题