在php中上传文件时出错

时间:2018-01-05 06:48:35

标签: php

我是php的初学者,为我的android项目开发API。我正在尝试上传php中的文件,在 Postman 中,API工作正常,但是当我尝试从Android上传文件时,它没有被上传。这里是我的php code.it显示undefined index: image。 从android我发送MultipartEntity

$app->post('/addBanner', 'authenticate', function () use ($app) {
global $user_id;
$response = array();
//verifyRequiredParams(array('title', 'address', 'description'));
$title = $app->request->post('title');
$address = $app->request->post('address');
$description = $app->request->post('description');
$admin = $app->request->post('admin');
$type = $app->request->post('type');

$response = uploadFile("/uploads/banners/", $_FILES['image']);
//pr($file_name);die;
$res = 5;
if (!empty($response['imageName'])) {
    $image = $response['imageName'];
    unset($response['imageName']);
    $db = new DbHandler();
    $res = $db->insertBanner($title, $address, $description, $admin, $image);
}

if ($res == 0) {
    $response["error"] = false;
    $response["message"] = "Complaint successfully added";
} else if ($res == 1) {
    $response["error"] = true;
    $response["message"] = "Oops! An error complaint not add";
}
echoRespnse(200, $response);
});

这是uploadFile()功能

function uploadFile($dir,$file)
{
$headers = apache_request_headers();

$target_dir = __DIR__ .$dir;


$uploadOk =1;
$file = $file;
$fileName=basename(time().md5($file['image']).$file['name']);
$target_file = $target_dir.$fileName;
$imageFileType =strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(file_exists($target_file)){
    echo "sorry, file already exists.";
    $uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"&& $imageFileType != "gif" ) {
    $response["error"] = true;
    $response["message"] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
if($uploadOk==0){
    $response["error"] = true;
    $response["message"] = "sorry, your file was not uploaded";
}else{
    if(move_uploaded_file($file["tmp_name"],$target_file)){
        $image=$fileName;
        $response["imageName"] = $fileName;

    }else{

        $response["error"] = true;
        $response["message"] = "Sorry, there was an error uploading your file.";
    }
}
return $response;
}

并且我在这里插入数据库

public function insertBanner($title, $address, $description, $admin, $image)
{
    $response = array();
    $stmt = $this->conn->prepare("INSERT INTO banners(title,address,description,admin,image) values(?, ?, ?, ?, ?)");
    $stmt->bind_param("sssss", $title, $address, $description, $admin, $image);
    $result = $stmt->execute();
    $stmt->close();
    // Check for successful insertion
    if ($result) {
        return COMPLAINT_CREATED_SUCCESSFULLY;
    } else {
        return COMPLAINT_CREATE_FAILED;
    }

}

这就是我的回应。

$_FILES DATA
Array
(
)
 $_REQUEST DATA
Array
(

以下错误,

 Slim Application Error

 The application could not run because of the following error:DetailsType: ErrorExceptionCode: <br >
 8Message: Undefined index: imageFile: /Applications/XAMPP/xamppfiles/htdocs/ech/v1/index.phpLine: 295<br >
 Trace<pre>#0 /Applications/XAMPP/xamppfiles/htdocs/ech/v1/index.php(295): Slim\Slim::handleErrors(8, 'Undefined index...', '/Applications/X...', 295, Array)

0 个答案:

没有答案