如何使用PHP将上传的图像保存在数据库中 [英] how to save uploaded image in database with php

查看:92
本文介绍了如何使用PHP将上传的图像保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,编写此代码可以将上传的图像上传并保存到数据库中,但是不起作用,请帮帮我!

hello write this code to upload and save uploaded image in data base but don't work , please help me !!

文件1:

File 1:

      <form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >

                <label>File:  
                <input name="myfile" type="file" size="30" />
                </label>

                <input type="submit" name="submitBtn" class="sbtn" value="Upload" />


                  <iframeid="upload_target"name="upload_target"src="#"style="width:0;height:0;border:0px solid #fff;"></iframe>
                </form>
                </div>

文件2:

<?php




   // Edit upload location here
   $destination_path = getcwd().DIRECTORY_SEPARATOR;
   $target_path="my/";
   $result = 0;
   $name=$_FILES['myfile']['name'];
   $target_path = $target_path . basename( $_FILES['myfile']['name']);

   if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
   list($width, $height, $type, $attr) = getimagesize($target_path);
echo "Image width " .$width;
echo "<BR>";
echo "Image height " .$height;
echo "<BR>";
echo "Image type " .$type;
echo "<BR>";
echo "Attribute " .$attr;


      $result = 1;
   }

  // sleep(1);




$link=mysql_connect('localhost','root','');
if(!$link)
{die('you cannot connect to database...');}
$db=mysql_select_db('final');
if(!$db)die('smile');

if (isset($_FILES['myfile']) && $_FILES['myfile']['size'] > 0) {

// define the posted file into variables 
$name = $_FILES['myfile']['name']; 
$tmp_name = $_FILES['myfile']['tmp_name']; 
$type = $_FILES['myfile']['type']; 
$size = $_FILES['myfile']['size']; 

// if your server has magic quotes turned off, add slashes manually 
//if(!get_magic_quotes_gpc()){ 
//$name = addslashes($name); 
//} 

// open up the file and extract the data/content from it 
$extract = fopen($tmp_name, 'r'); 
$content = fread($extract, filesize($tmp_name)); 
$content = addslashes($content); 
fclose($extract);  

// connect to the database 


// the query that will add this to the database 
$s=mysql_query("SET NAMES 'utf8'");
$sql = "INSERT INTO `final`.`products` (`Productcode`, `Productname`, `Price`,`Descriptionofgood`, `image`) VALUES ('','','','','".$target_path."') WHERE `products`.`Productcode`='1371' ";
$results = mysql_query($sql);
if(!$result)die('not');
}

?>

推荐答案

if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path))不正确.

应为if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path))

删除@.

这篇关于如何使用PHP将上传的图像保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