Create Dynamic fileupload api in php

342

I am working on file upload api in php. For Now I just create this simple php which will upload the file from an html page to server. But in this code the fileupload control's name is fixed so I pass that name in my php code for upload the file. But I want to create this api for third party. If anybody ask for api then I will give link of my api and they will consume it. Now anybody please help me to convert this into dynamic

Here is the html code

<html>
<head>
 </head>
 <body>

 <h2>Upload Image </h2>
<form action="http://mvcangularworld.com/api.php" method="POST" enctype="multipart/form-data" >
<input type="file" name="filename" value="" />
<br />
<input type="submit" value="Upload File"  />
</form>

</body>

</html>

and here is my php code for api.php

<?php

// Path to move uploaded files
$target_path = 'images/';


$response = array();


$file_upload_url = $target_path;
$filename = $_POST['filename']; 
 if (isset($_FILES['filename']['name'])) 
{
    $target_path = $target_path . basename($_FILES['filename']['name']);
    // reading other post parameters
    echo $_FILES['filename']['name']."<br />";
    echo $_FILES['filename']['tmp_name']."<br />";

    $response['file_name'] = basename($_FILES['filename']['name']);

    try 
    {
        // Throws exception incase file is not being moved
        if (!move_uploaded_file($_FILES['filename']['tmp_name'], $target_path)) 
        {
            // make error flag true
            $response['error'] = true;
            $response['message'] = 'Could not move the file!';
        }

        // File successfully uploaded
         //echo $file_upload_url . basename($_FILES['filename']['name']);
        $response['message'] = 'File uploaded successfully!';
        $response['error'] = false;
        $response['file_path'] = $file_upload_url . basename($_FILES['filename']['name']);
    } 
    catch (Exception $e) 
    {
        // Exception occurred. Make error flag true
        $response['error'] = true;
        $response['message'] = $e->getMessage();
    }
} 
//else 
//{ 
    // File parameter is missing
 /*    $response['error'] = true;
    $response['message'] = 'Not received any file';
} */

// Echo final json response to client
echo

 json_encode($response, JSON_UNESCAPED_SLASHES);
?>

here is the api link

http://mvcangularworld.com/api.php

Please help me to make this api dynamic

617

Answer

Solution:

A simple solution can be, add a hidden field which have name of file field like:

<input type="hidden" name="fileFieldName" value="filename" />

and on server side:

$fileFieldName = $_POST['fileFieldName'];
move_uploaded_file($_FILES[$fileFieldName]['tmp_name']

People are also looking for solutions to the problem: php - How to update database records after the initial data entry

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.