PHP将上传的图像显示到其他页面

时间:2015-02-03 08:26:32

标签: php mysql file-upload dreamweaver

嗨,我有这个问题。用户在page1.php中上传图像。它的文件名插入数据库,图像将转到名为' uploads'你如何获得这些图像并将其显示到page2.php?

第1页

if(isset($_FILES['filename'])){
  $errors = array();
  $file_name = $_FILES['filename']['name'];
  $file_size =$_FILES['filename']['size'];
  $file_tmp =$_FILES['filename']['tmp_name'];
  $file_type=$_FILES['filename']['type'];   
  $file_ext=strtolower(end(explode('.',$_FILES['filename']['name'])));


  $expensions= array("jpeg","jpg","png");         
  if(in_array($file_ext,$expensions)=== false){
    $errors[]="extension not allowed, please choose a JPEG or PNG file.";
   }
    if($file_size > 2097152){
    $errors[]='File size must be excately 2 MB';
   }          

   //if no error...     
   if (empty($errors)==true) {

    // upload the file...
    move_uploaded_file($file_tmp,"uploads/".$file_name);

    $servername = "localhost";
    $username = "root";
    $password = " ";
    $dbname = "admin";

    // create new record in the database
   include ("dbinfo.php");

    mysql_query("INSERT INTO payment_form (Tracking, date, ContactNo, totalsent, datesent, filename) VALUES ('$transactionNo', NOW(), '$contactNo', '$totalSent', '$dateSent', '$file_name')") ;

    header('Location: paymentform_success.php');
     }else{
      print_r($errors);
     }
 }

第2页有一个更新记录表。我只想让图像显示在那里的细胞上。 T__T我没有研究任何对我有用的东西。请帮助

2 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

<?php
$query = "SELECT * FROM payment_form";
$result = mysql_query($query);
if (!$result) {
    die('SQL error');
}
echo "<table>";
while ($row = mysql_fetch_assoc($result)) {
    echo "<tr>";
    echo "<td>{$row['Tracking']}</td>";
    echo "<td>{$row['date']}</td>";
    echo "<td>{$row['ContactNo']}</td>";
    echo "<td>{$row['totalsent']}</td>";
    echo "<td>{$row['datesent']}</td>";
    echo "<td><img src='/uploads/{$row['filename']}'/></td>";
    echo "</tr>";
}
echo "</table>";
?>

首先你需要用&#34; SELECT &#34;运行mysql_query。查询,然后运行像 mysql_fetch_assoc 这样的函数来获取每一行,然后输出信息。

此外,您的原始代码存在SQL注入漏洞,您必须对用户提交的所有值使用 mysql_real_escape_string ,例如

$transactionNo=mysql_real_escape_string($transactionNo);
$contactNo=mysql_real_escape_string($contactNo);
$totalSent=mysql_real_escape_string($totalSent);
$dateSent=mysql_real_escape_string($dateSent);
$file_name=mysql_real_escape_string($file_name);

UPD 要通过跟踪ID获取特定文件,您可以使用以下内容:

<?php
$Tracking = mysql_real_escape_string($_GET['Tracking']);
$query = "SELECT Tracking,filename FROM payment_form WHERE Tracking = '$Tracking'";
$result = mysql_query($query);
if (!$result) {
    die('SQL error');
}
echo "<table>";
while ($row = mysql_fetch_assoc($result)) {
    echo "<tr>";
    echo "<td>{$row['Tracking']}</td>";
    echo "<td><img src='/uploads/{$row['filename']}'/></td>";
    echo "</tr>";
}
echo "</table>";
?>

答案 1 :(得分:0)

您执行的操作与我向您显示here相同,您可以使用$_GET或在表格中添加表单并使用$_POST。在您的验证链接下,您将回显每条记录的ID <a href="verify.php?id=<?php echo $id; ?>">verify link</a>

在下一页上使用$GET

if(isset($_GET['id'])){
  code here
}