• Welcome to the Chevereto user community!

    Here users from all over the world gather around to learn the latest about Chevereto and contribute with ideas to improve the software.

    Please keep in mind:

    • 😌 This community is user driven. Be polite with other users.
    • 👉 Is required to purchase a Chevereto license to participate in this community (doesn't apply to Pre-sales).
    • 💸 Purchase a Pro Subscription to get access to active software support and faster ticket response times.

Multiupload from API

aladdin

Chevereto Member
Can you set an example for the multiupload images from your computer and URL-links? This requires a flash?

Version 2.5.8
 
Hello,

If I understand you correctly, you want to upload multiple files using the API?

Well, you shouldn't, simply because it will be slower than simply starting a thread for each selected image.
That way, you will upload multiple files, calling multiple time the server. It is simpler, and easier.
 
Yes you correctly understand me.
The standard interface, this function works fine, but i do not understand how to do it via the API.
This API upload one image from comp

PHP:
if (isset($_POST['submit']))
{
           
           
 
list($width, $height, $type, $attr) = getimagesize($_FILES['upload']['tmp_name']);
 
 
    //set POST variables
    $url = 'http://mysite.com/api';
       
    $fields = array(
                        'key' => urlencode('rebill_api'),
                        'upload' => base64_encode(file_get_contents($_FILES['upload']['tmp_name'])),
                        'format' => urlencode('txt')
                    );
 
if($width>1920) $fields['resize_width'] = 1920;
 
    //open connection
    $ch = curl_init();
 
 
 
    //set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, sizeof($fields));
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 240);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
 
    //execute post
   
    $result = curl_exec($ch);
   
if (!in_array($type, array('1', '2', '3')))
    {
        echo '<SCRIPT LANGUAGE="JavaScript">alert("Only PNG, JPG, JPEG or GIF");</SCRIPT>';
        exit;
    }
    if ($result=="too big")
    {
        echo '<SCRIPT LANGUAGE="JavaScript">alert("MAX 2Mb");</SCRIPT>';
        exit;
    }
   
    //close connection
print"".$result."";
    curl_close($ch);
 
}

and from URL

PHP:
if (isset($_POST['submit_url']))
{

list($width, $height, $type, $attr) = getimagesize($_POST['upload2']);



    //set POST variables
    $url = 'http://mysite.com/api';
    $fields = array(
                        'key' => urlencode('rebill_api'),
                        'upload' => $_POST['upload2'],
                        'format' => urlencode('txt')
                    );
if($width>1920) $fields['resize_width'] = 1920;

    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, sizeof($fields));
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 240);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));

    //execute post
    
    $result = curl_exec($ch);
    if (!in_array($type, array('1', '2', '3'))) 
    {
        echo '<SCRIPT LANGUAGE="JavaScript">alert("Only PNG, JPG, JPEG or GIF");</SCRIPT>';
        exit;
    }
    if ($result=="too big") 
    {
        echo '<SCRIPT LANGUAGE="JavaScript">alert("Max 2Mb");</SCRIPT>';
        exit;
    }
    
print"".$result."";

    //close connection
    curl_close($ch);

}

Upload form is

HTML:
<script>
function doGet() {
document.getElementById("uploading").style.display="inline";
document.getElementById("f_up").style.display="none";

}
</script>
<form NAME="stockForm" action="upload.php" method="post" enctype="multipart/form-data" style="margin:0; padding:0">

From comp
<table cellpadding="0" cellspacing="3" width="100%" border="0">
                    <tr valign="bottom">
                        <td>
                            <input type="file" size="30" name="upload" />
                            <br />

                        </td>
                        <td align="right"><input type="submit" class="button" name="submit" onclick="doGet();" value="Upload"  style="width:70px" /></td>
                    </tr>
                    </table>
From URL
<table cellpadding="0" cellspacing="3" width="100%" border="0">
                    <tr valign="bottom">
                        <td>
                            <input type="text" class="bginput" size="30" name="upload2" />
                            <br />

                        </td>
                        <td align="right">
                        
                        <input type="submit" class="button" name="submit_url" onclick="doGet();" value="Загрузить" style="width:70px" />
                        </td>
                    </tr>
                    </table>
</form>

For multiupload i need to use flash? or not? How i can do it?
 
No you do not need flash, flash is used to support older browsers like IE 8.

First of all, you must tell html that you want to allow multiple files, so

HTML:
<input type="file" size="30" name="upload" />

Should be :

HTML:
<input type="file" size="30" name="upload[]" />

Now, when you will receive your $_FILES variable in php, instead of having a string for the value of tmp_name or others variables, you will receive an array

So instead of

PHP:
$_FILES['upload']['tmp_name']

it should be

PHP:
$_FILES['upload']['tmp_name'][0]

and from there, you may loop the array and send a curl request for each items.

I hope it answer your question,
Danny
 
I hope it answer your question,
Danny


partially yes

Upload from comp works fine

PHP:
<script>
function doGet() {
document.getElementById("uploading").style.display="inline";
document.getElementById("f_up").style.display="none";
 
}
</script>
<form NAME="stockForm" action="upload.php" method="post" enctype="multipart/form-data" style="margin:0; padding:0">
 
From comp
<table cellpadding="0" cellspacing="3" width="100%" border="0">
<tr valign="bottom">
<td>
<input type="file" size="30" name="upload" />
<br />
 
</td>
<td align="right"><input type="submit" class="button" name="submit" onclick="doGet();" value="Upload" style="width:70px" /></td>
</tr>
</table>
From URL
<table cellpadding="0" cellspacing="3" width="100%" border="0">
<tr valign="bottom">
<td>
<input type="text" class="bginput" size="30" name="upload2" />
<br />
 
</td>
<td align="right">
 
<input type="submit" class="button" name="submit_url" onclick="doGet();" value="Загрузить" style="width:70px" />
</td>
</tr>
</table>
</form>
<?
if (isset($_POST['submit']))
{
for ($i = 0; $i < count($_FILES['upload'][name]); $i++)
{
 
 
list($width, $height, $type, $attr) = getimagesize($_FILES['upload']['tmp_name'][$i]);
 
 
//set POST variables
$url = '[URL]http://mysite.com/api';[/URL]
 
$fields = array(
'key' => urlencode('rebill_api'),
'upload' => base64_encode(file_get_contents($_FILES['upload']['tmp_name'][$i])),
'format' => urlencode('txt')
);
 
if($width>1920) $fields['resize_width'] = 1920;
 
//open connection
$ch = curl_init();
 
 
 
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, sizeof($fields));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 240);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
 
//execute post
 
$result = curl_exec($ch);
 
if (!in_array($type, array('1', '2', '3')))
{
echo '<SCRIPT LANGUAGE="JavaScript">alert("Only PNG, JPG, JPEG or GIF");</SCRIPT>';
exit;
}
if ($result=="too big")
{
echo '<SCRIPT LANGUAGE="JavaScript">alert("MAX 2Mb");</SCRIPT>';
exit;
}
 
//close connectionprint"".$result."";
curl_close($ch);
}
 
}
?>

Uploads from URL i will do the same, but i do not understand how to connect the Ajax as a demo? How better to use multiple URLs. PLZ

Can i use standart function in API? And How?
 
Each time you need to upload multiple files you must iterate that request. In the default website Chevereto issues a request for each URL, each file, etc. So if you have 5 files selected you must run the request 5 times, one single time for each file or parallel.
 
Hello, how to activate standart multiupload protection in API functions?

$config['max_uploads_per_minute'] = 10; // Default: 15
$config['max_uploads_per_hour'] = 30; // Default: 30
$config['max_uploads_per_day'] = 50; // Default: 50
$config['max_uploads_per_week'] = 200; // Default: 200
$config['max_uploads_per_month'] = 500; // Default: 500
 
Exactly where and how it should overwrite? How?

