Category: Scripts

Uploading Files with PHP

With many forms you may want to include some sort of upload of a file. There are many functions for files in PHP that you can use to do this.

Some things you may want to consider when uploading a file to your server:

  • File extension – what type of file is being uploaded.
  • File size – How big is the file, should the be a limit?
  • What to do on a successful upload.

Here is a basic form uploading to the file “upload.php”.

[code lang=”html”]


[/code]

Now on upload.php we need to have code that reacts to this form being submitted, other wise nothing will happen obviously.

[code lang=”php”][/code]

That would be our code for upload.php. When the form is submitted it will execute that code and perform the upload as long as it passes the tests.

You  can specify any directory, extensions, filesize limit you want of course. Enjoy.

Filed under: Scripts, TutorialsTagged with: , ,

Mysql Database Connect

To connect to a Mysql database you need a few things first.

Of course you need to have the database created with a name you make up as well as the username and password of the mysql account that can connect to that database. This is very simple if you are using web managing software such as Cpanel or DirectAdmin. Under the Mysql databases area you can do all of this quickly.

For many websites mysql connections are included in a common file such as “config.php”. Here is how a basic mysql connection looks:

[code lang=”php”]
$host = “localhost”;
$db_username = “USERNAME”;
$db_password = “PASSWORD”;
$db_name = “DATABASE”;

mysql_connect($host,$db_username,$db_password) or die(mysql_error());

// should there be any errors, we will receive them before reaching any further
// we have connected to the mysql server successfully, now let’s get into the database

mysql_select_db($db_name) or die(mysql_error());

// if the following returns no errors, you are now connected to your mysql database
[/code]

$host – The host that you are connecting to. Usually set to “localhost”.
$db_username – The username of the account that connects to the database specified.
$db_password – The password of the account that connects to the database specified.
$db_name – The name of the database you are connecting to. This is shown in your domain managing area.

This code will connect to a mysql database and if there are any errors reported it will end php and display the errors.

Filed under: Scripts, Tutorials, Web ProgrammingTagged with: ,