头痛的PHP文件分割

php
  PHP做文件分割,听着不难,实现起来就麻烦了。自己想方法,听起来显然不太现实,所以只有靠搜索,搜索时间是非常长的,历时好几天。开始找的一堆内容压根看不懂,以后找到一个有用的,也没看懂,之后文件函数看了一堆,看了一天才基本明白。这速度没戏啊。
  代码是这样的:

$fp=fopen($old_file,'rb');
fseek($fp,$l*$start);
$tmp_file=fread($fp,$l*($end-$start));
fclose($fp);
$new= fopen($new_file,'a+');
fwrite($new,$tmp_file);
fclose($new);

  当然这是关键的几行,其中用了fopen,fseek,fread,fclose,fwrite五个函数,就是如此而已。如果弄清楚了这几个函数的用法,似乎是很简单。当然,写一个简单一次性分割还是可以的。
  从昨天开始,简单的测试后,就开始写了,半天程序基本成型了。改啊改,终于差不多了。从最初的1.00被我一直修改到1.20,就两天,说快也快,不过有几处压根就不知道怎么去验证,比如该输入数字的地方输入了字母怎么办。
  还有比较疑惑的就是密码问题,加入密码本来不是很麻烦的事,我设定了COOKIE,本来时间是有5天,可就是时不时出现要输入密码,这玩意头痛啊。后来知道是路径问题,就把文件名加到路径中,这可不好玩,因为这个做法是错误的。唉。
  原本7行的代码,却一下子变成226行,程序就这样,不过我写的估计没人看得下去,你们可以想象,手机上写代码是什么状况,没有编辑器,你很难清楚突然冒出来的一个”}”是属于哪个if的,你不要指望会出现多少空格,除了偶尔在echo中输出某个参数时留个空格,很少有空格,所以这样的代码在电脑上一般很难认,除非你是作者,当然作者也有糊涂的时候。以前写注释,这点不错,原本以为这个程序不怎么大,没想到也有7K。如果加一堆注释那得了。当然,开始没准备写注释,后面也就没写。一个不在手机写这东西的人,很难想象超详细注释的作用,因为一丁点屏幕你看不到多少字母,所以……
  例子一个https://sourceforge.net/projects/my-php/files/File-Splitter/(66月29更新)

