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

Mysql Interface

Posted in Programming by gosukiwi on November 19, 2006

Well, i will now post the basic differences between PHP and Perl and Mysql

Mysql is a very used sql database, used to store massive ammount of data.

Mysql is supported by alot of free hosting services, like awardspace.com, freewebsitehosting.net, etc

PHP and Perl are also supported by almost all hosting services.

I will show you the basic Mysql Interface with those two languages

That would be connect to a database, select a dabatase, execute queries (insert, delete, update), and numrows.

Connect to a database:

In PHP:

 

mysql_connect(‘host’,’user’,’pass);
mysql_select_db(‘database’)

In Perl:

 

use Mysql;
$db = Mysql->connect(‘host’, ‘database’, ‘user, ‘pass’);

 

Not very hard right? The main difference is that Perl uses a OOP interface to connect, so you will have to access the methods trought $db

 

Executing a query

In PHP:

mysql_query(“SELECT * FROM mytable”);

In Perl:

$db->query(“SELECT * FROM mytable”);

Not much difference, in perl is a little bit shorter, but basically the same.

Select Query

In PHP:

$q = mysql_query(“SELECT title, content FROM news ORDER BY id DESC LIMIT 3”);

In Perl:

my $q = $db->query(“SELECT title, content FROM news ORDER BY id DESC LIMIT 3”);

Still, not much difference, we just assigned that query to a variable.

Display Content

In PHP:

$q = mysql_query(“SELECT title, content FROM news ORDER BY id DESC LIMIT 3”);
while($r = mysql_fetrch_array($q))
{
echo ‘Title: ‘.$r[‘title’].'<br />’.Content: ‘.$r[‘content’];
}

In Perl:

 

 

 

 

$q = $db->query(“SELECT title, content FROM news ORDER BY id DESC LIMIT 3”);
while(%r = $q->fetchhash)
{
print ‘Title: ‘.$r{‘title’}.'<br />Content: ‘.$r{‘content’};
}

Now, in PHP, we use an array with keys and values to hold the data, but in Perl, arrays are only numeric, and we have to use hashes to have a key and value interface. We could have used fetcharray instead of fetchhash but thebn, we would have to use $r[1] instead of $r{‘content’}. That does not happen with PHP, because PHP arrays can have only values with numeric keys, or act like Perl hashes or Python dictionaries.

 

 

 

 

Updating and deleting

In PHP:

$id = $_GET[‘id’];
$uq = mysql_query(“UPDATE news SET content=’$content’ WHERE id=’$id’ LIMIT 1”);
$dq = mysql_query(“DELETE FROM news WHERE id=’$id’ LIMIT 1”);
if($uq && $dq)
{
echo ‘Queries executed successfully’;
}
else
{
echo ‘There has been an error’;
}

In Perl:

use CGI’:all’;
my $id = param(‘id’);
my $uq = $db->query(“UPDATE news SET content=’$content’ WHERE id=’$id’ LIMIT 1”);
my $dq = $db->query(“DELETE FROM news WHERE id=’$id’ LIMIT 1”);
if($uq and $dq)
{
print ‘Queries executed successfully’;
}
else
{
print ‘There has been an error’;
}

Well, not many differences there, you can also use print in php, the ony differences between print and echo in PHP is that print prints only one line, and echo can print multiple lines.

Numbering Rows

In PHP

$q = mysql_query(“SELECT id FROM news”);
$r = mysql_num_rows($q);
print ‘Total Rows: ‘.$r;

 

 

In Perl

my $q = $db->query(“SELECT id FROM news”);
my $r = $q->numrows;
print ‘Total Rows: ‘.$r;

 

 

Not many differences too, in Perl, we refecence directly the query result to use the numrows method, in PHP we dont use a method, just a function, with the query as an argument.

We can also use PHP with an OOP mysql interface, but we have to make a class on our own.

Well, that’s all 😀 I hope it will be useful for you ^^

« Previous Page