1

I have a curl request to upload file and I want to show the progress. In progress function I am trying to run javascript, but javascript code isn't getting executed.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'some url');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array($this, 'progress'));
curl_setopt($ch, CURLOPT_NOPROGRESS, false); // needed to make progress function work
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => $data));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$out = curl_exec($ch);

public function progress($resource,$download_size, $downloaded, $upload_size, $uploaded)
{
    $percentage = 0;
    if($download_size > 0)
        $percentage = $downloaded / $download_size  * 100;
    if($upload_size > 0)
        $percentage = $uploaded / $upload_size  * 100;
    $percentage = floor($percentage);
    echo "<script>alert('asdas')</script>;"; // <----not executing
}

The alert is not executing, what could be the reason ?

Shoyeb Sheikh
  • 2,659
  • 2
  • 10
  • 19
  • How are you executing this script - did you call it in your browser directly, or in the background via AJAX? It might simply be the output buffer (although then you should rather get several alerts all at the same time, when the script finishes), https://stackoverflow.com/questions/8882383/how-to-disable-output-buffering-in-php has some more explanation on that. – CBroe Oct 05 '21 at 06:10
  • @CBroe its a ajax request, sorry I am not aware of php output buffering. I am not getting a single alert as of now, why is that ? – Shoyeb Sheikh Oct 05 '21 at 06:13
  • Probably because it is an AJAX request. You need to show us how you handled the response. – CBroe Oct 05 '21 at 06:15
  • it should add the script to html body but seems like it is adding the echo statement to buffer, correct me if I am wrong @CBroe – Shoyeb Sheikh Oct 05 '21 at 06:25
  • I like your questions - i think this is a good answer to your question [PHP cURL download progress using jquery](https://stackoverflow.com/a/41423055/2139671) – Roman Oct 05 '21 at 06:31
  • 1
    @Roman I thought of it but isnt it slower than my approach. my approach seems impossible anyway, you have to query for session variable every interval in the link you mentioned. – Shoyeb Sheikh Oct 05 '21 at 06:40

0 Answers0