PHP将上传的图片显示到其他页面 [英] PHP Display uploaded image to a different page

查看:155
本文介绍了PHP将上传的图片显示到其他页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了麻烦。用户在page1.php中上传图像。它的文件名插入到数据库中,并且图像进入名为 uploads的文件夹。如何获取这些图像并将其显示到page2.php?

Hi I'm having trouble with this. A user uploads an image in page1.php. It's filename inserts to a database and the image goes to a folder named 'uploads' How do you get those images and show it to 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我尚未研究对我有用的任何东西。请帮助

page 2 has an update records table. I just want the image to show on a cell there. T__T I haven't researched anything that worked for me.Please help

推荐答案

您可以尝试以下方法:

<?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>";
?>

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

first you need to run mysql_query with "SELECT" query, then run a function like mysql_fetch_assoc to get each row and then output the information.

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

Also, your original code has a SQL injection vulnerability, you have to use mysql_real_escape_string for all values submitted by user, e.g.

$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获取特定文件,您可以使用类似以下内容:

UPD To get specific file by tracking id you can use something like this:

<?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>";
?>

这篇关于PHP将上传的图片显示到其他页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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