上传图片php mysql二进制文件

时间:2012-03-29 22:53:09

标签: php mysql image upload

大家好我不断收到错误,当我尝试上传时没有选择图片,上传表格肯定是正确的名称和ID相同的上传所以它必须是代码有问题,谁能看到原因?

<?php


// Create MySQL login values and 
// set them to your login information.
$username = "**";
$password = "**";
$host = "**";
$database = "**";

// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
 if (!$link) {
      die('Could not connect: ' . mysql_error());
   }

   // Select your database
    mysql_select_db ($database);  

     session_start();
     if(!isset($_SESSION['username']))
    {
          die('You have no access to this page.');
    }
  else{
  $username = $_SESSION['username'];
   // Make sure the user actually 
   // selected and uploaded a file
    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

  // Temporary file name stored on the server
  $tmpName  = $_FILES['image']['tmp_name'];  

  // Read the file 
  $fp      = fopen($tmpName, 'r');
  $data = fread($fp, filesize($tmpName));
  $data = addslashes($data);
  fclose($fp);


  // Create the query and insert
  // into our database.
  $query = "INSERT INTO Members WHERE username = '$username' ";
  $query .= "(image) VALUES ('$data')";
  $results = mysql_query($query, $link);

  // Print results
  print "Thank you, your file has been uploaded.";

 }
else {
 print "No image selected/uploaded";
 }
 }
  // Close our MySQL Link
  mysql_close($link);
  ?>  

1 个答案:

答案 0 :(得分:0)

您的<form>代码可能缺少enctype参数。它应该是这样的:

<form action="index.php?action=upload" method="post" enctype="multipart/form-data">