20条评论在“头痛的PHP文件分割”

  1. 哈哈~在楼猪的时代,居然有66这个月份。位置在演示地址的后面。
    另外就是,1.21.php这个文档里面的日期居然出现穿越(1.20更新:2012.06.29.17.56.46)。
    还有就是,谢谢楼主哈~~ :mrgreen:

        1. 请你先考虑以个问题,我写这个的动机和目的,如果一个循环解决了,那么手机下载有会有一个等待过程,我要尽可能长时间把人留住,让手机慢下来,以前用得太快,受报应了的

  2. function split_go() {
    global $list, $options;
    for($i = 0; $i < count ( $_POST ["files"] ); $i ++) {
    $split_ok = true;
    $file = $list [$_POST ["files"] [$i]];
    $partSize = round ( ($_POST ["partSize"] [$i]) * 1024 * 1024 );
    $saveTo = ($options['download_dir_is_changeable'] ? stripslashes ( $_POST ["saveTo"] [$i] ) : realpath ( $options['download_dir'] )) . '/';
    $dest_name = basename ( $file ["name"] );
    $fileSize = filesize ( $file ["name"] );
    $totalParts = ceil ( $fileSize / $partSize );
    $crc = ($_POST ['crc_mode'] [$i] == 'file_read') ? dechex ( crc32 ( read_file ( $file ["name"] ) ) ) : (($_POST ['crc_mode'] [$i] == 'hash_file' && function_exists ( 'hash_file' )) ? hash_file ( 'crc32b', $file ["name"] ) : '111111');
    $crc = str_repeat ( "0", 8 – strlen ( $crc ) ) . strtoupper ( $crc );
    echo "Started to split file ” . basename ( $file [“name”] ) . “ parts of ” . bytesToKbOrMbOrGb ( $partSize ) . “, Using Method – Total Commander…”;
    echo “Total Parts: ” . $totalParts . ““;
    for($j = 1; $j <= $totalParts; $j ++) {
    if (file_exists ( $saveTo . $dest_name . '.' . sprintf ( "%03d", $j ) )) {
    echo "It is not possible to split the file. A piece already exists” . $dest_name . ‘.’ . sprintf ( “%03d”, $j ) . “ !”;
    continue 2;
    }
    }
    if (file_exists ( $saveTo . $dest_name . ‘.crc’ )) {
    echo “It is not possible to split the file. CRC file already exists” . $dest_name . ‘.crc’ . “ !”;
    } elseif (! is_file ( $file [“name”] )) {
    echo “It is not possible to split the file. Source file not found” . $file [“name”] . “ !”;
    } elseif (! is_dir ( $saveTo )) {
    echo “It is not possible to split the file. Directory doesn’t exist” . $saveTo . “ !”;
    } elseif (! @write_file ( $saveTo . $dest_name . “.crc”, “filename=” . $dest_name . “\r\n” . “size=” . $fileSize . “\r\n” . “crc32=” . $crc . “\r\n” )) {
    echo “It is not possible to split the file. CRC Error” . $dest_name . “.crc” . “ !”;
    } else {
    $time = filemtime ( $saveTo . $dest_name . ‘.crc’ );
    while ( isset ( $list [$time] ) ) {
    $time ++;
    }
    $list [$time] = array (“name” => $saveTo . $dest_name . ‘.crc’, “size” => bytesToKbOrMbOrGb ( filesize ( $saveTo . $dest_name . ‘.crc’ ) ), “date” => $time );
    $split_buffer_size = 2 * 1024 * 1024;
    $split_source = @fopen ( $file [“name”], “rb” );
    if (! $split_source) {
    echo “It is not possible to open source file ” . $file [“name”] . “ !”;
    continue;
    }
    for($j = 1; $j <= $totalParts; $j ++) {
    $split_dest = @fopen ( $saveTo . $dest_name . '.' . sprintf ( "%03d", $j ), "wb" );
    if (! $split_dest) {
    echo "Error openning file ” . $dest_name . ‘.’ . sprintf ( “%03d”, $j ) . “ !”;
    $split_ok = false;
    break;
    }
    $split_write_times = floor ( $partSize / $split_buffer_size );
    for($k = 0; $k < $split_write_times; $k ++) {
    $split_buffer = fread ( $split_source, $split_buffer_size );
    if (fwrite ( $split_dest, $split_buffer ) === false) {
    echo "Error writing the file ” . $dest_name . ‘.’ . sprintf ( “%03d”, $j ) . “ !”;
    $split_ok = false;
    break;
    }
    }
    $split_rest = $partSize – ($split_write_times * $split_buffer_size);
    if ($split_ok && $split_rest > 0) {
    $split_buffer = fread ( $split_source, $split_rest );
    if (fwrite ( $split_dest, $split_buffer ) === false) {
    echo “Error writing the file ” . $dest_name . ‘.’ . sprintf ( “%03d”, $j ) . “ !”;
    $split_ok = false;
    }
    }
    fclose ( $split_dest );
    if ($split_ok) {
    $time = filemtime ( $saveTo . $dest_name . ‘.’ . sprintf ( “%03d”, $j ) );
    while ( isset ( $list [$time] ) ) {
    $time ++;
    }
    $list [$time] = array (“name” => $saveTo . $dest_name . ‘.’ . sprintf ( “%03d”, $j ), “size” => bytesToKbOrMbOrGb ( filesize ( $saveTo . $dest_name . ‘.’ . sprintf ( “%03d”, $j ) ) ), “date” => $time );
    }
    }
    fclose ( $split_source );
    if ($split_ok) {
    if ($_POST[“del_ok”] && !$options[‘disable_deleting’]) {
    if (@unlink ( $file [“name”] )) {
    unset ( $list [$_POST [“files”] [$i]] );
    echo “Source file deleted.”;
    } else {
    echo “Source file isnot deleted!“;
    }
    }
    }
    if (! updateListInFile ( $list )) {
    echo “Couldn’t update file list. Problem writing to file!”;
    }
    }
    }
    }

    1. 可以先找个格式化工具,然后再看,手机写的代码就这样,能写出来就算奇迹了。当然你可以先测试下代码。

写下你最简单的想法