无法将上传的图像写入文件

时间:2018-02-01 17:40:13

标签: image perl

我正在尝试将动画gif保存到文件位置。这是我目前的代码:

# create folder location and file path name
my $dn="cmp".int(rand(10));
my $fn="ca".int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).".gif";
while (-f "$datapath/$dn/$fn") { $fn="ca".int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).".gif"; }

# open the uploaded image for saving to a file
open (IMAGE,$insfn{'attachment'});
binmode(IMAGE);

# open the file path for writing the image to
open (OUTPUT,"$datapath/$dn/$fn");
binmode(OUTPUT);

# write the image to the file
my $buf;
my $bufSize=4096;
while(read(IMAGE,$buf,$bufSize)) { print OUTPUT $buf; }

由于某种原因,它没有将图像保存到文件中。

不确定过去这一点该做什么我对perl很新。

1 个答案:

答案 0 :(得分:2)

您尚未打开OUTPUT进行书写。

替换

open (OUTPUT,"$datapath/$dn/$fn");

open (OUTPUT, ">", "$datapath/$dn/$fn");

和Perl将创建该文件,如果它已经存在则截断它,并允许写入它。

Relevant documentation.