PHP Classes

File: test.php

Recommend this page to a friend!
  Classes of Bao Nguyen Quoc   HTTP Upload   test.php   Download  
File: test.php
Role: Example script
Content type: text/plain
Description: Example
Class: HTTP Upload
Handle files uploaded via Web forms
Author: By
Last change: little change
Date: 19 years ago
Size: 5,425 bytes
 

Contents

Class file image Download
<?

$config_path
= '.';
$config_maxsize = 1024 * 1024;
$config_extension = "zip,rar,tar"; //default seperator : |
$config_img_extension = "gif,jpg,png"; //default seperator : |
$config_width = '200';
$config_height = '200';

include_once
"class.httpupload.php";

if (isset(
$_POST['mode'])) {

$object = new httpupload($config_path , 'up_file');

//$object->setuploaddir($config_path); //set upload directory
//$object->setuploadname('up_file'); //set upload form name
//$object->setuploadindex(null); //set upload file index (for multi file processing)

switch ($_POST['mode']) {
    case
0:
    break;
    case
1: //filter
       
$object->setmaxsize($config_maxsize);
       
$object->setallowext($config_extension , ","); //You can use array("zip","rar","tar")
   
break;
    case
2: //image
       
$object->setmaxsize($config_maxsize);
       
$object->setallowext($config_img_extension , ","); //You can use array("zip","rar","tar")
       
$object->setimagemaxsize($config_width , $config_height);
    break;
    case
3: //safe
       
$object->prefix = 'file';
       
$object->extension = 'txt';
    break;
}

if (
$_POST['mode'] == 4) {
   
$object->prefix = 'file';
   
$rs = ($object->upload_ex('m_up_file'));
   
$result = '';
    foreach (
$rs as $key => $var) {
       
$result .= "Index : <b>" . $var['index'] . "</b><br>";
        if (
$var['error_code'] > 0) {
           
$result .= "Your file (" . $var['file'] . ") ";
           
$result .= "has been saved to " . $var['fullpath'] . "<BR><br>";
           
$result .= "File size : " . $var['size'] . " byte<BR>";
           
$result .= "File MIME : " . $var['type'] . "<BR>";
           
$result .= "Orginal name : " . $var['name'] . "<BR>";
           
$result .= "Tmp file : " . $var['tmp_name'] . "<BR><BR>";
        } else {
           
$result .= "There was a problem with your uploaded file.<br>";
           
$result .= "Error Code : <b>" . $var['error_code'] . "</b><br>";
           
$result .= "Error Description : <br><br>";
           
$result .= $object->get_error($var['error_code']);
           
$result .= "<BR><BR>";
        }
    }
} else {

if (
$object->hasUpload()) {
    if (
$object->upload()) {
       
$result = 'Your file has been uploaded successfuly.<br>';
       
$result .= "Your file (" . $object->getsavedname(false) . ") ";
       
$result .= "has been saved to " . $object->getsavedname(true) . "<BR><br>";
       
$result .= "File size : " . $object->getuploadsize() . " byte<BR>";
       
$result .= "File MIME : " . $object->getuploadtype() . "<BR>";
       
$result .= "Orginal name : " . $object->getuploadname() . "<BR>";
       
$result .= "Tmp file : " . $object->getuploadtmp() . "<BR>";
    } else {
       
$result = "There was a problem with your uploaded file.<br>";
       
$result .= "Error Code : <b>" . $object->error_code . "</b><br>";
       
$result .= "Error Description : <br><br>";
       
$result .= $object->get_error();
    }
} else
$result = 'No file was submited.';

}

}
?>
<html>

<head>
<style>
<!--
body { font-family: Verdana; font-size: 12px }
-->
</style>
</head>
<title>HTTPUpload Example</title>
<body>

<h3><font color="navy">HTTPUpload</font></h3>
<font size=1 color=Gray>(This example requires javascript.)</font>

<?

if (@$result) {
?>
<br><br>
<b>Upload Result:</b><br><br>
<?
echo $result;
}
?>

<form name="upload_frm" id="upload_frm" action="test.php" method=post enctype="multipart/form-data">
    Select a file on your computer <input type=file name="up_file"> <input type=submit value="Upload"><br>
    <inputtype=submit value="Upload file"><br><br>
        <select name=mode onchange="test()">
        <option value=0>Simple file upload</option>
        <option value=1>Filter file upload</option>
        <option value=2>Image upload</option>
        <option value=3>Safe upload</option>
        <option value=4>MultiFile upload</option>
    </select>
<script language=javascript>
old_id = 0;
function test() {
    document.all.item('layer_' + old_id).style.display = 'none';
    document.all.item('layer_' + document.upload_frm.mode.value).style.display = 'block';
    old_id = document.upload_frm.mode.value;
}
</script>
<!-- Div -->

<div style="" id="layer_0" style="display:block;">
<b> Simple file upload:<br>
</b><br>
    All file accept. <br>
&nbsp;</div>

<div style="" id="layer_1" style="display:none;">
<b> Filter file upload:<br>
<br>
</b>Upload file with filter.<br>
<br>
    Max file size : <?= round($config_maxsize / 1024 , 2) ?> kb<br>
    Allow extension : <?= $config_extension ?><br>
<br>
</div>

<div style="" id="layer_2" style="display:none;">
<b> Image upload:<br>
</b><br>
    Accept only image file.<br>
<br>
Max file size : <?= round($config_maxsize / 1024 , 2) ?> kb<br>
    Max width: <?= $config_width?>px<br>
    Max Height: <?= $config_height?>px<br>
    Allow extension : <?= $config_img_extension?><br>
<br>
</div>

<div style="" id="layer_3" style="display:none;">
    <b>Safe Upload:</b><br>
    <br>
    Upload file with an unique name and no config_extension (no script hacking)<br>
</div>

<div style="" id="layer_4" style="display:none;">
    <b>MultiFile Upload:</b><br>
    <br>
    Upload many files (no files limit)<br>
   
    <input type=file name="m_up_file[0]"><br>
    <input type=file name="m_up_file[1]"><br>
    <input type=file name="m_up_file[2]"><br>
    <input type=file name="m_up_file[3]"><br>
    <input type=file name="m_up_file[4]"><br>
   
</div>

</form>

<font size=1 color="gray">(Click <a href="http://en.vietapi.com/wiki/index.php/PHP:_HttpUpload">here</a> to view class
information )</font>

</body>
</html>