PHP upload custom file in ckeditor Example

Today, We want to share with you PHP upload custom file in ckeditor Example.In this post we will show you custom file CKEDITOR, Image Upload in PHP, hear for PHP ckeditor upload image from computer we will give you demo and example for implement.In this post, we will learn about PHP Uploading Image File in Ckeditor with an example.

PHP upload custom file in ckeditor Example

There are the Following The simple About PHP upload custom file in ckeditor Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop PHP – How to custom file uploading with CKEDITOR?, so the some major files and Directory structures for this example is following below.

  • make “Images” Folder
  • index.php
  • upload.php

step : 2 create index.php file

index.php

This is where I will make a simple HTML form and PHP server side source code for our web application. To make the forms simply all souce code copy and write it into your any text editor Like Notepad++, then save file it as index.php.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="robots" content="noindex, nofollow">
  <title>PHP File Manager Integration with Validation Example</title>
  <script src="http://cdn.ckeditor.com/4.6.2/standard-all/ckeditor.js"></script>
</head>
<body>
<h2>How to upload custome file in ckeditor in php</h2>
<p>PHP Simple upload custom file in ckeditor Example</p>
  <textarea cols="10" id="editor1" name="editor1" rows="10" >
  </textarea>
  <script>
	CKEDITOR.replace( 'editor1', {
		height: 300,
		filebrowserUploadUrl: "/upload.php",
	} );
  </script>
</body>

step : 2 Make Serever side Source Code

PHP create upload.php file

<?php
$destination_folder = array(
    'img'=> '/postimages/',
);

$configImg = array(
    // 'maxsize' => 2000,    
    // 'maxwidth' => 900,    
    // 'maxheight' => 800,    
    'minwidth' => 10,      
    'minheight' => 10,
    'type' => array('bmp', 'gif', 'jpg', 'jpeg', 'png'),
);


define('RENAME_F', 1);

$dataresults = '';
if(isset($_FILES['upload']) && strlen($_FILES['upload']['name']) >1) {
    define('F_NAME', preg_replace('/\.(.+?)$/i', '', basename($_FILES['upload']['name']))); 

 
    $site = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'].'/';
    $sepext = explode('.', strtolower($_FILES['upload']['name']));
    $type = end($sepext);    // gets extension
    $destination_folder = in_array($type, $configImg['type']) ? $destination_folder['img'] : $destination_folder['audio'];
    $destination_folder = trim($destination_folder, '/') .'/';


    if(in_array($type, $configImg['type'])){
        // list($width, $height) = getimagesize($_FILES['upload']['tmp_name']);  // image width and height
        if(isset($width) && isset($height)) {
            if($width > $configImg['maxwidth'] || $height > $configImg['maxheight']) $dataresults .= '\n Width x Height = '. $width .' x '. $height .' \n The maximum Width x Height must be: '. $configImg['maxwidth']. ' x '. $configImg['maxheight'];
            if($width < $configImg['minwidth'] || $height < $configImg['minheight']) $dataresults .= '\n Width x Height = '. $width .' x '. $height .'\n The minimum Width x Height must be: '. $configImg['minwidth']. ' x '. $configImg['minheight'];
            if($_FILES['upload']['size'] > $configImg['maxsize']*1000) $dataresults .= '\n Maximum file size must be: '. $configImg['maxsize']. ' KB.';
        }
    }
  
    else $dataresults .= 'The file: '. $_FILES['upload']['name']. ' has not the allowed Any More extension type.';


    function fileNameSet($p, $fn, $ex, $i){
        if(RENAME_F ==1 && file_exists($p .$fn .$ex)) return fileNameSet($p, F_NAME .'_'. ($i +1), $ex, ($i +1));
        else return $fn .$ex;
    }

    $f_name = fileNameSet($_SERVER['DOCUMENT_ROOT'] .'/'. $destination_folder, F_NAME, ".$type", 0);
    $relstorepath = $_SERVER['DOCUMENT_ROOT'] .'/'. $destination_folder . $f_name;  // full file path

    if($dataresults == '') {
        // print_r($_FILES);exit;
        if(move_uploaded_file($_FILES['upload']['tmp_name'], $relstorepath)) {
            $CKEditorFuncNum = $_GET['CKEditorFuncNum'];
            $url = $site. $destination_folder . $f_name;
            $msg = F_NAME .'.'. $type .' successfully uploaded: \n- Size: '. number_format($_FILES['upload']['size']/1024, 2, '.', '') .' KB';
            $dataresults = in_array($type, $configImg['type']) ? "window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum, '$url', '$msg')"  //for img
           : 'var editorObj = window.parent.CKEDITOR; for(var ckid in editorObj.instances) { if(editorObj.instances[ckid].focusManager.hasFocus) break;} editorObj.instances[ckid].insertHtml(\'<audio src="'. $url .'" controls></audio>\', \'unfiltered_html\'); alert("'. $msg .'"); var dialog = editorObj.dialog.getCurrent();  dialog.hide();';
        }
        else $dataresults = 'alert("Sorry, Unable to upload the file")';
    }
    else $dataresults = 'alert("'. $dataresults .'")';
}

@header('Content-type: text/html; charset=utf-8');
echo '<script>'. $dataresults .';</script>';

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP upload custom file in ckeditor Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Ng-Repeat Directive using Angular Example
  2.   Top 10 Advanced PHP Interview Questions Answers
  3.   WordPress Create Plugin From Scratch
  4.   PHP Image Resize Script Example
  5.   PHP Delete All Directory Files In Directory
  6.   Very Simple PHP Script to Upload Image or file from an URL
  7.   Laravel 6 String Explode and Implode Tutorial
  8.   how to upload image in ckeditor in Laravel?
  9.   ng-table using Angular Example
  10.   How to Open URL in New Tab using PHP