0
<?php
// open the current directory
$dhandle = opendir('.');
// define an array to hold the files
$files = array();

if ($dhandle) {
   // loop through all of the files
   while (false !== ($fname = readdir($dhandle))) {
      // if the file is not this file, and does not start with a '.' or '..',
      // then store it for later display
      if (($fname != 'index.htm') && ($fname != 'torcache.php')&& ($fname != 'error_log') && (substr($fname, 0, 5) != 'other') && (substr($fname, 0, 2) != 'dd') &&
          ($fname != basename($_SERVER['PHP_SELF']))) {
          // store the filename
          $files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname;
      }
   }
   // close the directory
   closedir($dhandle);
}


function curl_upload($url,$fileFormAttribute,$file){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch,CURLOPT_REFERER, 'https://torcache.net/');
        curl_setopt($ch,CURLOPT_ENCODING,"gzip");
        curl_setopt($ch, CURLOPT_POST, true);
        $post = array($fileFormAttribute=>"@".$file);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $response = curl_exec($ch);
        return $response;
}
foreach( $files as $fname )
{
error_reporting(E_ALL);


$upload_result = curl_upload('http://torcache.net/autoupload.php','torrent','public_html/download_folder/'.$files[0]);
}
var_dump($upload_result);
?>

当我运行这个脚本时,它只发布数组中的第一项,而不是循环遍历整个数组我做错了什么?我想让它遍历目录中的所有文件,然后将它们发布到 torcache,然后将字符串还给我;

4

1 回答 1

6

不应该.$files[0]$fname倒数第四行吗?像这样:

$upload_result = curl_upload('http://...','torrent','public_html/download_folder/'.$fname);
于 2012-06-15T20:00:17.220 回答