Php 读取文件夹中的多个csv文件

Php 读取文件夹中的多个csv文件,php,csv,directory,dir,Php,Csv,Directory,Dir,我需要帮助^^ 我需要的是脚本,它将打开并读取文件夹“csv/files”中的所有.csv文件,然后在“if”中执行该操作。嗯,当我只有一个文件时,它工作得很好。我设法建立了一些脚本,它是不工作,但没有“错误行”弹出了。。。 有人能看看我的代码,告诉我我做错了什么吗 <?php foreach (glob("*.csv") as $filename) { echo $filename."<br />"; if (($handle = fopen($filen

我需要帮助^^ 我需要的是脚本,它将打开并读取文件夹“csv/files”中的所有.csv文件,然后在“if”中执行该操作。嗯,当我只有一个文件时,它工作得很好。我设法建立了一些脚本,它是不工作,但没有“错误行”弹出了。。。 有人能看看我的代码,告诉我我做错了什么吗

<?php 
foreach (glob("*.csv") as $filename) {
    echo $filename."<br />";

    if (($handle = fopen($filename, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
            $url = $data[0];
            $path = $data[1];

            $ch = curl_init($url);
            $fp = fopen($path, 'wb');
            curl_setopt($ch, CURLOPT_FILE, $fp);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_exec($ch);
            curl_close($ch);
            fclose($fp);

        }
        fclose($handle);
    }
}
?>

这是多线程的主要候选,下面是一些代码:

<?php
class WebWorker extends Worker {
    public function run() {}
}

class WebTask extends Stackable {

    public function __construct($input, $output) {
        $this->input  = $input;
        $this->output = $output;
        $this->copied = 0;
    }

    public function run() {
        $data = file_get_contents($this->input);
        if ($data) {
            file_put_contents(
                $this->output, $data);
            $this->copied = strlen($data);
        }
    }

    public $input;
    public $output;
    public $copied;
}

class WebPool {
    public function __construct($max) {
        $this->max = $max;
        $this->workers = [];
    }

    public function submit(WebTask $task) {
        $random = rand(0, $this->max);

        if (isset($this->workers[$random])) {
            return $this->workers[$random]
                ->stack($task);
        } else {
            $this->workers[$random] = new WebWorker();
            $this->workers[$random]
                ->start();
            return $this->workers[$random]
                ->stack($task);
        }
    }

    public function shutdown() {
        foreach ($this->workers as $worker)
            $worker->shutdown();
    }

    protected $max;
    protected $workers;
}

$pool = new WebPool(8);
$work = [];
$start = microtime(true);

foreach (glob("csv/*.csv") as $file) {
    $file = fopen($file, "r");

    if ($file) {
        while (($line = fgetcsv($file, 0, ";"))) {
            $wid = count($work);
            $work[$wid] = new WebTask(
                $line[0], $line[1]);
            $pool->submit($work[$wid]);
        }
    }
}

$pool->shutdown();
$runtime = microtime(true) - $start;

$total = 0;
foreach ($work as $job) {
    printf(
        "[%s] %s -> %s %.3f kB\n", 
        $job->copied ? "OK" : "FAIL",
        $job->input, 
        $job->output, 
        $job->copied/1024);
    $total += $job->copied;
}
printf( 
    "[TOTAL] %.3f kB in %.3f seconds\n", 
    $total/1024, $runtime);
?>

这将创建最大数量的池线程,然后它将读取分号分隔的csv文件目录,其中每一行都是输入的;输出时,它将提交任务以读取输入,并将输出异步写入池中执行,同时主线程继续读取csv文件

我使用了最简单的输入/输出,因此您可以看到它在没有输入/输出的情况下是如何工作的

将任务提交到池时选择的工作线程是随机的,这可能不可取,可以检测工作线程是否忙,但这会使示例复杂化

进一步阅读:


也许脚本挂起了。。。读取多个文件并发送http请求是一个非常繁重且耗时的过程。请尝试将glob(*.csv)读入变量,然后循环。否则它将以无限循环结束。@SajithNair不,它不会:嗯,它将加载页面并停止,所以我认为不存在无限循环。我认为您应该参考这个