douguazhi5966
douguazhi5966
2015-02-03 08:26

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?

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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dpwbc42604 dpwbc42604 6年前

    You can try something like this:

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

    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.

    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 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>";
    ?>
    
    点赞 评论 复制链接分享
  • doufu9521 doufu9521 6年前

    You do the same thing as i have shown you here, you can either use $_GET or include a form in your table and use $_POST. under your verify link you echo the id of each record <a href="verify.php?id=<?php echo $id; ?>">verify link</a>

    on the next page you use $GET

    if(isset($_GET['id'])){
      code here
    }
    
    点赞 评论 复制链接分享

相关推荐