duandianwen1723
duandianwen1723
2017-06-28 09:04

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

已采纳

i'm using an update button on an HTML page to get the values of a table and then i'm just putting those values on a .csv file. I want to use my already existing upload form to post the newly generated file .csv. This is the first part of the code called by the update button:

<?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!";
    }
}

This is the HTML upload form:

<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"/>


Ok so, once the csv file generated, i'm using cURL like this:

$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);

And i'm obtaining an infinte loop on my localhost server.

So i tried with:

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"
    );

And this is the output of my localhost server log:

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

I have to kill it as the TIME LEFT field is always reseting.

Thank you for your help

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

This is the weird behaviour of the uploading proccess:

  % 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条回答

  • dtihe8614 dtihe8614 4年前

    Apparently, passing the php.ini file in argument of the php built-in webserver command line is the solution, like this:

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

    From the man page of PHP:

    --php-ini path|file
           -c path|file   Look for php.ini file in the directory path or use the specified file
    
    点赞 评论 复制链接分享