WebSite-PHP Framework PHP
Multi language
Simple Ajax mode
No HTML, no JavaScript
URL rewriting
Mail sending
Sitemap - RSS - Web service
Download WebSite-PHP FrameWork now
 


Loading
 


Tutorial Uploadfile

Tutorials

>

Tutorial Uploadfile



Tutorial 1 : Upload a file
File: /pages/tutorials/uploadfile/uploadfile-01.php

<?php
class Uploadfile01 extends Page {
    public function 
InitializeComponent() {
        
parent::$PAGE_TITLE "Tutorial : Upload a file";
        
        
$form = new Form($this);
        
        
$form_obj = new WSPObject();
        
$form_obj->setAlign(WSPObject::ALIGN_CENTER);
        
        
// it's not possible in a Tabs or DialogBox to use
        // button to upload a file.
        // To check if we are in the case of Tab we init
        // variable used bellow in the code
        
$isTabs $this->isAjaxLoadPage() || 
                    
$this->isAjaxPage();
        
        
// create upload file
        
$this->upload = new UploadFile(
                            (
$isTabs?$this:$form));
        
        
// define mime type authorized 
        
$this->upload->setAuthorizedMimeTypes(array(
                
"image/jpeg","image/png""image/gif"));
        
        
// define file size limit
        
$this->upload->setFileSizeLimit("300kb");
        
        
$form_obj->add($this->upload);
        
        if (!
$isTabs) {
            
// We will display the button only in a classic page
            
$btn = new Button($form);
            
$btn->setValue("Upload")->onClick("onClick");
            
$form_obj->add("<br/>"$btn);
            
            
$form->setContent($form_obj);
            
$this->render $form;
        } else {
            
// We are in the Tabs and we need to set ajax
            // event to upload a file
            
$this->upload->setAjaxEvent();
            
$this->upload->onChange("onClick");
            
            
// Display message to explain
            
$link = new Link("tutorials/uploadfile/uploadfile-01.html"
                            
Link::TARGET_NONE
                            
"tutorials/uploadfile/uploadfile-01.html");
            
$form_obj->add("<br/><br/>To use UploadFile in a Tab, it's ".
                
"necessary to use ajax event.<br/>Please go on the page "
                
$link" to test without ajax event.");
            
            
$this->render $form_obj;
        }
    }
    
    public function 
onClick($sender) {
        
// check the file is correctly uploaded
        
if ($this->upload->checkMimeType() && 
            
$this->upload->checkFileSize()) {
                
alert("File ".$this->upload->getFileName()." (".
                    
$this->upload->getFileSize().") uploaded !");
        } else if (
$this->upload->isEmptyFile()) {
            
alert("Your file is empty !");
        } else if (!
$this->upload->checkFileSize()) {
            
alert("Your file is too big !");
        } else {
            
alert("Mime type ".$this->upload->getFileMimeType().
                    
" not supported !");
        }
    }
}
?>


Tutorial 2 : Upload a file with ajax event
File: /pages/tutorials/uploadfile/uploadfile-02.php

<?php
class Uploadfile02 extends Page {
    public function 
InitializeComponent() {
        
parent::$PAGE_TITLE "Tutorial : Upload a file 
                    with ajax event"
;
        
        
// create upload file
        
$this->upload = new UploadFile($this);
        
        
// define mime type authorized 
        
$this->upload->setAuthorizedMimeTypes(array(
                
"image/jpeg","image/png""image/gif"));
        
        
// define file size limit
        
$this->upload->setFileSizeLimit("300kb");
        
        
// define the event method
        
$this->upload->setAjaxEvent();
        
$this->upload->onChange("onUpload");
        
        
$this->render $this->upload;
    }
    
    public function 
onUpload($sender) {
        
// check the file is correctly uploaded
        
if ($this->upload->checkMimeType() && 
            
$this->upload->checkFileSize()) {
                
alert("File ".$this->upload->getFileName()." (".
                    
$this->upload->getFileSize().") uploaded !");
        } else if (
$this->upload->isEmptyFile()) {
            
alert("Your file is empty !");
        } else if (!
$this->upload->checkFileSize()) {
            
alert("Your file is too big !");
        } else {
            
alert("Mime type ".$this->upload->getFileMimeType().
                    
" not supported !");
        }
    }
}
?>


