PHP Classes

File: ex_FileUpload.php

Recommend this page to a friend!
  Classes of Ben Cooling  >  File-Upload  >  ex_FileUpload.php  >  Download  
File: ex_FileUpload.php
Role: Example script
Content type: text/plain
Description: Simple example usage of FileUpload class
Class: File-Upload
Validate and process uploaded files
Author: By
Last change: File added
Date: 9 years ago
Size: 1,828 bytes



Class file image Download
('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);


// Allow only PDF files and produce copies of files uploaded withthe same filename as exisiting files.
if (count($_FILES)>0){
$fileUpload = new FileUpload(array('naming'=>'random')); // All properties can be set on stand-alone lines or within an array when instantiating the FileUpload object
$fileUpload->file = $_FILES['file'];
$fileUpload->deny = array('pdf');
    if (
$fileUpload->upload_file()) echo 'File uploaded baby!';
    else echo

// Allow all files, deny none by not setting either property. Also note the naming property has not been set when instantiating the object. Random filenames will be generated.
if (count($_FILES)>0){
    $fileUpload = new FileUpload();
    $fileUpload->file = $_FILES['file'];
    $fileUpload->naming = 'random';
    if ($fileUpload->upload_file()) echo 'File uploaded baby!';
    else echo $fileUpload->file['error'];

// Deny PDF and ZIP files. As naming is not set, the default is unique. That means if a file is uploaded with the same filename as an exisiting file, an error will be returned
if (count($_FILES)>0){
    $fileUpload->file = $_FILES['file'];
    $fileUpload->deny = array('pdf','zip');
    if ($fileUpload->upload_file()) echo 'File uploaded baby!';
    else echo $fileUpload->file['error'];


<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <title>File class usage</title>
      <h1>Upload a zip file</h1>
    <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
            <p><label>File 1:</label><input type="file" name="file" /></p>
            <p><button type="submit">Upload</button></p>

For more information send a message to info at phpclasses dot org.