0

I am working with Google Chart API where I am converting the graph into image and then download it into my downloads folder. Now after download I want to rename the image file and move it to other directory for which I am using rename() function in PHP.

Now the problem I am facing is that the rename() function in PHP executes before I can execute the download image function (which is in javascript) and hence it gives me error showing "Specified file not found".

I have tried using PHP delay function usleep() and javascript function setTimeOut() and I also tried "time-wasting" loops. but didn't have any success. Can Someone please suggest me something I can implement to accomplish this.

This is my code:

/*Firstly there is the google line chart code */
In body I have:
<script type="text/javascript">
onload = function download_this(){
    grChartImg.DownloadImage('chart_div');
}
</script>

//PHP
<?
$changefrom = "C:/somelocation/Downloads/download" ;
     $changeto = __DIR__.'\mygraph';
     rename($changefrom, $changeto.'.png');
?>

This is grchartimg library which convert and download the graph image. I want the overwrite protection that is why I am using rename. Because after renaming I want to embed this image in PDF file.

Vikas Arora
  • 1,666
  • 2
  • 17
  • 38
  • 1
    Can you show us your code please? – Alessandro Minoccheri Aug 20 '13 at 11:30
  • Why do you even have to rename the image? Do you want some kind of "download only once" protection? If so, you should do that in code rather than by renaming files. – Mario Aug 20 '13 at 11:31
  • You could try some sort of long-polling to check the status of the script, then execute the download when it's done. – Ben Fortune Aug 20 '13 at 11:32
  • how can you download an image using javascript? I'm guessing you are just loading it in the browser? in your question it looks like you are saving it on the server side... – NDM Aug 20 '13 at 11:34
  • Check : http://stackoverflow.com/questions/1563187/check-if-download-is-completed – Bhavin Rana Aug 20 '13 at 11:35
  • 2
    PHP code is always going to execute before Javascript because it happens on the server rather than the client. Would have thought `setTimeout()` with an appropriate delay is probably the way to go. – Steve Chambers Aug 20 '13 at 11:36
  • You can't use `sleep` for that. It is not secure, you never know if the download will take longer or shorter time to finish. What you have to do is to trigger an ajax function after the download(you have how to know this, right?) and then in PHP you rename it. – DontVoteMeDown Aug 20 '13 at 11:37
  • 2
    You are mixing two different world - php & javascript. JS comes into picture after php's job is done. – Prasanth Aug 20 '13 at 11:46
  • @Mario I want to rename the file because: 1. The grchartimg library gives me base64 encoded file which I want to convert to PNG file. Renaming the file works. 2. As you said..I want the download only once protection. – Vikas Arora Aug 20 '13 at 11:47
  • @SteveChambers I used the setTimeout() function but it is not doing the desired job..PHP always overtakes it.. – Vikas Arora Aug 20 '13 at 11:50
  • @VikasArora This is always going to be the case. As far as timings are concerned, it would either have to be all in PHP or all in Javascript - since you are relying on Javascript to download the image it would have to be the latter. – Steve Chambers Aug 20 '13 at 12:00

1 Answers1

0

Why not just use PHP to download the image?

download image file from given google chart api url using php

header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="chart.png"');
$image = file_get_contents('http://chart.apis.google.com/chart?    chs=300x300&cht=qr&chld=L|0&chl=http%253A%252F%252Fnetcane.com%252Fprojects%252Fyourl%252F3');
header('Content-Length: ' . strlen($image));
echo $image;
Community
  • 1
  • 1
cfox
  • 431
  • 2
  • 6
  • 18
  • I am providing the data through database inside the google chart api using inline php. This code is good for downloading the image of a graph but I will not be able to provide the data fetched from the database. – Vikas Arora Aug 20 '13 at 12:06