Tutorial 3 : Upload multiple files with ajax event
File: /pages/tutorials/uploadfile/uploadfile-03.php

<?php
class Uploadfile03 extends Page {
    public function 
InitializeComponent() {
        
parent::$PAGE_TITLE "Tutorial : Upload multiple files 
                    with ajax event"
;
        
        
// create upload files
        
$this->uploads = new UploadFile($this);
        
$this->uploads->activateMultipleFiles();
        
        
// define mime type authorized 
        
$this->uploads->setAuthorizedMimeTypes(array(
                
"image/jpeg","image/png""image/gif"));
        
        
// define file size limit
        
$this->uploads->setFileSizeLimit("300kb");
        
        
// define the event method
        
$this->uploads->setAjaxEvent();
        
$this->uploads->onChange("onUploads");
        
        
$this->render $this->uploads;
    }
    
    public function 
onUploads($sender) {
        
// check the files are correctly uploaded
        
for ($i $i $this->uploads->count(); $i++) {
            if (
$this->uploads->checkMimeType($i) && 
                
$this->uploads->checkFileSize($i)) {
                    
alert("File ".$this->uploads->getFileName($i)." (".
                        
$this->uploads->getFileSize($i).") uploaded !");
            } else if (
$this->uploads->isEmptyFile($i)) {
                
alert("Your file ".$this->uploads->getFileName($i)." is empty !");
            } else if (!
$this->uploads->checkFileSize($i)) {
                
alert("Your file ".$this->uploads->getFileName($i)." is too big !");
            } else {
                
alert("Mime type ".$this->uploads->getFileMimeType($i).
                        
" of the file ".$this->uploads->getFileName($i).
                        
" is not supported !");
            }
        }
    }
}
?>


Tutorial 4 : Upload fileson a drop zone with ajax event
File: /pages/tutorials/uploadfile/uploadfile-04.php

<?php
class Uploadfile04 extends Page {
    public function 
InitializeComponent() {
        
parent::$PAGE_TITLE "Tutorial : Upload files
                    on a drop zone with ajax event"
;
        
        
// create upload file
        
$this->uploads = new UploadFile($this);

        
// Drop zone
        // See compatible browser list: http://caniuse.com/#feat=fileapi
        
$drop_zone_lbl = new Label("Drop your files in this zone to upload"true);
        
$drop_zone_obj = new WSPObject("<br/><br/><br/><br/>"$drop_zone_lbl);
        
$drop_zone_obj->setId("drop_zone_id_4");
        
$drop_zone_obj->setWidth(400)->setHeight(150);
        
$drop_zone_obj->setAlign(WSPObject::ALIGN_CENTER);
        
$drop_zone_obj->setStyle("border: 1px dashed #BBBBBB;");
        
$this->uploads->setObjectDropZone($drop_zone_obj);

        
// Authorized multiple file upload
        
$this->uploads->activateMultipleFiles();
        
        
// define mime type authorized 
        
$this->uploads->setAuthorizedMimeTypes(array(
                
"image/jpeg","image/png""image/gif"));
        
        
// define file size limit
        
$this->uploads->setFileSizeLimit("300kb");
        
        
// define the event method
        
$this->uploads->setAjaxEvent();
        
$this->uploads->onChange("onUploads");
        
        
$this->render $this->uploads;
    }

    public function 
onUploads($sender) {
        
// check the files are correctly uploaded
        
for ($i $i $this->uploads->count(); $i++) {
            if (
$this->uploads->checkMimeType($i) &&
                
$this->uploads->checkFileSize($i)) {
                
alert("File ".$this->uploads->getFileName($i)." (".
                    
$this->uploads->getFileSize($i).") uploaded !");
            } else if (
$this->uploads->isEmptyFile($i)) {
                
alert("Your file ".$this->uploads->getFileName($i)." is empty !");
            } else if (!
$this->uploads->checkFileSize($i)) {
                
alert("Your file ".$this->uploads->getFileName($i)." is too big !");
            } else {
                
alert("Mime type ".$this->uploads->getFileMimeType($i).
                    
" of the file ".$this->uploads->getFileName($i).
                    
" is not supported !");
            }
        }
    }
}
?>


