Skip to content

Instantly share code, notes, and snippets.

@robokoder
Forked from fowkswe/upload.php
Created March 18, 2017 02:10
Show Gist options
  • Select an option

  • Save robokoder/196bf7845d1c85af3d091bcce5ee5efd to your computer and use it in GitHub Desktop.

Select an option

Save robokoder/196bf7845d1c85af3d091bcce5ee5efd to your computer and use it in GitHub Desktop.

Revisions

  1. @fowkswe fowkswe revised this gist Sep 25, 2015. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion upload.php
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,17 @@
    <?php
    //
    // this replaces includes/create/upload.php - besure to save your old upload.php!
    // you must put the S3.php file in includes/helpers/S3.php
    // you can get it here:
    // https://github.com/tpyo/amazon-s3-php-class
    //
    // This is an improvement on this gist:
    // https://gist.github.com/Wysie/03934b6a79a715772abd

    //
    // Ensure your AWS user has propper credentials to upload to s3
    //
    // Be sure to set your bucket name and region url below.
    //

    include('../functions.php');
    include('../login/auth.php');
  2. @fowkswe fowkswe revised this gist Sep 25, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion upload.php
    Original file line number Diff line number Diff line change
    @@ -52,7 +52,6 @@

    }
    else {
    print_r($s3);
    die('there was a problem');
    }
    }
  3. @fowkswe fowkswe created this gist Sep 25, 2015.
    62 changes: 62 additions & 0 deletions upload.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    <?php
    // this replaces includes/create/upload.php - besure to save your old upload.php!
    // you must put the S3.php file in includes/helpers/S3.php
    // you can get it here:
    // https://github.com/tpyo/amazon-s3-php-class
    //
    // This is an improvement on this gist:
    // https://gist.github.com/Wysie/03934b6a79a715772abd


    include('../functions.php');
    include('../login/auth.php');
    require_once('../helpers/S3.php');

    //Init
    $file = $_FILES['upload']['tmp_name'];
    $fileName = $_FILES['upload']['name'];
    $extension_explode = explode('.', $fileName);
    $extension = $extension_explode[count($extension_explode)-1];
    $time = time();
    $allowed = array("jpeg", "jpg", "gif", "png");

    if(in_array($extension, $allowed)) {
    $awsAccessKey = get_app_info('s3_key');
    $awsSecretKey = get_app_info('s3_secret');
    $bucketName = 'YOURBUCKET'; //Change accordingly
    $endpoint = 's3-us-west-2.amazonaws.com'; //Change accordingly
    $s3 = new S3($awsAccessKey, $awsSecretKey, false, $endpoint);
    $s3Filename = $time.baseName($fileName);

    if ($s3 -> putObject($s3->inputFile($file), $bucketName, $s3Filename, S3::ACL_PUBLIC_READ)) {
    $array = array(
    'filelink' => 'http://'.$endpoint.'/'.$bucketName.'/'.$s3Filename
    );
    // echo stripslashes(json_encode($array));

    // Required: anonymous function reference number as explained above.
    $funcNum = $_GET['CKEditorFuncNum'] ;
    // Optional: instance name (might be used to load a specific configuration file or anything else).
    $CKEditor = $_GET['CKEditor'] ;
    // Optional: might be used to provide localized messages.
    $langCode = $_GET['langCode'] ;

    // Check the $_FILES array and save the file. Assign the correct path to a variable ($url).
    $url = APP_PATH.'/uploads/'.$time.'.'.$extension;
    $url = 'http://'.$endpoint.'/'.$bucketName.'/'.$s3Filename;

    // Usually you will only assign something here if the file could not be uploaded.
    $message = '';

    echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($funcNum, '$url', '$message');</script>";

    }
    else {
    print_r($s3);
    die('there was a problem');
    }
    }
    else {
    die('File not allowed');
    }
    ?>