douguazhi5966 2015-02-03 08:26
浏览 31
已采纳

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 2015-02-03 08:45
    关注

    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>";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记