From PHP to Perl


Uploading a file

Posted in Programming by gosukiwi on November 20, 2006

Hi all! I will now show you how to upload a file in PHP and Perl

First of all the HTML, is the same for both languages, you just have to change the action.

We will make an example upload script, assuming that we want to upload an avatar.

HTML:

<form action=”myscript.php” method=”post” enctype=”multipart/form-data”>
<table border=”0″>
<tr>
<td>Upload your avatar </td>
<td><input type=”file” name=”avatar” /></td>
</tr>
<tr>
<td> </td>
<td><input type=”submit” name=”Submit” value=”Upload” /></td>
</tr>
</table>
</form>

Remember to change the action field
In that HTML code, we make a form with a POST method, and a multipart/form-data encryption. Thats very important!, so do not change it.
Then we make a simple file input and a submit button.

Now, the code 🙂

In PHP:

<?php
if($_POST[‘Submit’]) // If the form is submitted
{
$avatar = $_FILES[‘avatar’][‘tmp_name’]; // Avatar temp name
$avatar_name = $_FILES[‘avatar’][‘name’]; // Avatar name
$ext = array(‘.gif’,’.jpg’,’.jpeg’,’.png’); // Array with allowed extensions
$my_avatar_name = ‘avatar.gif’; // This is the name that our avatar will have
$do = false; // Set the variable $do as false
foreach($ext as $e) // Loop trought the array
{
if(eregi($e, $avatar_name) // If it find the extension in the avatar name, it will stop looping
{
$do = true; // Set do to true
break; // Stop looping
}
}
if($do) // If $do is true
{
if(copy($avatar, ‘avatars/’.$my_avatar_name)) // Copy the avatar
{
echo ‘Avatar uploaded!’; // if it could copy, we show this message
}
else
{
echo ‘Error uploading avatar :(‘; // Else, if it did not copy, we will show this message
}
}
}
?>

In Perl:

#!/usr/bin/perl
use CGI’:all’; # We use all CGI methods
if(param(‘Submit’)) # If the form is submitted
{
my $avatar = upload(‘avatar’); # Avatar temp name
my $avatar_name = param(‘avatar’); # Avatar name
my @ext = qw/.gif .jpg .jpeg .png/; # Array with allowed extensions
my $my_avatar_name = ‘avatar.gif’; # This is the name that our avatar will have
my $do = 0; # Set the variable $do as false
foreach (@ext) # Loop trought the array
{
if($avatar_name =~ /\.$_$/i)) # If it find the extension in the avatar name, it will stop looping
{
$do = 1; # Set do to true
last; # Stop looping
}
}
if($do) # If $do is true
{
open(AVATAR, “>avatars/$my_avatar_name”); # We create a file to write
binmode AVATAR; # We set it to bin mode to make sure it work in UNIX systems
while(<$avatar>)
{
print AVATAR; # Print the content byte per byte
}
close AVATAR; # Close the file
print header,start_html(); # Printe the html header and start with the html, header and body tags
if(-e “avatars/$my_avatar_name”)
{
print ‘Avatar uploaded!’; # if it could copy, we show this message
}
else
{
print ‘Error uploading avatar :(‘; # Else, if it did not copy, we will show this message
}
print end_html; # Close the body and html tags
}
}

Well, the codes are self-explained, i hope you all find it useful ^-^

Bye!

Advertisements

7 Responses to 'Uploading a file'

Subscribe to comments with RSS or TrackBack to 'Uploading a file'.

  1. jason said,

    Is this perl script good for video uploads?

  2. assasiner said,

    this is a very basic and simple upload script, i dont reccomend using it for video uploads, but you certanly can

  3. sda said,

    ad

  4. Samuel L. said,

    I can tell that this is not the first time at all that you write about this topic. Why have you decided to write about it again?


  5. Very good article I like your blog carry on the good articles

  6. Mike said,

    I get this error when I try to upload it..

    Parse error: syntax error, unexpected ‘{‘ in /home/zoovix/public_html/testsocial/uploadavatar.php on line 12

    How do I fix this?

  7. bug said,

    …becase a close parenthesis is missing on line 11.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: