图像未上传到数据库但保存到文件夹

images not uploading to database but saving to folder

我的表单是为用户上传图片。图像存储在一个文件夹中,路径应该存储在数据库中,但事实并非如此。也就是说,图像已正确上传到文件夹,但路径未保存到数据库。

我试过两个完全不同的查询,但都没有用。另外,我提到了这两个问题; How to upload images into MySQL database using PHP code and php image not uploading to database,下面是有问题的代码。

<?php

  // load current profile photo script

  $username='';
  $check_pic='';
  $check_pic = mysqli_query($connection,"SELECT profile_pic FROM users WHERE username='$username'");
  $get_pic_row = mysqli_fetch_assoc($check_pic);
  $profile_pic_db = $get_pic_row['profile_pic'];
  if ($profile_pic_db == "") {
  $profile_pic = "images/default_pic.jpg";
  }
  else
  {
  $profile_pic = "userdata/profile_pics/".$profile_pic_db;
  }


  //script for uploading profile photo

   if (isset($_FILES['profilepic'])) {
   if ((@$_FILES["profilepic"]["type"]=="image/jpeg")) {

   $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   $rand_dir_name = substr(str_shuffle($chars), 0, 15);
   mkdir("C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name");

   if (file_exists("C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name/".@$_FILES["profilepic"]["name"]))
   {
    echo @$_FILES["profilepic"]["name"]." Already exists";
   }
   else
   {

//moves images to folder userdata/profile_pics...

   move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);

 //saves image url to table...

  $profile_pic_name = (@$_FILES["profilepic"]["name"]);
  $profile_pic_loc = "C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name/$profile_pic_name'";

  if($profile_pic_query = mysqli_query($connection, "INSERT INTO users (profile_pic) VALUES ('$profile_pic_loc')")){

   echo "successful upload";
     }
   else {

   echo "unsuccessful upload";
     }

  header("Location: profile.php");

  }
  }
   else
  {
   echo "unsuccessful";
  }
  }


echo"

<p>UPLOAD PROFILE PHOTO:</p>
<form action='' method='POST' enctype='multipart/form-data'>
<img src='$profile_pic' width='70' />
<input type='file' name='profilepic' /><br />
<input type='submit' name='uploadpic' value='Upload Image'>
</form>
";

?>

我尝试将 $profile_pic_query 作为 if 语句的一部分,如上所示,但没有 if 语句。我也试过 $profile_pic_name = file_get_contents(@$_FILES["profilepic"]["name"]); 有和没有 file_get_contents,没有区别。 这是我尝试过的另一种查询格式,没有任何不同;

//saves image to folder userdata/profile_pics...

 move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);

//saves image url to table...

 $profile_pic_name = @$_FILES["profilepic"]["name"];
 $profile_pic_query = mysqli_query($connection, "UPDATE users SET profile_pic='C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name/$profile_pic_name' WHERE username='$username'");

这是会话内容...

<? php
session_start();
if (isset($_SESSION['user_login'])) {
$username = $_SESSION["user_login"];
}
else {
$username = "";
}
?>

会话、查询是否有问题,或者我是否犯了一个语法错误,但出于某种原因我没有收到警告?

****更新**

根据下面的建议,我注释掉了;

header(location: 'profile.php');

我得到了一个很常见的错误:

file_get_contents(.jpg): failed to open stream: no such file or directory

常见的建议似乎围绕着确保创建适当的 tmp_file,但这里似乎并非如此?

修改一下mkdir()的代码,也许对你有帮助。

 mkdir("userdata/profile_pics/$rand_dir_name");

TO

 mkdir("userdata/profile_pics/$rand_dir_name/{$_FILES['profilepic']['name']}");

你有几个问题。

首先是您存储位置的查询有误,您的 "insert ... Where..." 表格不正确。您应该按照 MySQL 文档中的说明使用 "insert into ... values ..."。

其次,您不应该在 move 或 mkdir 等函数中使用相对路径。您可以在此处了解原因:PHP - Failed to open stream : No such file or directory

第三,echo后不能用"header()"。 Header() 发送 headers 的 http 响应。一旦您开始回应,就无法完成此操作,因为 headers 已被发送。

我已经自己解决了这个问题,并愿意与任何有类似困境的人分享...有几个不同的问题。好吧,我收到的错误似乎是错误的。错误与负责将图像从客户端上传到服务器硬盘的脚本部分有关。脚本正确上传和保存图像,但是当我删除路径中不必要的部分时,我得到的错误得到修复。

主要问题出在查询上,将图像 url 上传到 table,根本没有发生。当我重新编写查询时,这一切都得到了纠正。请参阅下面的所有代码。

<?php



  $check_pic = mysqli_query($connection,"SELECT profile_pic FROM users WHERE username='$username'");
  $get_pic_row = mysqli_fetch_assoc($check_pic);
  $profile_pic_db = $get_pic_row['profile_pic'];
  if ($profile_pic_db == "") {
  $profile_pic = "images/default_pic.jpg";
  }
  else
  {
  $profile_pic = "userdata/profile_pics/".$profile_pic_db;
  }

  //script for uploading profile photo to hard disk

   if (isset($_FILES['profilepic'])) {
   if ((@$_FILES["profilepic"]["type"]=="image/jpeg")) {

   $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   $rand_dir_name = substr(str_shuffle($chars), 0, 15);
   mkdir("userdata/profile_pics/$rand_dir_name");

   if (file_exists("userdata/profile_pics/$rand_dir_name/".@$_FILES["profilepic"]["name"]))
   {
    echo @$_FILES["profilepic"]["name"]." Already exists";
   }
   else
   {
   move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);

  //update table with url for photo

 $profile_pic_loc = "$rand_dir_name/".@$_FILES["profilepic"]["name"];


 $profile_pic_query = mysqli_query($connection, "UPDATE users SET profile_pic='$profile_pic_loc' WHERE username='$username'"); 


  if ($profile_pic_query) {

   header("Location: profile.php");
     }

   else 
   {
        die('failure');
     }
   }
  }
   }


   echo    " 

<div>
<p>UPLOAD PROFILE PHOTO:</p>
<form action='profile.php' method='POST' enctype='multipart/form-data'>
<img src='$profile_pic' width='70' />
<input type='file' name='profilepic' /><br />
<input type='submit' name='uploadpic' value='Upload Image'>
</form>
</div>
";


?>

改变;

mkdir("C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name");

   if (file_exists("C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name/".@$_FILES["profilepic"]["name"]))

和;

move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"C:/xampp/htdocs/folder/userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);

至;

mkdir("userdata/profile_pics/$rand_dir_name");

   if (file_exists("userdata/profile_pics/$rand_dir_name/".@$_FILES["profilepic"]["name"]))
   {

和;

move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);

更正了关于 'filed to open file stream' 的所有错误,还有什么不是...虽然我仍然不知道那是什么意思。

还将查询更正为;

$profile_pic_loc = "$rand_dir_name/".@$_FILES["profilepic"]["name"];


 $profile_pic_query = mysqli_query($connection, "UPDATE users SET profile_pic='$profile_pic_loc' WHERE username='$username'"); 


  if ($profile_pic_query) {

   header("Location: profile.php");
     }

   else 
   {
        die('failure');
     }

已成功将图像 URL 插入 table。现在像冠军一样工作。