1

我需要帮助编写此代码以上传和保存上传的图像在数据库中。

文件 1:

<?php

      <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');
}

?>
4

3 回答 3

1

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

它应该是if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path))

删除@.

于 2013-07-19T06:03:12.490 回答
1

从技术上讲,如果它是一个小项目。您不应该将图像文件存储在“数据库”中;而只是他们的链接(你甚至可能不需要那个)。图像或任何媒体文件与您的其他文件(html、css、php)一起存储在服务器上。当然,您需要将它们放在专用文件夹中。不存储在数据库上的原因:因为它们仅用于数据检索,更重要的是它们的尺寸较小(确实存在较大的尺寸,我说的是对于需要最少资源的小型项目。将媒体文件存储在数据库中只是效率不高。

查看您的代码,我可以告诉您正在尝试将文件存储在您的服务器上。

他们使用了一个非常简单的脚本在这里上传。在尝试在服务器上之前先在本地主机上尝试。

于 2013-06-29T07:09:56.403 回答
0

我为它创建了三个页面

  1. index.php(上传图片的表格)

  2. upload.php (将图像保存在上传文件夹及其在数据库中的路径)

3.showimage.php(显示数据库中的图像)

数据库结构

id int(4) 自动增量 - image varchar(100) - image_name varchar(50)

这是代码:

索引.php

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

<label>Choose File to Upload:</label><br />

<input type="hidden" name="id" />

<input type="file" name="uploadimage" /><br />

<input type="submit" value="upload" />

</form>

上传文件

<?php
$target_Folder = "upload/";

$uid = $_POST['id'];

$target_Path = $target_Folder.basename( $_FILES['uploadimage']['name'] );

$savepath = $target_Path.basename( $_FILES['uploadimage']['name'] );

    $file_name = $_FILES['uploadimage']['name'];

    if(file_exists('upload/'.$file_name))
{
    echo "That File Already Exisit";
    }
    else
    {

        // Database 
    con=mysqli_connect("localhost","user_name","password","database_name"); //Change it if required

//Check Connection
        if(mysqli_connect_errno())
        {
            echo "Failed to connect to database" .     mysqli_connect_errno();
        }

        $sql = "INSERT INTO image (id,image, image_name)
                    VALUES     ('','$target_Folder$file_name','$file_name') ";

        if (!mysqli_query($con,$sql))
        {
            die('Error: ' . mysqli_error($con));
        }
        echo "1 record added successfully in the database";
        echo '<br />';
        mysqli_close($con);

        // Move the file into UPLOAD folder

        move_uploaded_file( $_FILES['uploadimage']['tmp_name'],     $target_Path );

        echo "File Uploaded <br />";
        echo 'File Successfully Uploaded to:&nbsp;' . $target_Path;
        echo '<br />';  
        echo 'File Name:&nbsp;' . $_FILES['uploadimage']['name'];
        echo'<br />';
        echo 'File Type:&nbsp;' . $_FILES['uploadimage']['type'];
        echo'<br />';
        echo 'File Size:&nbsp;' . $_FILES['uploadimage']['size'];

    }
?>

<a href="showimage.php">Show Image</a>

显示图像.php

<?php


$con=mysqli_connect("localhost","user_name","password","test"); //Change it if required

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM image " );


while($row = mysqli_fetch_array($result))
{
echo '<img src="' . $row['image'] . '" width="200" />';
echo'<br /><br />';  
}


mysqli_close($con);

?>
于 2013-11-14T09:34:10.813 回答