PHP:
<?php
/* --------------------------------------------------------------------

  Chevereto
  http://chevereto.com/

  @version    2.5.8
  @author    Rodolfo BerrГ-os A. <http://rodolfoberrios.com/>
            <inbox@rodolfoberrios.com>

  Copyright (C) 2013 Rodolfo BerrГ-os A. All rights reserved.
 
  BY USING THIS SOFTWARE YOU DECLARE TO ACCEPT THE CHEVERETO EULA
  http://chevereto.com/license

  --------------------------------------------------------------------- */

define('access', 'API');
require_once('includes/chevereto.php');

/*** Die, die, die my darling ***/
if(chevereto_config('api_key')=='my_api_key' and chevereto_config('api_mode')=='private' and !is_localhost()) {
    chevereto_die(array('Open <code>includes/config.php</code>','Edit <code>$config[\'api_key\'] = \'my_api_key\';</code> with a different key.'), 'API key', array('You haven\'t changed the default api key, the API won\'t work until you fix this.'));
}

$key        = $_REQUEST['key'];
$to_upload    = $_REQUEST['upload'];
$to_resize    = $_REQUEST['resize_width'];
$format        = $_REQUEST['format'];
$callback    = $_REQUEST['callback'];

/*** Checks the auth ***/
if(api_mode('private') and api_key()!==$key and !is_localhost()) {
    $error_key_msg = 'Invalid API key';
    $ERROR_AUTH_API = array(
        'status_code'    => 403,
        'status_txt'    => $error_key_msg
    );
    switch($format) {
        default:
        case 'json':
        default:
            json_output($ERROR_AUTH_API, $callback);
        break;
        case 'xml':
            xml_output($ERROR_AUTH_API);
        break;
        case 'txt':
            echo $error_key_msg;
        break;
    }
    exit; // Shout the door
}

/*** Observe the image request ***/
if(is_image_url($to_upload)) {
    $api_remote_upload = true;
} else {
    if(check_value($to_upload) or check_value($_FILES['upload'])) {
        // Creates the temp image
        $api_temp_name = __CHV_PATH_IMAGES__.generateRandomString(16).'.temp';
        while(file_exists($api_temp_name)) {
            $api_temp_name = __CHV_PATH_IMAGES__.generateRandomString(16).'.temp';
        }
        if(check_value($_FILES['upload']['tmp_name'])) {
            $to_upload = $_FILES['upload'];   
        } else {
            // The base64 comes from POST?
            if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                // Handles the stream
                $fh = fopen($api_temp_name,'w');
                stream_filter_append($fh,'convert.base64-decode',STREAM_FILTER_WRITE);
                if(!@fwrite($fh, $to_upload)) {
                    $error = 'invalid base64 byte sequence';
                } else {
                    // Since all the validations works with $_FILES, we're going to emulate it.
                    $to_upload = array(
                        'name'        => generateRandomString(rand(5,10)).'.jpg',
                        'type'        => 'image/jpeg',
                        'tmp_name'    => $api_temp_name,
                        'error'        => 'UPLOAD_ERR_OK',
                        'size'        => '1'
                    );
                }
                fclose($fh);
               
            } else {
                $error = "image base64 string must be sent using POST method";
            }
           
        }
       
    }
}

// No errors, attemp to do the upload process
if(!$error) {
    require_once(__CHV_PATH_CLASSES__.'class.upload.php');
    $api_upload = new Upload($to_upload);
    if($api_remote_upload) $api_upload->is_remote = true;
   
    $api_upload->img_upload_path = __CHV_PATH_IMAGES__;
    $api_upload->storage = chevereto_config('storage');
    $api_upload->resize_width = $to_resize;
    $api_upload->thumb_width = chevereto_config('thumb_width');
    $api_upload->thumb_height = chevereto_config('thumb_height');
   
    $api_upload->max_size = return_bytes(chevereto_config('max_filesize'));
   
    /*** Do the thing? ***/
    if($api_upload->process()) {
       
        $api_status_code = 200;
        $api_status_txt = 'OK';
        // Build the data array
        $api_data_array = $api_upload->image_info;
        if($api_upload->is_remote) {
            $api_data_array['source'] = $to_upload;
        } else {
            $api_data_array['source'] = 'base64 image string';
        }
        $api_data_array['resized'] = check_value($to_resize) ? '1' : '0';
        $api_txt_output = $api_upload->image_info['image_url'];
       
        // Short URL generation
        if(is_config_short_url()) {
            require(__CHV_PATH_INCLUDES__.'shorturl.php');
            $api_data_array['shorturl'] = $ShortURL->get_ShortURL($api_upload->image_info['image_url']);
        }
       
    } else {
        $api_status_code = 403;
        $api_status_txt = $api_upload->error;
    }
} else {
    $api_status_code = 403;
    $api_status_txt = $error;
}

