Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Core PHP Intermediate Capabilities

Tech 1
  • Robust exception and error management
  • Handling file transmissions (uploads/downloads)
  • Dynamic media manipulation (e.g., GD library)
  • Persistent data storage via PDO or MySQLi
  • Debugging strategies and unit testing
  • Object-Oriented paradigms (OOP)
  • Namespace organization
  • Dependency management with Composer and PSR standards
  • Non-blocking execution (ReactPHP, Swoole)
  • Security hardening (XSS mitigation, cryptographic hashing)

Below are practical implementations demonstrating file handling, image manipulation, database operations, and HTTP requests.

// Handling File Transmission
if (isset($_FILES['document']) && $_FILES['document']['error'] === UPLOAD_ERR_OK) {
    $tempPath = $_FILES['document']['tmp_name'];
    $originalName = basename($_FILES['document']['name']);
    $destination = '/var/www/uploads/' . $originalName;

    if (move_uploaded_file($tempPath, $destination)) {
        // File successfully moved
    }
}

// Image Manipulation using GD
$sourceImage = imagecreatefromjpeg($destination);
$width = imagesx($sourceImage);
$height = imagesy($sourceImage);
$newImage = imagecreatetruecolor($width, $height);
imagecopyresampled($newImage, $sourceImage, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($newImage, '/var/www/processed/resized_' . $originalName, 90);
imagedestroy($sourceImage);
imagedestroy($newImage);

// Database Operation using PDO
try {
    $dbConnection = new PDO('mysql:host=127.0.0.1;dbname=app_db', 'db_user', 'secure_pass');
    $dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $insertQuery = "INSERT INTO accounts (handle, contact_email) VALUES (:handle, :email)";
    $statement = $dbConnection->prepare($insertQuery);
    $statement->execute([':handle' => $handle, ':email' => $email]);
} catch (PDOException $ex) {
    // Handle database exception
}

// Executing HTTP GET Request
function fetchRemoteData($apiEndpoint, $queryParams = []) {
    if (!empty($queryParams)) {
        $apiEndpoint .= '?' . http_build_query($queryParams);
    }

    $curlHandler = curl_init();
    $options = [
        CURLOPT_URL            => $apiEndpoint,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HEADER         => false,
        CURLOPT_TIMEOUT        => 30,
    ];
    curl_setopt_array($curlHandler, $options);

    $responsePayload = curl_exec($curlHandler);

    if ($responsePayload === false) {
        $failureReason = curl_error($curlHandler);
        curl_close($curlHandler);
        throw new RuntimeException("HTTP request failed: " . $failureReason);
    }

    curl_close($curlHandler);
    return $responsePayload;
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.