Quick Start for PHP

  • Step 1. Add uploadcare-php to your composer.json:

    "require": {
        "uploadcare/uploadcare-php": ">=v1.1.0,<2.0"
    }
    
  • Step 2. Include the library.

    require_once 'vendor/autoload.php';
    use \Uploadcare;
    
  • Step 3. Create an instance of Uploadcare\Api:

    $api = new Uploadcare\Api('demopublickey', 'demoprivatekey');
    
  • Step 4. To show a widget, add a script tag to your <head> tag and show an input right where it belongs:

    <head>
      <?php echo $api->widget->getScriptTag(); ?>
    </head>
    
    <form method="POST" action="upload.php">
      <?php echo $api->widget->getInputTag('qs-file'); ?>
      <input type="submit" value="Save!" />
    </form>
    
  • Step 5. When file is uploaded and form is submitted you will recieve a file_id among your $_POST parameters:

    $file_id = $_POST['qs-file'];
    
  • Step 6. The $file_id is file identifier. If you need this file, use $file->store(); method to mark is as stored.

    $file = $api->getFile($file_id);
    $file->store();
    

    After that, you should save $file->getFileId(); in your database. It's just a string, containing an UUID.

  • Step 7. Such $file object can be used to display the file:

    <img src="<?php print $file->getUrl(); ?>" />
    <img src="<?php print $file->getUrl()->crop(300, 300, true)->effect('grayscale'); ?>" />
    

Read more at uploadcare-php docs.

If you haven't found what you were looking for in these docs, try looking in our Knowledge Base.