【问题标题】:uploading images php mysql binary上传图片 php mysql 二进制文件
【发布时间】:2012-03-29 22:53:09
【问题描述】:

大家好,我在尝试上传时一直收到错误消息,没有选择图片,上传表单肯定是正确的名称和 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);
  ?>  

【问题讨论】:

  • var_dump($_FILES) 给你什么?要么你传入了一个空文件,要么你没有给文件输入正确的名称(它需要"image")。
  • 你不应该使用addlashes,而应该使用mysql_real_escape_string来确保你完全免受sql注入。

标签: php mysql image upload


【解决方案1】:

可能您的&lt;form&gt; 标签缺少enctype 参数。它应该是这样的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-14
    • 2016-07-20
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 2012-12-22
    相关资源
    最近更新 更多