1

I have a problem. I'm downloading csv file with url and using curl library, but problem is that, this csv file is not right, so I need to change semicolon (;) on comma (,).

if ($fileHandle) {
    $url = $this->csvUrl;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_FILE, $fileHandle);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $string = fopen($localPath, 'r');
    $data = stream_get_contents($string, -1, 30);
    $data = str_replace(';', ',', $data);
    curl_exec(???);
    curl_close(???);
    chmod($localPath, 0755);

    return $localPath;
}

I want to write changed content on this path.

Dharman
  • 30,962
  • 25
  • 85
  • 135
qrsl
  • 29
  • 7
  • You could use [str_getcsv](https://www.php.net/manual/en/function.str-getcsv.php) and pass the second argument as `';'` – Dharman Mar 27 '19 at 21:32
  • @Dharman excuse me? – qrsl Mar 27 '19 at 21:33
  • You didn't get me. I'm writing csv file, but only that is with semicolons, I just want to write edited one, with commas. – qrsl Mar 27 '19 at 21:37
  • It's not the problem mate, its changing content. My Question is how to pass changed one content into csv not downloaded one. – qrsl Mar 27 '19 at 21:40
  • Then to save a file to hard disk you need [`file_put_contents`](https://www.php.net/manual/en/function.file-put-contents.php) – Dharman Mar 27 '19 at 21:42
  • Sorry but I still can't understand. – qrsl Mar 27 '19 at 21:48
  • are you asking how to save the changed CSV back to the server where you downloaded it from? – ADyson Mar 27 '19 at 21:49
  • 1
    Same here... Could you write at least a paragraph explaining the expected behaviour? – Dharman Mar 27 '19 at 21:50
  • No I'm downloading CSV file, and I want change dynamically content, semicolons with commas and save it to my local folder – qrsl Mar 27 '19 at 21:52
  • if you want to save it to the local folder then use file_put_contents() as already mentioned. Google "php save data to disk" and you'll get plenty of examples. – ADyson Mar 27 '19 at 21:54
  • I still cant figure out what the hell is going on. I want to curl_exec and then save file, I don't even know what to write in curl_exec , cause it needs type of resource, I guess I need to pass $ch,but it won't work – qrsl Mar 27 '19 at 21:59
  • "won't work" means what? You get an error? If so please tell us what the error is, otherwise we can't help you – ADyson Mar 27 '19 at 22:02
  • I'm also creating filename in __construct, so I can't pass the filename – qrsl Mar 27 '19 at 22:03
  • P.S. yes you should be passing `$ch` into curl_exec - the [manual](https://www.php.net/manual/en/function.curl-exec.php) makes this clear: "Parameters: "ch" - A cURL handle returned by curl_init()." – ADyson Mar 27 '19 at 22:03
  • 1
    "I'm also creating filename in __construct, so I can't pass the filename" ...what do you mean? If you want to save the file to disk you'll need a filename. And where is __construct, and how does it relate to your code above? You realise we can only see what is written above in your question? We can't read your screen, or your disk, or your mind. So talking about your code as if we can see it is not very much use. You need to provide full details, or else it will not make any sense to us. Thanks – ADyson Mar 27 '19 at 22:05

0 Answers0