PHP Display uploaded image to a different page

强颜欢笑 提交于 2019-12-13 03:47:52

问题


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


回答1:


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



回答2:


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
}


来源:https://stackoverflow.com/questions/28294350/php-display-uploaded-image-to-a-different-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!