【问题标题】:Undefined index while image uploading in PHP在PHP中上传图像时未定义的索引
【发布时间】:2017-02-12 05:46:28
【问题描述】:

这是我的网络服务,将图像从 android 上传到 php 服务器。我在 Advance Rest Client 中运行此服务,但每次都会出现未定义索引错误。

这是我的 php 文件:

<?php   
session_start();
include('classes/mainclass.php');
header('Content-type: application/json; charset=utf-8');
$CustomHeaders = apache_request_headers();

$category=array();

$main_cat = $_POST['main_cat'];
$sub_cat = $_POST['sub_cat'];
$content_name = $_POST['content_name'];
$address=$_POST['address'];
$contact=$_POST['contact'];
$website=$_POST['website'];
$email=$_POST['email'];
$additional=$_POST['additional'];
//$imgurl=$_POST['imageurl'];


$dataObj = new UserClass();

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    $imageUrl = $target_dir.basename( $_FILES["fileToUpload"]["name"]);

    $result1 = $dataObj->requestcontent($main_cat,$sub_cat,$content_name,$address,$contact,$website,$email,$additional,$imageUrl);

            if($result1)
            {       
                ResponseClass::ResponseMessage("1","Content Request Submitted","True");

            }
            else
            {

                ResponseClass::ResponseMessage("2","Content Request Not Submitted","False");
            }



} else {
    echo "Sorry, there was an error uploading your file.";
}
?>

【问题讨论】:

  • 确保您的请求已发布
  • 你的请求内容类型是json还是key值?
  • 请求在@B.Desai 发布
  • 请求内容类型是 urlencoded @suraj

标签: php file-upload image-uploading


【解决方案1】:

您不能将 if 条件用于 post 值。这意味着

if(isset($_POST){$main_cat = $_POST['main_cat'];$sub_cat = $_POST['sub_cat'];$content_name =$_POST['content_name'];$address=$_POST['address'];$contact=$_POST['contact'];$website=$_POST['website'];$email=$_POST['email'];$additional=$_POST['additional'];}

【讨论】:

  • 一行代码很难阅读,你不需要做isset($_POST) - 数组本身总是可用的。
猜你喜欢
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 2019-07-04
  • 2018-08-28
  • 2016-04-16
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多