php上传文件使用cURL导致无限循环

时间:2017-06-28 09:04:08

标签: php html curl

我使用HTML页面上的更新按钮来获取表格的值,然后我将这些值放在.csv文件上。 我想使用我现有的上传表单发布新生成的文件.csv。 这是更新按钮调用的代码的第一部分:

<?php

$PATH = "ABSOLUTE/PATH/TO/FOLDER";
if(isset($_POST)){
    if(isset($_POST['data'])){
        $Data = $_POST['data']['arrayHTML'];
        $id = $_POST['data']['id'];
        $filename = $PATH.$id."_".date('Ymd').".csv";
        $fileHandler = fopen($filename, "w");
        $splittedArray = array_chunk($Data, 4);
        fputcsv($fileHandler, array("EAN", "REC", "RETAIL", "BRAND DISCOUNT"), ";");
        foreach ($splittedArray as $line) {
            fputcsv($fileHandler, $line, ";");
        }
        fclose($fileHandler);
        echo "Done!";
    }
}

这是HTML上传表单:

<table border="1" align="center" width="800px;">
    <form METHOD="POST" ACTION="upload.php" enctype="multipart/form-data">
        <tr><td>
        <h5><h3><b>Sync file:</b></h3><input type="file" name="fichero">
        <input type=text name="lateupdate" value='.date("Y-m-d").' id="datepicker">
        <input type=hidden name="carpeta" value="ABSOLUTE/PATH/TO/UPLOAD/FOLDER/">
        <input type=hidden name="page" value="index.php"/>
        <input type=hidden name="id" value="'.$idprov.'"/>
        <input type="submit" name="submit" value="UPLOAD"/>


好的,生成csv文件后,我就像这样使用cURL:

$file_name_with_full_path = realpath($filename);
$cfile = curl_file_create($file_name_with_full_path);
$headers = array("Content-Type:multipart/form-data");
$target_url = "http://localhost:8000/upload.php";
$post = array('id'=>$id, 'file'=>$cfile, 'lateupdate'=>date('Y-m-d'), 'carpeta'=>'PATH/TO/FOLDER', 'page'=>'index.php');
//Iniating cURL
$ch = curl_init();
//setting the options
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);

我在我的localhost服务器上获得了一个infinte循环。

所以我尝试了:

exec("curl -X POST -F 'fichero=@$filename' -F 'page=index.php'\
      -F 'carpeta=/srv/http/TradeinnDev/killred/upload/'\ 
      -F 'lateupdate=".date('Y-m-d')."' \
      -F'id=$id' http://localhost:8000/upload.php"
    );

这是我的localhost服务器日志的输出:

php -S localhost:8000 -t /srv/http/
PHP 7.1.6 Development Server started at Wed Jun 28 10:00:35 2017
Listening on http://localhost:8000
Document root is /srv/http/
Press Ctrl-C to quit.
[Wed Jun 28 10:00:38 2017] ::1:54168 [200]: /index.php?id=1
[Wed Jun 28 10:00:38 2017] ::1:54174 [200]: /css/menu_style.css
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   878    0     0  100   878      0      8  0:01:49  0:01:41  0:00:08     0^C

我必须杀死它,因为TIME LEFT字段始终在重置。

感谢您的帮助

******************* EDIT *************************** ********

这是上传过程的奇怪行为:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   777    0     0  100   777      0    387  0:00:02  0:00:02 --:--:--   388
100   777    0     0  100   777      0    258  0:00:03  0:00:03 --:--:--   258
100   777    0     0  100   777      0    193  0:00:04  0:00:04 --:--:--   194
100   777    0     0  100   777      0    110  0:00:07  0:00:07 --:--:--     0
100   777    0     0  100   777      0     97  0:00:08  0:00:08 --:--:--     0
100   777    0     0  100   777      0     64  0:00:12  0:00:12 --:--:--     0
100   777    0     0  100   777      0     25  0:00:31  0:00:30  0:00:01     0
100   777    0     0  100   777      0     22  0:00:35  0:00:34  0:00:01     0
100   777    0     0  100   777      0     19  0:00:40  0:00:40 --:--:--     0
100   777    0     0  100   777      0     18  0:00:43  0:00:41  0:00:02     0
100   777    0     0  100   777      0     11  0:01:10  0:01:10 --:--:--     0

1 个答案:

答案 0 :(得分:0)

显然,在php内置webserver命令行的参数中传递php.ini文件是解决方案,如下所示:

php -S localhost:8000 -t /srv/http/TradeinnDev/killred -c /etc/php/php.ini

来自PHP的手册页:

--php-ini path|file
       -c path|file   Look for php.ini file in the directory path or use the specified file