dongqie4402 2018-06-11 07:51
浏览 146

使用POSTMAN在目录中上传图像

In my api,I am able to store all data like username ,email ,password and also imagepath in my abc table of xyz database but the image is not appearing in my folder. When I send the data using POSTMAN , it shows 'Product has been created' and all the data stored in my database but the image doesn't appears in my folder. Its my first api and I am a beginner, can anyone help me out.....

<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// get database connection
include_once '/formdatabase.php';

// instantiate product object
include_once '/formproduct.php';

$database = new Database();
$db = $database->getConnection();

$product = new Product($db);

// get posted data
$data = (object) $_POST;

// set product property values
$product->username = $data->username;
$product->email = $data->email;
$product->password = $data->password; 

//path were our avatar image will be stored
        $product->avatar = ('image/'.$_FILES['avatar']['name']);

        //make sure the file type is image
        if (preg_match("!image!",$_FILES['avatar']['type'])) {

            //copy image to images/ folder 
            if (copy($_FILES['avatar']['tmp_name'], $avatar)){



            }
            else {
                echo '{';
                    echo '"message": "File upload failed!"';
                echo '}';
            }

        }   
        else {
            echo '{';
                echo '"message": "Please only upload GIF, JPG or PNG images!"';
            echo '}';
        }



// create the product
if($product->create()){
    echo '{';
        echo '"message": "Product was created."';
    echo '}';
}

// if unable to create the product, tell the user
else{
    echo '{';
        echo '"message": "Unable to create product."';
    echo '}';
}
?>
  • 写回答

1条回答 默认 最新

  • doumanshan6314 2018-06-11 08:16
    关注

    When you copy your file you use $avatar instead of $product->avatar, that's why your image doesn't copy.

    It should be:

    if (copy($_FILES['avatar']['tmp_name'], $product->avatar))
    

    Hoping it helps

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分