$REST_API = array(
        'status_code'    => $api_status_code,
        'status_txt'    => $api_status_txt,
        'data'            => $api_data_array
);

$OUTPUT_REST_API = array_filter($REST_API);

switch($format) {
    default:
    case 'json':
    default:
        json_output($OUTPUT_REST_API, $callback);
    break;
    case 'xml':
        xml_output($OUTPUT_REST_API);
    break;
    case 'txt':
        echo $api_txt_output;
    break;
    case 'redirect':
        if($OUTPUT_REST_API['status_code']==200) {
            $redirect_url = __CHV_BASE_URL__.__CHV_VIRTUALFOLDER_IMAGE__.'/'.$api_upload->image_info['image_id_public'];
            header("Location: $redirect_url");
        } else {
            die($OUTPUT_REST_API['status_txt']);
        }
    break;
}

?>
 
Just below the config call, just $config["overrite"] = "will work";
 
PHP:
<?php
/* --------------------------------------------------------------------

  Chevereto
  http://chevereto.com/

  @version    2.5.8
  @author    Rodolfo Berríos A. <http://rodolfoberrios.com/>
            <inbox@rodolfoberrios.com>

  Copyright (C) 2013 Rodolfo Berríos A. All rights reserved.
 
  BY USING THIS SOFTWARE YOU DECLARE TO ACCEPT THE CHEVERETO EULA
  http://chevereto.com/license

  --------------------------------------------------------------------- */

define('access', 'API');
require_once('includes/chevereto.php');

/*** Die, die, die my darling ***/
if(chevereto_config('api_key')=='my_api_key' and chevereto_config('api_mode')=='private' and !is_localhost()) {
    chevereto_die(array('Open <code>includes/config.php</code>','Edit <code>$config[\'api_key\'] = \'my_api_key\';</code> with a different key.'), 'API key', array('You haven\'t changed the default api key, the API won\'t work until you fix this.'));
}

$key        = $_REQUEST['key'];
$to_upload    = $_REQUEST['upload'];
$to_resize    = $_REQUEST['resize_width'];
$format        = $_REQUEST['format'];
$callback    = $_REQUEST['callback'];

    $config['max_uploads_per_minute'] = 10; // Default: 15
    $config['max_uploads_per_hour'] = 30; // Default: 30
    $config['max_uploads_per_day'] = 50; // Default: 50
    $config['max_uploads_per_week'] = 200; // Default: 200
    $config['max_uploads_per_month'] = 500; // Default: 500



/*** Checks the auth ***/
if(api_mode('private') and api_key()!==$key and !is_localhost()) {
    $error_key_msg = 'Invalid API key';
    $ERROR_AUTH_API = array(
        'status_code'    => 403,
        'status_txt'    => $error_key_msg
    );
    switch($format) {
        default:
        case 'json':
        default:
            json_output($ERROR_AUTH_API, $callback);
        break;
        case 'xml':
            xml_output($ERROR_AUTH_API);
        break;
        case 'txt':
            echo $error_key_msg;
        break;
    }
    exit; // Shout the door
}

