Not able to copy the file in perl cgi script

Hello experts,

I am facing an very typical problem and hope the issue can be solved.

I have a page download.cgi in /cgi-bin folder.



use CGI;
use CGI::Carp qw ( fatalsToBrowser );  
use File::Copy

copy("C:\\Program Files\\Apache Software Foundation\\Apache2.2\\cgi-bin\\tcstemp\\$reportname","H:\\") or die("cannot copy file... giridhar $!");

print "<center><table border=0>";
print "<tr><td aligh=left>";
print qq~<a href="H:\\$report_name"><img src="/images/xlsx.png" width=40 height=40></a>~;
print qq~<a href="C:\\reports\\$report_name"><img src="/images/csv.jpg" width=40 height=40></a>~;
print "</td></tr>";
print "</table></center>";

But I am not able to copy the file to other drive( in windows).
I am able to run the cgi file in browser and it is showing an error message

Software error:
Cannot copy file at C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/tcstemp/temp_final_report.cgi line 315.

I have tried multiple ways to download the file but none of them worked.

Instead of copy the file to other drive I gave the file name directly in the link to download but no luck.

And also the below way calling perl script to cgi script to copy the file to another directory. But still no luck

use CGI;
use CGI::Carp qw ( fatalsToBrowser );  
use File::Copy

`perl filecopy.pl $reportname` or die("Cannot copy file");

print "<center><table border=0>";
print "<tr><td aligh=left>";
print qq~<a href="H:\\$report_name"><img src="/images/xlsx.png" width=40 height=40></a>~;
print qq~<a href="C:\\reports\\$report_name"><img src="/images/csv.jpg" width=40 height=40></a>~;
print "</td></tr>";
print "</table></center>";

filecopy code

use File::Copy;
use strict;
use warnings;


my $report_name = $ARGV[0];

copy("C:\\Program Files\\Apache Software Foundation\\Apache2.2\\cgi-bin\\tcstemp\\$report_name","H:\\") or die("cannot copy file... giridhar $!");

Could anyone please let me know what went wrong....

Tons of thanks....!!!

Did you try 'use Win32' ?

Win32::CopyFile(FROM, TO, OVERWRITE)
1 Like

But I am receiving an error message.

Win32::CopyFile("C:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin\tcstest\$report_name", "H:\$report_name","H:\$report_name") or die("Cannot copy file");

The third argument is not required or can be set to 0.

Please check the correct syntax here

Win32::kernel32 - search.cpan.org

1 Like

Many thanks Raja...

the file got copied but copied with 0 bytes. I am not able to open the file
and had an error message while opening the copied file

Excel cannot open the file filename.xlsx because the file format or 
file extension is not valid. Verify that the file has not been corrupted 
and that the file extension matches the format of the file

Could you please let know what can be the possible mistake...

Thanks in advance...

Also attaching the original script....

Not sure. May be try giving only the destination path instead of the complete file name in the "to" argument.

Raja.... Could you please have a look towards the cgi script and let me know if anything went wrong...

I think you can simply use "/" instead of "\\". Also if you are using File::Copy, you might want to try

syscopy("C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/gnsncc/$report_name","C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/gnsncc/$report_name") or die("cannot copy file... giridhar $!");