1

我想使用 php 将图像上传到 mysql 服务器。我已经创建了 html 和 sql 连接,但图像上传显示错误。我无法上传图片,它显示有效图片错误,即您必须上传jpeg,bmp,gif;并在目录中读/写。

any1可以帮我解决这个问题,php文件是

    <?php
//Start session
session_start();

//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

// Check to see if the type of file uploaded is a valid image type
function valid($file)
{
    // This is an array that holds all the valid image MIME types
    $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif");

    //echo $file['type'];
    if (in_array($file['type'], $valid_types))
        return 1;
    return 0;
}

// Build our target path full string.  This is where the file will be moved do
// i.e.  images/picture.jpg
$TARGET_PATH = "image/";
$TARGET_PATH = $TARGET_PATH . basename( $_FILES['image']['name']);

$pimage = $_FILES['image']['name'];

// Check to make sure that our file is actually an image
    // You check the file type instead of the extension because the extension can easily   be faked
    if (!valid($pimage))
    {
   $_SESSION['ERRMSG_ARR'] = array('You must upload a jpeg, gif, or bmp');
   header("Location: admin.php");
   exit;
     }

// Here we check to see if a file with that name already exists
   // You could get past filename problems by appending a timestamp to the filename and then      continuing
   if (file_exists($TARGET_PATH))
   {
 $_SESSION['ERRMSG_ARR'] = array('A file with that name already exists');
 header("Location: admin.php");
     exit;
   }

 // Lets attempt to move the file from its temporary directory to its new home
   if (move_uploaded_file($_FILES['image']['tmp_name'], $TARGET_PATH))
   {
  // NOTE: This is where a lot of people make mistakes.
  // We are *not* putting the image into the database; we are putting a reference to the     file's location on the server
   $sql = "insert into people (p_category, p_name, p_quantity, p_desc, p_image) values        ('$pcategory', '$pname','$pquantity','pdesc', '" . $pimage['name'] . "')";
   $result = mysql_query($sql);
    //Check whether the query was successful or not
      if($result) {
    $_SESSION['ERRMSG_ARR'] = array('Product added');;
    $_SESSION['MSG_FLAG'] = 0;
    session_write_close();
    header("location: admin.php");
    exit();
     }else {
    die("Query failed: ".mysql_error());
        }
       }
       else
       {
     // A common cause of file moving failures is because of bad permissions on the     directory attempting to be written to
     // Make sure you chmod the directory to be writeable
     $_SESSION['ERRMSG_ARR'] = array('Could not upload file.  Check read/write persmissions on the directory');
      header("Location: admin.php");
      exit;
       }
?>
4

2 回答 2

1

我认为

$pimage = $_FILES['image']['name'];

应该

$pimage = $_FILES['image'];

您可能错过了这一点,因为您的代码非常不一致 - 有时您使用$pimage,而在其他地方您直接引用$_FILES数组。如果文件字段的名称发生更改,这使得维护变得更加困难。如果不是数组,您还可以键入提示valid()使 PHP 抱怨的函数:$file

function valid(array $file) { ... }

您设置了什么级别的错误报告?它会突出显示错误,例如尝试访问未定义的数组键。

于 2013-09-17T17:49:45.063 回答
0

看到您在行中传递图像类型if (!valid($pimage)) 但是在 valid() 函数中,您再次尝试获取图像的类型$file['type']

乔治所说的也应该有效,但是由于您正在为图像类型$ptype和名称创建变量,因此$pimage您可以自己使用它们。

所以更改应该$file['type']变成$file并且$file['type']插入查询中的 &$pimage['name']变成$pimage

我相信这可以解决它,Bahua ;)

于 2013-09-17T19:04:08.053 回答