PDA

View Full Version : Renaming files before upload


christopherbrag
02-02-2003, 08:22 PM
Thank you for reading my post...


I have this script:

(It is used for uploading .jpg files to my server)

$filename = $query->param("upload");
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = $query->upload("upload");
open S, ">somedirectory/$filename";

binmode S;

while (<$upload_filehandle>){

print S;

}

close S;



It works by saving the file to my server with its original name. The problem is, I want to save the file with a different name, and when I replace "$filename" (on line: open S, ">somedirectory/$filename") with my own word, such as (file.jpg) The file does not upload to the server properly (It uploads the filename I specified, but the actual file is empty, and of course, will not work.)

If anyone has a solution to this, please let me know


Thank you very much!

mburkwit
02-06-2003, 05:19 AM
I want to know the answer also!

Grizz2
02-07-2003, 06:27 AM
Try this one and see if it works for you.

$upload = $query->param('upload');
if($upload ne "")
{
$type = $query->uploadInfo($upload)->{'Content-Type'};

## only allow "gif, jpg, jpeg" files
unless($type =~ /image\/[gif|jpg|jpeg]/i)
{
&error("Wrong file type.<br>Gif and Jpg files only.");
exit;
}

$upload =~ s/.*[\/\\](.*)/$1/;

## rename the file
$newfile = "somedirectory/somepic.jpg";

if(open(S, ">$newfile") || print "Error opening file. $!")
{
while($bytesread = read($upload,$buffer,1024))
{
print S "$buffer";
$sum_bytes += $bytesread;

## limit upload size (2 megs here)
if($sum_bytes > (1024 * 2000))
{
close(S);
unlink $newfile;
&error("File too big.<br>Max size=2 megs");
exit;
}
}
close(S);

}
}


sub error{

($error, @errorfields) = @_;
print<<"PrintTag";
<html>
<!---------------------------- you can edit between these comment tags ------>
<head><title>Error</title></head>
<body bgcolor="#FFFFFF">
<center><font size="+2" color="red"><b>Error</b></font><br>&nbsp;<br>
<font size="+2" color=#000080><b>$error</b></font><br>&nbsp;<br>
<font size="+2" color=#000080><b>Use your back button to try again.</b></font></center>
<!----------------------------- end of html edit -------------------------------->
</body>
</html>
PrintTag
}

Grizz2
02-07-2003, 06:35 AM
I forgot, you'll need to add Content type to the error sub if you haven't already in your script.

sub error{

($error, @errorfields) = @_;

print"Content-type: text/html\n\n";

print<<"PrintTag";
<html>
<!---------------------------- you can edit between these comment tags ------>
<head><title>Error</title></head>
<body bgcolor="#FFFFFF">
<center><font size="+2" color="red"><b>Error</b></font><br> <br>
<font size="+2" color=#000080><b>$error</b></font><br> <br>
<font size="+2" color=#000080><b>Use your back button to try again.</b></font></center>
<!----------------------------- end of html edit -------------------------------->
</body>
</html>
PrintTag
}