Quantcast

How to Upload File in PHP

Here is simple example about how to do a file upload using PHP. First let’s create the HTML page for upload form.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload Form</title>
</head>
<body>
<p>Simple Upload Form</p>
<form enctype="multipart/form-data" method="post" action="p_upload_file.php">
  <label>
    <input type="file" name="file_upload" id="file_upload" /><input name="submit" type="submit" value="Upload"/>
  </label>
</form>
<p>&nbsp;</p>
</body>
</html>
HTML Upload Form

HTML Upload Form

Save it with name upload.php. Next, we will create the PHP script called p_upload_file.php that handle the file upload. Here is the code.

1
2
3
4
5
6
7
8
9
10
11
12
<?php
  // script name: p_upload_file.php
 
  if ($_FILES["file_upload"]["error"] > 0) {
    echo "Error: " . $_FILES["file_upload"]["error"] . "<br />";
  } else {
    echo "Upload: " . $_FILES["file_upload"]["name"] . "<br />";
    echo "Type: " . $_FILES["file_upload"]["type"] . "<br />";
    echo "Size: " . $_FILES["file_upload"]["size"] . " bytes<br />";
    echo "Stored in: " . $_FILES["file_upload"]["tmp_name"];
  }
?>

On PHP there is $_FILES array that hold items uploaded to the particular script via the HTTP POST method. The $_FILES['file_upload'] above is associated with input tag name ‘file_upload’ on our previous HTML upload form.

Above code I’ve taken from http://www.w3schools.com/php/php_file_upload.asp. Every file upload on PHP, the file actually will be saved first on your server default temporary upload folder. On my machine the result would be like below.

Upload: Sakkc_logo.gif
Type: image/gif
Size: 8090 bytes
Stored in: C:\xampp\tmp\php20B0.tmp

I upload Sakkc_logo.gif file and after uploaded will be saved on C:\xampp\tmp\php20B0.tmp. You can move the uploaded file with move_uploaded_file() function on PHP. For example I want to copy the uploaded file to my web root folder, so the code will be like below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
  // script name: p_upload_file.php
 
  if ($_FILES["file_upload"]["error"] > 0) {
    echo "Error: " . $_FILES["file_upload"]["error"] . "<br/>";
  } else {
    echo "Upload: " . $_FILES["file_upload"]["name"] . "<br/>";
    echo "Type: " . $_FILES["file_upload"]["type"] . "<br/>";
    echo "Size: " . $_FILES["file_upload"]["size"] . " bytes<br/>";
    echo "Stored in: " . $_FILES["file_upload"]["tmp_name"] . "<br/><br/>";
 
    // move uploaded data to web root folder
 
    if (move_uploaded_file($_FILES["file_upload"]["tmp_name"], getcwd() . "/" . $_FILES["file_upload"]["name"])) {
      echo "Your uploaded file now moved to " . getcwd() . "/" . $_FILES["file_upload"]["name"];
    }
  }
?>

The sample result will be like below.

Upload: stock_raw.xls
Type: application/vnd.ms-excel
Size: 35328 bytes
Stored in: C:\xampp\tmp\phpD5B0.tmp

Your uploaded file now moved to C:\xampp\htdocs/stock_raw.xls

Now your file will be moved to your web root folder and you can process it for later purpose.

Bookmark and Share

Related posts:

  1. Simulate File Upload with PHP and curl
  2. Import Fingerprint Data ’1_attlog.dat’ to OrangeHRM Table ‘hs_hr_attendance’ using PHP
  3. How to Install Zend Framework?
  4. How to Use Zend Framework without Editing php.ini File?
  5. Convert VBScript File to EXE File using IExpress

3 Comments

  1. good blog keep it up.

  2. wiyono says:

    ini pake sql gak mas, kalau langsung di hosting??

    Ma kasihh..

  3. Sony AK says:

    @wiyono kalo yang ini gak pake SQL jadi ya bisa langsung di hosting.

Leave a Reply