In this page it is presented the code for a Simple PHP Upload script.
This script can be used to upload images, or other file types on your server. It contains the HTML form with the field for upload file, and the PHP code that checks the file type (its extension), file size, width and height for images, and performs the upload if no errors.
You can set in the script the allowed file types (allowed extensions), maximum allowed file size and maximum Width and Height for images (see the comments in code).
- The files are saved in the "upload" directory (or the directory which you add to $uploadpath variable). This directory must be created in the same place in which you add the php file with this script. The PHP must have writable permissions in this directory (CHMOD 0755 [or 0777]), to can save the files.
After the file is successfully uploaded, the script will output the URL address of the file saved on sarver.

- You can download the script from this link: PHP Upload Script, or copy the code below (and create the "upload/" folder on your server).

Code for PHP Upload Script

<?php
// Simple PHP Upload Script:  http://coursesweb.net/php-mysql/

$uploadpath = 'upload/';      // directory to store the uploaded files
$max_size = 2000;          // maximum file size, in KiloBytes
$alwidth = 900;            // maximum allowed width, in pixels
$alheight = 800;           // maximum allowed height, in pixels
$allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png');        // allowed extensions

if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) {
  $uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);       // gets the file name
  $sepext = explode('.', strtolower($_FILES['fileup']['name']));
  $type = end($sepext);       // gets extension
  list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']);     // gets image width and height
  $err = '';         // to store the errors

  // Checks if the file has allowed type, size, width and height (for images)
  if(!in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';
  if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB.';
  if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<br/>The maximum Width x Height must be: '. $alwidth. ' x '. $alheight;

  // If no errors, upload the image, else, output the errors
  if($err == '') {
    if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) { 
      echo 'File: <b>'. basename( $_FILES['fileup']['name']). '</b> successfully uploaded:';
      echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';
      echo '<br />Size: <b>'. number_format($_FILES['fileup']['size']/1024, 3, '.', '') .'</b> KB';
      if(isset($width) && isset($height)) echo '<br/>Image Width x Height: '. $width. ' x '. $height;
      echo '<br/><br/>Image address: <b>http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath.'</b>';
    }
    else echo '<b>Unable to upload the file.</b>';
  }
  else echo $err;
}
?> 
<div style="margin:1em auto; width:333px; text-align:center;">
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data"> 
  Upload File: <input type="file" name="fileup" /><br/>
  <input type="submit" name='submit' value="Upload" /> 
 </form>
</div>

• If the file /image is successfully uploaded, the script will output a set of data like these:
File: image.jpg successfully uploaded:
File type: image/jpeg
Size: 16.545 KB
Image Width x Height: 300 x 200

Image address: http://domain/upload/image.jpg
Simple PHP Upload Script

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which attribute is used in <a> tag for the address of the link?
src href rel
<a href="http://coursesweb.net/" title="CoursesWeb.net">CoursesWeb.net</a>
Which CSS property sets the type of the text font?
font-family text-decoration font-size
h2 {
  font-family:"Calibri",sans-serif;
}
What instruction selects all the <div> tags with class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // number of selected items
alert(nr_elms);
Indicate the function that can be used to get the sum of values in an array.
array_sum() array_diff() array_shift()
$arr = array(1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10

Last accessed pages

  1. PHP OOP - Abstract classes (1610)
  2. JavaScript Chronometer / Stopwatch (2571)
  3. Download HTML - XHTML resources (1651)
  4. The Fifth Agreement (8637)
  5. Zuma Deluxe (680)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (35586)
  2. PHP-MySQL free course, online tutorials PHP MySQL code (28463)
  3. Read Excel file data in PHP - PhpExcelReader (27340)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (26600)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (23347)