为什么我不能将文件路径插入数据库或存储图像? PHP

时间:2016-04-20 09:52:37

标签: php mysql

我在将图像上传到文件夹并将相应的文件路径插入数据库时​​遇到了很多问题。

这就是我尝试过的:

$target_dir = "img/posts";
$target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["imageUpload"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}

$image=basename( $_FILES["imageUpload"]["name"],".jpg"); // used to store the filename in a variable

//storind the data in your database
$query= "INSERT INTO posts `post_image` VALUES ('$image')";
mysql_query($query);

我该怎么办?

4 个答案:

答案 0 :(得分:2)

插入查询语法

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

()

中写下列
$query= "INSERT INTO posts (`post_image`) VALUES ('$image')";

                          ^^^^         ^^^
  

注意: - 不推荐使用mysql而是使用mysqli或PDO

答案 1 :(得分:0)

更新您的插入查询:

$query= "INSERT INTO posts (`post_image`) VALUES ('$image')";

同时打印此查询以检查$ image是否有值。

答案 2 :(得分:0)

以下是我测试的上传示例,它可以正常使用

<form enctype="multipart/form-data" method="post" action="">

<input type="file" name="imageUpload" >
<input type="submit" name="submit" >
</form>

php code

if(isset($_POST['submit']))
 {
   $target_dir = "upload/";
   $target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
   $uploadOk = 1;
   $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

   if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["imageUpload"]["name"]). " has been uploaded.";
   } else {
    echo "Sorry, there was an error uploading your file.";
   }

   $image=basename( $_FILES["imageUpload"]["name"],".jpg"); // used to store the filename in a variable

  //storind the data in your database
  $query= "INSERT INTO posts (`post_image`) VALUES ('$image')";
  $result=mysql_query($query);
  if($result){
    echo "success";
  }else{
    echo "failure";
  }
}

如果这不起作用,请检查数据库连接

注意: - 不推荐使用mysql而是使用mysqli或PDO

答案 3 :(得分:0)

您可以使用此代码

$allow = array("jpg", "jpeg", "gif", "png");

$todir = 'img/posts/';

if ( $_FILES['imageUpload']['tmp_name'] ) // is the file uploaded yet?
{
    $filename = str_replace(array("(",")"), "-", str_replace(" ", "_", $_FILES['imageUpload']['name']));
    $filename_parts = pathinfo($filename);
    $filename = $filename_parts['filename'].'_'.time().'.'.$filename_parts['extension'];

    $info = explode('.', strtolower( $_FILES['imageUpload']['name']) ); // whats the extension of the file

    if ( in_array( strtolower(end($info)), $allow) ) // is this file allowed
    {
        $photo_file = $filename;


            if ( move_uploaded_file( $_FILES['imageUpload']['tmp_name'], $todir . basename($filename ) ) )
            {
                // the file has been moved correctly
                // Registration Success
                $query= "INSERT INTO posts (`post_image`) VALUES ('$photo_file')";
                mysql_query($query);
                echo "The file ". $photo_file. " has been uploaded.";
            }
            else
            {
                // uploading error
                echo 'Error: while file uploading but data has been saved';
            }

    }
    else
    {
        // error this file ext is not allowed
        echo 'Error: file ext is not allowed';
    }
}