Mar 9

一段自己写的PHP代码

admin , 14:24 , 我的程序 , 评论(0) , 引用(0) , 阅读(123) , Via 本站原创 | |
自动上传文件到网络硬盘,并自动更新下载记录



#!/usr/bin/php
<?php

/**
* @author
* @copyright 2010
*/


$file = $argv[1];
//$url = upload($file);
echo $url;

//$url = 'http://jumbofiles.com/ene2bfph2mps';
//$file_path = get_file_path($url);

//echo $file_path;

//insert_db($file,$url);

update_status();

function update_status(){
  $mysqli = new mysqli('localhost','ssss','sssss','jumbofiles');
  $sql = 'select * from xxx';
  $res = $mysqli->query($sql);
  if($res){
    while($row = $res->fetch_array()){
      $url = $row['url'];
      echo "$url \r\n";
      $file_path = get_file_path($url);
      echo "$file_path \r\n";
    }
  }
}



function insert_db($file,$url){
  $mysqli = new mysqli('localhost','root','root','jumbofiles');
  $filesize = filesize($file);
  $filename = basename($file);
  $filename = $mysqli->escape_string($filename);
  $url = $mysqli->escape_string($url);
  
  $createtime = time();
  $sql = "INSERT INTO `jumbofiles`.`xxx` (`id`, `url`, `filename`, `filesize`, `createtime`) VALUES (NULL, '$url', '$filename', '$filesize', '$createtime');";
  $r = $mysqli->query($sql);
  if($r){
    echo "Insert " .$url . " OK \r\n";
  }else{
    echo "Insert " .$url . " Error \r\n";
  }
  $mysqli->close();
}



function get_rand($url)
{
    $content = file_get_contents($url);
    if ($content == '') {
        echo "Get content error \r\n";
        exit();
    }
    $rand_file = '/tmp/' . rand();
    file_put_contents($rand_file,$content);
    $file = file($rand_file);
    $rand = '';
    foreach ($file as $value) {
        $str = trim($value);
        if (strstr($str, 'name="rand"')) {
        //  echo $str;
            $rand = substr($str, 41, 8);
        }
    }
    unlink($rand_file);
    if ($rand != '') {
      echo "rand is $rand \r\n";
        return $rand;
    } else {
        return false;
    }
}

function get_file_path($url)
{
    $parse_url = parse_url($url);
    $sid = $parse_url['path'];
    $id = substr($sid, 1);
    $rand = get_rand($url);
    if(!$rand){
    echo "Get rand error \r\n";
    exit();
  }
    $rand_file = '/tmp/' . rand();
    $cmd = 'curl -d "op=download2" -d "id=' . $id . '" -d "rand=' . $rand . '" ' .
        $url . '> ' . $rand_file;
//   echo $cmd;
    echo "\r\n";
    exec($cmd);
    // get file path
    $file = file($rand_file);
    foreach ($file as $value){
    $str = trim($value);
    if(strstr($str,'<FORM ACTION=')){
      $file_path = substr($str,14);
      //$file_path = substr($file_path,-15);
      $file_path = str_replace('" method="get">','',$file_path);
      $file_path = trim($file_path);
      unlink($rand_file);
      return $file_path;
    }
  }
  unlink($rand_file);
  return false;  

}


function upload($file)
{
    if (!file_exists($file)) {
        echo 'File not exits';
        echo "\r\n";
        exit();
    }
    $rand = rand();
    $cmd = 'curl -F "upload_type=file" -H "Referer: http://jumbofiles.com/" -F "file_0=@' .
        $file . '" http://www4.jumbofiles.com/cgi-bin/upload.cgi -D /tmp/' . $rand;
    //  echo $cmd;
    //  echo "\r\n";
    exec($cmd);
    $header_file = "/tmp/$rand";
    $header = file_get_contents($header_file);
    if ($header == "") {
        echo 'Upload file error';
        echo "\r\n";
        exit();
    }
    //  echo $header;
    $header = file($header_file);
    $result_url = "";
    foreach ($header as $value) {
        $str = trim($value);
        if (substr($str, 0, 8) == "Location") {
            $url = substr($str, 9);
            $parse_url = parse_url($url);
            $query = $parse_url['query'];
            if (strstr($query, '&st=OK')) {
                parse_str($query, $output);
                $fn = $output['fn'];
                $result_url = 'http://jumbofiles.com/' . $fn;
            }
        }
    }
    //  unlink($header_file);
    if ($result_url == '') {
        return false;
    } else {
        return $result_url;
    }

}

?>
Tags:
发表评论

昵称

网址

电邮

您也可用OpenID登入:
打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]