Home > Enterprise >  How to change an image in the table php?
How to change an image in the table php?

Time:03-01

I have a table that receives some data, but I want to change an image of the table according to the filename Let's say I have a pdf file I want to set a table row with this image.

Below you will see my code files logic.php

if (isset($_POST['save'])) { // if save button on the form is clicked
    // name of the uploaded file
    $filename = $_FILES['myfile']['name'];
    $clientname = $_POST['FileName'];
    // destination of the file on the server
    $destination = 'uploads/' . $filename;
    

    // get the file extension
    $extension = pathinfo($filename, PATHINFO_EXTENSION);

    // the physical file on a temporary uploads directory on the server
    $file = $_FILES['myfile']['tmp_name'];
    $size = $_FILES['myfile']['size'];

    if (!in_array($extension, ['zip', 'pdf', 'docx','rar','jpeg','jpg','png','psd'])) {
        echo "You file extension must be .zip, .pdf or .docx ";
        
    } elseif ($_FILES['myfile']['size'] > 10000000000) { // file shouldn't be larger than 1Megabyte
        echo "File too large!";
    } else {
        // move the uploaded (temporary) file to the specified destination
        if (move_uploaded_file($file, $destination)) {
            $sql = "INSERT INTO files (name, size, downloads,Cliente) VALUES ('$filename', $size, 0,'$clientname')";
            if (mysqli_query($conn, $sql)) {
                echo "File uploaded successfully";

                header('Location: enviar.php');
                exit;

            }
        } else {
            echo "Failed to upload file.";
        }
    }
    if ($extension= ['zip', 'pdf', 'docx','rar','jpeg','jpg','png','psd']) {
        echo "is taking the file";
    }
    
}

This is the confirmation field "if ($extension= ['zip', 'pdf', 'docx','rar','jpeg','jpg','png','psd']) { echo "is taking the file"; }" Here is my table

 <table id ="myTable">

    
<thead>

    <th>Nome</th>
    <th>Arquivo</th>
    <th>Tamanho(em mb)</th>
    <th>Downloads</th>
    <th>Opções</th>
</thead>
<tbody>

  <?php foreach ($files as $file  ): 
    
  
    
   
    ?>
    
    
    <tr class ="header">
    
    
    <td><?php echo $file['Cliente']; ?></td>
      
      
       <td><?php echo $file['name']; ?> <img src="uploads/pdficon.png" alt="" ></img></td>
      <td><?php echo floor($file['size'] / 1000) . ' KB'; ?></td>
      <td><?php echo $file['downloads']; ?></td>
      
    <td ><a href="downloads.php?file_id=<?php echo $file['id'] ?>">Download</a>  <a href="delete.php?id=<?=$file['id']?>" >Apagar</a></td>
    

   
  </tr>
  
  <?php endforeach;?>
  
  

</tbody>
</table>

CodePudding user response:

If you build an associative array of the filenames of your icons, one for each file type, where the extension is the key, mapped to the icon filename as the value, then you can simply get the file extension for each file in your list, and get the correct icon name from your array.

For example:

$fileTypeImages = array("docx" => "docx.png", "jpg" => "jpg.png", "zip" = "zipfile.jpg"` 

...etc.

And then:

$fileExt = pathinfo($file['name'])['extension'];
...

<td><?php echo $file['name']; ?> <img src="uploads/<?php echo $fileTypeImages[$fileExt]; ?>" alt="" ></img></td>

(See How to get a file's extension in PHP? for the reference to getting a file's extension from its name.)

  • Related