douzhanjia0773 2015-11-25 04:51
浏览 25

在php中创建动态fileupload api

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

  • 写回答

1条回答 默认 最新

  • doublestar2014 2015-11-25 05:27
    关注

    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']
    
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线