Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15015

Upload File on Server

$
0
0
I'm not exactly sure if this is a VB.NET question, a PHP question, or both.

I have the following PHP web API that uploads a file to the server and then creates a record in the database that references the file location:
Code:

<?php
// imports
require_once(__DIR__ . '/../../model/attachment.php');
require_once(__DIR__ . '/../../model/document.php');
require_once(__DIR__ . '/../../service/attachment.php');
require_once(__DIR__ . '/../../utilities/auth.php');
require_once(__DIR__ . '/../../utilities/controller.php');

// return headers/body
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=UTF-8');
$returnedJson = array();

$url = 'server/api/attachment/upload.php';
$currentUserId = AuthUtility::assertUserIdInHeaders(apache_request_headers());

// check for unauthorized request
if (!ControllerUtility::assertSystemActionPermission($url, $currentUserId)) {
    return;
}

// check for bad method
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    $returnedJson['error'] = 'The supplied request method is not supported for the requested resource. This resource expects a POST request.';
    echo json_encode($returnedJson);
    return;
}
if (!$_POST) {
    $rest_json = file_get_contents('php://input');
    $_POST = json_decode($rest_json, true);
}

// check for bad request
$errors = array();
$attachment = new AttachmentModel();
foreach (AttachmentModel::$RequiredColumnNames as $property) {
    $success = ControllerUtility::isValueInRequest($_POST, $property);
    if (!$success) {
        array_push($errors, $property);
        continue;
    }
    $attachment->$property = $_POST[$property];
}

$document = new DocumentModel();
foreach (DocumentModel::$RequiredColumnNames as $property) {
    if ($property === 'Path') { continue; }
    $success = ControllerUtility::isValueInRequest($_POST, $property);
    if (!$success) {
        array_push($errors, $property);
        continue;
    }
    $document->$property = $_POST[$property];
}
if (count($errors) > 0) {
    http_response_code(400);
    $returnedJson['error'] = 'Malformed request syntax. The following properties are missing from the request: ' . join(', ', $errors);
    echo json_encode($returnedJson);
    return;
}

// start: https://www.php.net/manual/en/features.file-upload.php#114004
if (!isset($_FILES['path']['error']) || is_array($_FILES['path']['error'])) {
    http_response_code(400);
    $returnedJson['error'] = 'Malformed request syntax. The file is either missing from the request or the request is not formatted properly.';
    echo json_encode($returnedJson);
    return;
}

switch ($_FILES['path']['error']) {
    case UPLOAD_ERR_OK:
        break;
    case UPLOAD_ERR_NO_FILE:
        http_response_code(400);
        $returnedJson['error'] = 'Malformed request syntax. The file is either missing.';
        echo json_encode($returnedJson);
        return;
    case UPLOAD_ERR_INI_SIZE:
    case UPLOAD_ERR_FORM_SIZE:
        http_response_code(400);
        $returnedJson['error'] = 'Malformed request syntax. The file size is too large.';
        echo json_encode($returnedJson);
        return;
    default:
        http_response_code(500);
        $returnedJson['error'] = 'Something went wrong uploading the file.';
        echo json_encode($returnedJson);
        return;
}

$path_parts = pathinfo($_FILES['path']['name']);
$extension = $path_parts['extension'];
$randomFileName = sha1_file($_FILES['path']['tmp_name']);
$newFileName = sprintf(__DIR__ . '/../../uploads/%s.%s', $randomFileName, $extension);
if (!move_uploaded_file($_FILES['path']['tmp_name'], $newFileName)) {
    http_response_code(500);
    $returnedJson['error'] = 'Something went wrong uploading the file.';
    echo json_encode($returnedJson);
    return;
}
$document->Path = '/server/uploads/' . $randomFileName . '.' . $extension;
// end: https://www.php.net/manual/en/features.file-upload.php#114004

// upload the document to the server, create the document record, and then create the attachment
try {
    $record = AttachmentService::upload($attachment, $document, $currentUserId);

    http_response_code(201);
    $returnedJson = $record;
}
catch (exception $e) {
    $statusCode = (!($e->getCode()) ? 500 : $e->getCode());
    http_response_code($statusCode);
    $returnedJson['error'] = $e->getMessage();
}

echo json_encode($returnedJson);
?>

As you can tell, there is a lot going on to just upload a file.

Now I need to do the same thing, the only difference is that I want to do this from my VB.NET desktop application instead of my HTML/JQuery web application.

I feel like I could make a REST request, but I'm not sure how to include the file in the request because my PHP code expects the $_FILES.

On the other hand, I feel like it would be easier to write the files directly to my server and hit the database from my VB.NET application, but I'm not sure I can write files directly to my server.

What do y'all suggest?

Viewing all articles
Browse latest Browse all 15015

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>