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

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
What attribute makes a radio button or checkbox input selected?
checked="checked" selected="selected" disabled="disabled"
<input type="checkbox" name="a_name" value="value" checked="checked" />
What CSS value scales the background image to the largest size contained within the element?
repeat-x contain linear-gradient
#id {
  background:url("path_to_image.png");
  background-size:contain;
  background-repeat:no-repeat;
}
What operator is used to determine the rest of the division of two numbers?
% * /
var rest8_7 = 8 % 7;
alert(rest8_7);
Indicate the PHP function that rounds a number up to the next highest integer.
floor() ceil() abs()
$nr = ceil(3.5);
echo $nr;        // 4
Simple PHP Upload Script

Last accessed pages

  1. SSEP - Site Search Engine PHP-Ajax (2415)
  2. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (2374)
  3. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (18026)
  4. JavaScript code and PHP (8376)
  5. Get the value of multiple selected checkboxes with same name (2999)

Top accessed pages

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