【问题标题】:PHP Undefined index in image upload图片上传中的PHP未定义索引
【发布时间】:2018-08-28 18:28:11
【问题描述】:

我正在尝试上传要存储在 mysql 数据库中的图像,但是我一直收到错误消息:

注意:未定义的索引:profilepic in >[php-path]/tuto>rsignupsubmit.php on line 17

表单上有一个简单的上传类型文件的按钮:

<div class="field">
    <label>Profile Picture</label>
    <input class="button" type="file" name="profilepic">
</div>

然后我在另一个提交页面上有此代码,我对 PHP 很陌生,但是我已经搜索和搜索,但在任何地方都找不到解决方案。

//File upload
$target_dir = "../img/";
$newprofilepic = $target_dir . basename($_FILES ["profilepic"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($newprofilepic,PATHINFO_EXTENSION));

$newlocation = mysqli_real_escape_string($conn,$_POST['location']);
$insertquery = "INSERT INTO tutors(name, username, password, email, mobile, 
profilepic, location, 
message)"."VALUES('$newname','$newusername','$newpassword','$newemail', 
'$newmobile', '$newprofilepic', '$newlocation', '$newmessage')";

$result = mysqli_query($conn, $insertquery) or die(mysqli_error($conn));


mysqli_close($conn);

提前致谢。

【问题讨论】:

  • 我建议你做 var_dump($_FILES);以便查看您的服务器正在接收什么。
  • 你能分享你的整个html代码吗??

标签: php mysql html file-upload


【解决方案1】:

我已尝试重现此错误。这是我的代码

<form action="" method="post" enctype="multipart/form-data" >
   <div class="field">
      <label>Profile Picture</label>
      <input class="button" type="file" name="profilepic">
   </div>
   <div>
      <input type="submit">
   </div>
</form>

$target_dir = "../img/";
$newprofilepic = $target_dir . basename($_FILES["profilepic"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($newprofilepic, PATHINFO_EXTENSION));

我没有出现这个错误。您在表单标签中使用enctype="multipar/form-data" 吗?

如果您的 $_FILES 数组中不存在此键,则会出现此错误。请使用var_dump($_FILES);检查它

【讨论】:

  • 我现在已经将字符串名称上传到 mysql 表,但是我在将实际图像上传到服务器以便它可以从表正确显示到网站上时遇到问题?跨度>
  • 你是如何把文件放到服务器上的?使用 copy() 方法?只需进行 copy($_FILES['profilepic']['tmp_name'], 'your_file_name.jpg') 检查一切是否正常,然后将新行添加到数据库中。
【解决方案2】:

未定义索引:当您调用数组或对象的元素但该元素不存在时,这是一个常见错误。例如,如果在下面的 $_FILES 数组中没有“profilepic”元素,您将收到此错误。

$_FILES ["profilepic"]["name"]

【讨论】:

    猜你喜欢
    • 2019-07-04
    • 2012-04-05
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    相关资源
    最近更新 更多