适当的Systax /用于PHP文件上传

时间:2014-03-29 06:47:13

标签: php

我正在将这个系统用于我们的学校项目。但是,我找不到当前问题的答案。

程序流程如下:

  • 注册用户将上传包含
  • 表格文本的文件
  • 注册文件将出现在全球市场(自己的描述)

问题:

  • 无法上传图片
  • 我为此使用了简单的文件上传,但我找不到正确使用它的方法

这是我的代码:

<table border=1>
    <th colspan=2>Sell your Item! </th>
<tr>
    <td colspan=2><input type=file id=file>Product Name:<input type=text name=productName>

上传表格:

 <?php

        if (ISSET($_GET['submit']))
            {

            move_uploaded_file($_FILES['image']['tmp_name'],"images/".$_FILES['image']["name"]."-owner-".$_SESSION['username']."-firstname-".$_SESSION['f_name'])
             $showimage = "images/".$_FILES['image']["name"];
                include ("dbconnect.php");

                    $a = $_SESSION['username'];
                    $b = $_GET['info'];
                    $d = $_GET['productName'];
                    $e = $_GET['category'];
                    $f = $_GET['price'];




                    $query = "INSERT INTO `products`(`product_name`,`product_category`,`product_info`,`product_owner`,`product_price`) VALUES ('$d','$e','$b','$a','$f')";
                    $result = $con->query($query);

                echo "<font size=5px color=red><center>Product successfully posted on the online market</center></font>";
            }

    ?>

我之前使用过简单的图像/文件上传,现在可以使用了,我必须将它用于我的项目。

为了在resultIFrame上显示图像,我使用下面的代码:

 <?php
   if(isset($_GET['search']))
{
    $a=$_GET['searchbar'];
    include("dbconnect.php");
    $sql = "SELECT * FROM products WHERE product_name LIKE '%$a%'";
    $result=mysqli_query($con,$sql);
    $bilang=mysqli_num_rows($result);

        if($bilang==0)
            {
            print "<font color=red>No existing product";
            }
        else 
            {
            while ($rows=mysqli_fetch_array($result))
                {   
                    $a=$rows['product_name'];
                    $b=$rows['product_category'];
                    $c=$rows['product_info'];
                    $d=$rows['product_owner'];
                    $e=$rows['product_price'];
                    $_SESSION['product_name']=$a;

                    print "<table border=1>
                        <tr>
                            <td height=250px width=150px>

                                <center></br><img src='$product_image_path'></br>
                                <center><b>$a</b>
                                    </br>$c
                                    </br>$b
                                </br><b>$e</b>
                                </br>$d
                                </br><center><a href='about.php?product_name=$a&product_category=$b&product_info=$c&product_owner=$d'>
                                Learn more...</a>
                            </td>


                            ";
                }   
        //print "<tr><td colspan=6><center>Total Record/s found : <b>$bilang</b></center></table>";
            }
}

&GT;

1 个答案:

答案 0 :(得分:1)

你写了一个错误的代码,你没有存储上传文件的文件路径。没有它你将无法查看文件

HTML

<form enctype="multipart/form-data" method="POST" action="youfilename.php">
.........
.....
. 
<input type="file" name="image">
.........
.......
.
</form>

代码

if (isset($_POST['submit']) && (!empty($_FILES["image"]) && $_FILES['image']['error'] == 0))
{
$filename = "img-owner-" . $_SESSION['username'] . "-firstname-" . $_SESSION['f_name'] . $_FILES['image']["name"];
if (move_uploaded_file($_FILES['image']['tmp_name'], 'img/' . $filename))
    {
    include ("dbconnect.php");

    $a = $_SESSION['username'];
    $b = $_POST['info'];
    $d = $_POST['productName'];
    $e = $_POST['category'];
    $f = $_POST['price'];
    $query = "INSERT INTO products(product_name,product_category,product_info,product_owner,product_price,product_image_path)
     VALUES ('$d','$e','$b','$a','$f','$filename')";
    $result = $con->query($query);
    if ($result)
        {
        echo "<font size=5px color=red><center>Product successfully posted on the online market</center></font>";
        }
    }
}


else
    {
    echo "Your error message";
    }

同时更改您的产品表并添加另一列product_image_path或您能想到的任何其他相关名称。

要查看图像,只需从表中提取值并使用此代码

<?php
       if(isset($_GET['search']))
    {
        $a=$_GET['searchbar'];
        include("dbconnect.php");
        $sql = "SELECT * FROM products WHERE product_name LIKE '%$a%'";
        $result=mysqli_query($con,$sql);
        $bilang=mysqli_num_rows($result);
        if($bilang==0)
                {
                print "<font color=red>No existing product";
                }
            else 
                {
                while ($rows=mysqli_fetch_array($result))
                    {   
                        $a=$rows['product_name'];
                        $b=$rows['product_category'];
                        $c=$rows['product_info'];
                        $d=$rows['product_owner'];
                        $e=$rows['product_price'];
                        $product_image_path=$row['product_image_path'];
                        $_SESSION['product_name']=$a;

                        print "<table border=1>
                            <tr>
                                <td height=250px width=150px>

                                    <center></br><img src='$product_image_path'></br>
                                    <center><b>$a</b>
                                        </br>$c
                                        </br>$b
                                    </br><b>$e</b>
                                    </br>$d
                                    </br><center><a href='about.php?product_name=$a&product_category=$b&product_info=$c&product_owner=$d'>
                                    Learn more...</a>
                                </td>


                                ";
                    }   
            //print "<tr><td colspan=6><center>Total Record/s found : <b>$bilang</b></center></table>";
                }
    ?>