/*** Observe the image request ***/
if(is_image_url($to_upload)) {
    $api_remote_upload = true;
} else {
    if(check_value($to_upload) or check_value($_FILES['upload'])) {
        // Creates the temp image
        $api_temp_name = __CHV_PATH_IMAGES__.generateRandomString(16).'.temp';
        while(file_exists($api_temp_name)) {
            $api_temp_name = __CHV_PATH_IMAGES__.generateRandomString(16).'.temp';
        }
        if(check_value($_FILES['upload']['tmp_name'])) {
            $to_upload = $_FILES['upload'];   
        } else {
            // The base64 comes from POST?
            if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                // Handles the stream
                $fh = fopen($api_temp_name,'w');
                stream_filter_append($fh,'convert.base64-decode',STREAM_FILTER_WRITE);
                if(!@fwrite($fh, $to_upload)) {
                    $error = 'invalid base64 byte sequence';
                } else {
                    // Since all the validations works with $_FILES, we're going to emulate it.
                    $to_upload = array(
                        'name'        => generateRandomString(rand(5,10)).'.jpg',
                        'type'        => 'image/jpeg',
                        'tmp_name'    => $api_temp_name,
                        'error'        => 'UPLOAD_ERR_OK',
                        'size'        => '1'
                    );
                }
                fclose($fh);
               
            } else {
                $error = "image base64 string must be sent using POST method";
            }
           
        }
       
    }
}

// No errors, attemp to do the upload process
if(!$error) {
    require_once(__CHV_PATH_CLASSES__.'class.upload.php');
    $api_upload = new Upload($to_upload);
    if($api_remote_upload) $api_upload->is_remote = true;
   
    $api_upload->img_upload_path = __CHV_PATH_IMAGES__;
    $api_upload->storage = chevereto_config('storage');
    $api_upload->resize_width = $to_resize;
    $api_upload->thumb_width = chevereto_config('thumb_width');
    $api_upload->thumb_height = chevereto_config('thumb_height');
   
    $api_upload->max_size = return_bytes(chevereto_config('max_filesize'));
   
    /*** Do the thing? ***/
    if($api_upload->process()) {
       
        $api_status_code = 200;
        $api_status_txt = 'OK';
        // Build the data array
        $api_data_array = $api_upload->image_info;
        if($api_upload->is_remote) {
            $api_data_array['source'] = $to_upload;
        } else {
            $api_data_array['source'] = 'base64 image string';
        }
        $api_data_array['resized'] = check_value($to_resize) ? '1' : '0';
        $api_txt_output = $api_upload->image_info['image_url'];
       
        // Short URL generation
        if(is_config_short_url()) {
            require(__CHV_PATH_INCLUDES__.'shorturl.php');
            $api_data_array['shorturl'] = $ShortURL->get_ShortURL($api_upload->image_info['image_url']);
        }
       
    } else {
        $api_status_code = 403;
        $api_status_txt = $api_upload->error;
    }
} else {
    $api_status_code = 403;
    $api_status_txt = $error;
}

$REST_API = array(
        'status_code'    => $api_status_code,
        'status_txt'    => $api_status_txt,
        'data'            => $api_data_array
);

$OUTPUT_REST_API = array_filter($REST_API);

switch($format) {
    default:
    case 'json':
    default:
        json_output($OUTPUT_REST_API, $callback);
    break;
    case 'xml':
        xml_output($OUTPUT_REST_API);
    break;
    case 'txt':
        echo $api_txt_output;
    break;
    case 'redirect':
        if($OUTPUT_REST_API['status_code']==200) {
            $redirect_url = __CHV_BASE_URL__.__CHV_VIRTUALFOLDER_IMAGE__.'/'.$api_upload->image_info['image_id_public'];
            header("Location: $redirect_url");
        } else {
            die($OUTPUT_REST_API['status_txt']);
        }
    break;
}

?>

not work. pleaze help me do it.
 
It means go from 10 to 1000 or some. Increase the limits.
 
But I do not need 1000 uploads per minute.
I need 10 per minute, 30 per hour, 50 per day, etc.
What should I do? Please give me exact code. Thanks.
 
Ok but the API is the same for every API request so if you have 2 clients they will share the limits. Get it?
 
Back
Top