miércoles, 27 de noviembre de 2024

check if a value is not or is in an array and create other array with those values

 <?php



$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);

$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 2, 'cyan'   => 1);



$arr=array();



foreach($array1 as $value){

if (!in_array($value, $array2)) {

$arr[]=$value;

}


}


print_r($arr);



?>


Array ( [0] => 3 [1] => 4 )

sábado, 2 de noviembre de 2024

AWS s3 upload

Step 1: Install Required Packages

 sudo apt update

sudo apt install php php-cli php-zip unzip

sudo apt install php-xml

Step 2: Download and Install Composer

curl -sS https://getcomposer.org/installer | php

Move Composer to a Global Location:
sudo mv composer.phar /usr/local/bin/composer

Make It Executable:

sudo chmod +x /usr/local/bin/composer

Step 3: Verify the Installation

composer --version

Step 4: Create a PHP Project and Install Dependencies

cd /path/to/your/project

composer init

composer require aws/aws-sdk-php 

#############


<?php
require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;

// Create a new S3 client
$s3Client = new S3Client([
    'region' => 'us-east-1', // e.g. us-west-2
    'version' => 'latest',
    'credentials' => [
        'key'    => 'AKMNB#888888PA',
        'secret' => 'I54444444444444a'
    ],
]);


$bucket = 'asteriskrecordings';
$filePath = "$argv[1]"; // Local file path
$keyName = basename($filePath); // Use the base name of the file

try {
    // Upload the file
    $result = $s3Client->putObject([
        'Bucket' => $bucket,
        'Key'    => $keyName,
        'SourceFile' => $filePath
    ]);

    echo "File uploaded successfully. File URL: " . $result['ObjectURL'] . "\n";
} catch (AwsException $e) {
    // Output error message if upload fails
    echo "Error uploading file: " . $e->getMessage() . "\n";
}
?>




...........

function version

<?php
require '/root/fm/aws/vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;

// Create a new S3 client with hardcoded credentials
$s3Client = new S3Client([
    'region' => 'us-east-1', // AWS region (e.g., us-west-2, us-east-1)
    'version' => 'latest', // Use the latest version of the API
    'credentials' => [
        'key'    => 'AKI11A', // Hardcoded AWS Access Key
        'secret' => 'I5HTgA33brbpj/Sz' // Hardcoded AWS Secret Key
    ],
]);

function s3_name_upload($filePath)
{
    $bucket = 'asteriskrecordings';  // Your S3 bucket name

    // Validate that the file exists
    if (!file_exists($filePath)) {
        echo "Error: The file '$filePath' does not exist.\n";
        return;
    }

    $keyName = basename($filePath); // Use the base name of the file for the S3 key

    try {
        // Upload the file to S3
        $result = $GLOBALS['s3Client']->putObject([
            'Bucket' => $bucket,
            'Key'    => $keyName,
            'SourceFile' => $filePath
        ]);

        echo "File uploaded successfully. File URL: " . $result['ObjectURL'] . "\n";
    } catch (AwsException $e) {
        // Output error message if upload fails
        echo "Error uploading file: " . $e->getMessage() . "\n";
    }
}

// Ensure a file path argument is passed when running the script
if ($argc < 2) {
    echo "Usage: php script.php <file-path>\n";
    exit(1);
}

$filePath = $argv[1];  // Local file path passed as a command-line argument
s3_name_upload($filePath);
?>