Tutorial 5 : Upload a file with JavaScript check before sending
File: /pages/tutorials/uploadfile/uploadfile-05.php

<?php
class Uploadfile05 extends Page {
    public function 
InitializeComponent() {
        
parent::$PAGE_TITLE "Tutorial : Upload a file 
                    with JavaScript check before sending"
;
        
        
// create upload file
        
$this->upload = new UploadFile($this);
        
        
// define mime type authorized 
        
$this->upload->setAuthorizedMimeTypes(array(
                
"image/jpeg","image/png""image/gif"));
        
        
// define file size limit
        
$this->upload->setFileSizeLimit("300kb");

        
// Activate JavasScript check (size + mime type)
        // See compatible browser list: http://caniuse.com/#feat=fileapi
        
$this->upload->activateSizeLimitJsCheck();
        
$this->upload->activateMimeTypeJsCheck();

        
// define the event method
        
$this->upload->setAjaxEvent();
        
$this->upload->onChange("onUpload");
        
        
$this->render $this->upload;
    }
    
    public function 
onUpload($sender) {
        
// check the file is correctly uploaded
        
if ($this->upload->checkMimeType() && 
            
$this->upload->checkFileSize()) {
                
alert("File ".$this->upload->getFileName()." (".
                    
$this->upload->getFileSize().") uploaded !");
        } else if (
$this->upload->isEmptyFile()) {
            
alert("Your file is empty !");
        } else if (!
$this->upload->checkFileSize()) {
            
alert("Your file is too big !");
        } else {
            
alert("Mime type ".$this->upload->getFileMimeType().
                    
" not supported !");
        }
    }
}
?>


Tutorial 6 : Upload a file by choosing from an icon the file (with ajax event)
File: /pages/tutorials/uploadfile/uploadfile-06.php

<?php
class Uploadfile06 extends Page {
    public function 
InitializeComponent() {
        
parent::$PAGE_TITLE "Tutorial : Upload a file 
                    by choosing from an icon the file (with ajax event)"
;
        
        
// create upload file
        
$this->upload = new UploadFile($this);

        
// Define the icon to select the file (replace the normal input)
        
$this->upload->setSelectFileIcon("img/paper_clip_32.png");
        
        
// define mime type authorized 
        
$this->upload->setAuthorizedMimeTypes(array(
                
"image/jpeg","image/png""image/gif"));
        
        
// define file size limit
        
$this->upload->setFileSizeLimit("300kb");
        
        
// define the event method
        
$this->upload->setAjaxEvent();
        
$this->upload->onChange("onUpload");
        
        
$this->render $this->upload;
    }
    
    public function 
onUpload($sender) {
        
// check the file is correctly uploaded
        
if ($this->upload->checkMimeType() && 
            
$this->upload->checkFileSize()) {
                
alert("File ".$this->upload->getFileName()." (".
                    
$this->upload->getFileSize().") uploaded !");
        } else if (
$this->upload->isEmptyFile()) {
            
alert("Your file is empty !");
        } else if (!
$this->upload->checkFileSize()) {
            
alert("Your file is too big !");
        } else {
            
alert("Mime type ".$this->upload->getFileMimeType().
                    
" not supported !");
        }
    }
}
?>


Tutorial 7 : Upload a file from icon with a specific wait object (ajax event)
File: /pages/tutorials/uploadfile/uploadfile-07.php

