55365

Add to an uploaded images table the choice of dele

Question:

I have a page where I can upload some images, see the list of images loaded in a table. I want to add the chance to delete one of the images in the list from the server. I tried but something in this code is not working. Any help?

<html> <body> <form action="" method="post" enctype="multipart/form-data"> Select a photo to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Load" name="submit"> </form> <?php $dir = 'up/'; $files = scandir($dir); $maxnum = count($files); ?> <table class="thumbnail"> <?php $i = 2; for ($j = 0; $j < $maxnum; $j++) { echo '<tr>'; $k = $i + 5; for ($i; $i < $k; $i++) { If ($i == $maxnum) { break; } echo '<td><a href="'.$dir.$files{$i}.'"><img src="'.$dir.$files{$i}.'"></a> </td>'; if (isset($_GET['delete'])) { unlink($_GET['delete']); $_SESSION['delete'] = $_GET['delete']; unset($_GET['delete']); $url = $_SERVER['SCRIPT_NAME'].http_build_query($_GET); header("Refresh:0; url=".$url); } ?> <a href='index.php?delete=up/<?php echo $key?>' id="button">Delete now</a> } echo "</tr>"; } ?> </table> </div> </body> </html> <?php if (isset($_POST['submit'])) { echo '<center><h1>succesfully loaded!</h1></center>'; $structure = 'up/'; $target_file = $structure.basename($_FILES["fileToUpload"]["name"]); move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file); } ?>

Answer1:

Mistakes that have done :

  1. Not closing the loop }} at the end

  2. Not giving the proper file path $dir.$files{$i} in the anchor tag.

Here's the eval

Here's the code :

<html> <body> <form action="" method="post" enctype="multipart/form-data"> Select a photo to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Load" name="submit"> </form> <?php $dir = 'up/'; $files = scandir($dir); $maxnum = count($files); ?> <table class="thumbnail"> <?php $i = 2; for ($j = 0; $j < $maxnum; $j++) { echo '<tr>'; $k = $i + 5; for ($i; $i < $k; $i++) { If ($i == $maxnum) { break; } echo '<td><br><a href="'.$dir.$files{$i}.'"><img src="'.$dir.$files{$i}.'" height="50" width="50"></a> </td>'; if (isset($_GET['delete'])) { unlink($_GET['delete']); $_SESSION['delete'] = $_GET['delete']; unset($_GET['delete']); $url = $_SERVER['SCRIPT_NAME'].http_build_query($_GET); header("Refresh:0; url=".$url); } ?> <a href='index.php?delete=<?php echo $dir.$files{$i}?>' id="button">Delete now</a> </table> </div> </body> </html> <?php if (isset($_POST['submit'])) { echo '<center><h1>succesfully loaded!</h1></center>'; $structure = 'up/'; $target_file = $structure.basename($_FILES["fileToUpload"]["name"]); move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file); } }} ?>

Note :

The first anchor tag might look at left aligned. It's just a design issue ;)



Answer2:

I've found two problems: you forgot a <?php tag and your delete link was wrong

So the code:

<html> <body> <form action="" method="post" enctype="multipart/form-data"> Select a photo to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Load" name="submit"> </form> <?php $dir = 'up/'; $files = scandir($dir); $maxnum = count($files); ?> <table class="thumbnail"> <?php $i = 2; for ($j = 0; $j < $maxnum; $j++) { echo '<tr>'; $k = $i + 5; for ($i; $i < $k; $i++) { If ($i == $maxnum) { break; } echo '<td><a href="'.$dir.$files{$i}.'"><img src="'.$dir.$files{$i}.'"></a> </td>'; if (isset($_GET['delete'])) { unlink($_GET['delete']); $_SESSION['delete'] = $_GET['delete']; unset($_GET['delete']); $url = $_SERVER['SCRIPT_NAME'].http_build_query($_GET); header("Refresh:0; url=".$url); } ?> <a href='index.php?delete=<?php echo $dir.$files{$i}?>' id="button">Delete now</a> <?php //<--you forgot this } echo "</tr>"; } ?> </table> </div> </body> </html> <?php if (isset($_POST['submit'])) { echo '<center><h1>succesfully loaded!</h1></center>'; $structure = 'up/'; $target_file = $structure.basename($_FILES["fileToUpload"]["name"]); move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file); } ?>

Recommend

  • Is there a fix or a workaround for the memory leak in getpwnam?
  • How to Autoload & Display Images in View (Codeigniter)
  • How to add wildcard names to directory search in php
  • Scan current folder using PHP
  • Valums Ajax file Upload handle the up. file?
  • Argument 5: cannot convert from 'System.Drawing.Image' to 'string' - calling cla
  • Save image as is in photo album using swift
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • List images(01.png) and descriptions(01.txt) from directory
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Copy to all folders batch file?
  • Button click event not firing in jQuery
  • Loading .coffee files via a view in Rails
  • Create DicomImage from scratch using Dcmtk
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • How to render a blob on a canvas element?
  • How to test if a URL from an Eclipse bundle is a directory?
  • JSON response opens as a file, but I can't access it with JavaScript
  • FFmpeg Conversion Error
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • req.body is undefined - nodejs
  • what is the difference between the asp.net mvc application and asp.net web application
  • Upload files with Ajax and Jquery
  • jquery mobile loadPage not working
  • Do I've to free mysql result after storing it?
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • Matrix multiplication with MKL
  • A cron job substitute?
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • How to stop GridView from loading again when I press back button?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Busy indicator not showing up in wpf window [duplicate]
  • Binding checkboxes to object values in AngularJs
  • How to load view controller without button in storyboard?