<?php
class Uploadfile07 extends Page {
    public function 
InitializeComponent() {
        
parent::$PAGE_TITLE "Tutorial : Upload a file 
            from icon with a specific wait object (ajax event)"
;
        
        
// create upload file
        
$this->upload = new UploadFile($this);

        
// Create wait progress bar object
        
$wait_obj $this->upload->getProgressBarObject();
        
$wait_obj->setWidth(300)->setHeight(18);
        
$this->upload->setAjaxWaitMessage($wait_obj);
        
$upload_obj = new Table();
        
$upload_obj->addRowColumns($this->upload$wait_obj);

        
// Define the icon to select the file (replace the normal input)
        
$this->upload->setSelectFileIcon("img/paper_clip_32.png");
        
        
// define mime type authorized 
        
$this->upload->setAuthorizedMimeTypes(array(
                
"image/jpeg","image/png""image/gif"));
        
        
// define file size limit
        
$this->upload->setFileSizeLimit("300kb");
        
        
// define the event method
        
$this->upload->setAjaxEvent();
        
$this->upload->onChange("onUpload");
        
        
$this->render $upload_obj;
    }
    
    public function 
onUpload($sender) {
        
// check the file is correctly uploaded
        
if ($this->upload->checkMimeType() && 
            
$this->upload->checkFileSize()) {
                
alert("File ".$this->upload->getFileName()." (".
                    
$this->upload->getFileSize().") uploaded !");
        } else if (
$this->upload->isEmptyFile()) {
            
alert("Your file is empty !");
        } else if (!
$this->upload->checkFileSize()) {
            
alert("Your file is too big !");
        } else {
            
alert("Mime type ".$this->upload->getFileMimeType().
                    
" not supported !");
        }
    }
}
?>


Tutorial 8 : Drop files on a zone and specific a wait object (ajax event)
File: /pages/tutorials/uploadfile/uploadfile-08.php

<?php
class Uploadfile08 extends Page {
    public function 
InitializeComponent() {
        
parent::$PAGE_TITLE "Tutorial : Drop files on a zone
            and specific a wait object (ajax event)"
;
        
        
// create upload file
        
$this->uploads = new UploadFile($this);
        
$this->uploads->activateMultipleFiles();

        
// Drop zone
        // See compatible browser list: http://caniuse.com/#feat=fileapi
        
$drop_zone_lbl = new Label("Drop your files in this zone to upload"true);
        
$drop_zone_obj = new WSPObject("<br/><br/><br/><br/>"$drop_zone_lbl);
        
$drop_zone_obj->setId("drop_zone_id_8");
        
$drop_zone_obj->setWidth(400)->setHeight(150);
        
$drop_zone_obj->setAlign(WSPObject::ALIGN_CENTER);
        
$drop_zone_obj->setStyle("border: 1px dashed #BBBBBB;");

        
// Create wait progress bar object
        
$wait_obj $this->uploads->getProgressBarObject();
        
$wait_obj->setWidth(300)->setHeight(18);
        
$this->uploads->setAjaxWaitMessage($wait_obj);
        
$drop_zone_obj->add($wait_obj);

        
// Set drop zone on upload object
        
$this->uploads->setObjectDropZone($drop_zone_obj);
        
        
// define mime type authorized 
        
$this->uploads->setAuthorizedMimeTypes(array(
                
"image/jpeg","image/png""image/gif"));
        
        
// define file size limit
        
$this->uploads->setFileSizeLimit("300kb");
        
        
// define the event method
        
$this->uploads->setAjaxEvent();
        
$this->uploads->onChange("onUploads");
        
        
$this->render $this->uploads;
    }

    public function 
onUploads($sender) {
        
// check the files are correctly uploaded
        
for ($i $i $this->uploads->count(); $i++) {
            if (
$this->uploads->checkMimeType($i) &&
                
$this->uploads->checkFileSize($i)) {
                
alert("File ".$this->uploads->getFileName($i)." (".
                    
$this->uploads->getFileSize($i).") uploaded !");
            } else if (
$this->uploads->isEmptyFile($i)) {
                
alert("Your file ".$this->uploads->getFileName($i)." is empty !");
            } else if (!
$this->uploads->checkFileSize($i)) {
                
alert("Your file ".$this->uploads->getFileName($i)." is too big !");
            } else {
                
alert("Mime type ".$this->uploads->getFileMimeType($i).
                    
" of the file ".$this->uploads->getFileName($i).
                    
" is not supported !");
            }
        }
    }
}
?>



Share

 


Copyright © 2009-2024 WebSite-PHP Framework PHP
Home Documentation Download Quick start Tutorials Wiki Issue Tracker