Use PHP Variable as Button Name Issue

2 replies
I use this code as reference. I won't paste the link where I got this since I am not sure if it is okay to paste external sites.

PHP Code:
<?php $files glob("images/*.*"); 
for (
$i=1$i<count($files); $i++) { 
   
$num $files[$i]; 
   print 
$num."<br />";
    echo 
'<img src="'.$num.'" alt="random image" />'."<br /><br />"
    } 
?>
Here's what I have. I need a button that will delete specific file. The code works but sometimes it deletes the wrong file.

Note: ob_start( ) is placed at the start of the page for the header to work.

PHP Code:
<?php
               $files 
glob("folder1/*.jpg*");
               
array_multisort(
               
array_map'filemtime'$files ),
               
SORT_NUMERIC,
               
SORT_DESC,
               
$files
               
);
               for (
$i=0$i<count($files); $i++)                  
               {
                  
$image $files[$i];
echo 
"               <div class=\"boxout\">\n";
echo 
"                  <div class=\"label\">\n";
                        print 
basename($image);
echo 
"                  </div>\n";?>
                     <div class="label">
                        <form><input type="submit" name="select<?php echo $i;?>" value="Move" id="backup" class="backup"/></form>
                     </div>
<?php                     if(isset($_GET["select".$i]) ) {

                        
file_put_contents('backup.txt'"This is ".basename($image".jpg").chr(32).chr(34)."Some
                        strings."
.chr(34)."\n"FILE_APPEND LOCK_EX);
                        
                        
rename($image,"folder1/images/page1_".basename($image));
                        
                        
unlink($image);
                        
                        
ob_end_clean( );
                        
                        
header("Location: Page1.php");
                        }
Please need help on this. I just started on this php thing last month so I am really a newbie on this. Any help would be awesome.

Relax: watch pete's dragon | watch bad moms | watch suicide squad
#button #issue #php #variable
  • Profile picture of the author sunsetcoder
    I would advice you to rename file while uploading to the specific folder or server so you will have unique name and it would better for you to delete it
    {{ DiscussionBoard.errors[10864653].message }}
  • Profile picture of the author David Beroff
    Two things strike me when I take a look at this:

    1. The code is referencing files by the iterator from the loop, and you don't have much guarantee that this will remain consistent, (thus hitting the wrong file). Why not simply use the file name instead of a number?

    2. HTML forms generally need two files, one to display the form, and one to take action, (though one can do this with a single file). I'm not against using a single file, but when you're first learning, it may be easier to use two.
    Signature
    Put MY voice on YOUR video: AwesomeAmericanAudio.com
    {{ DiscussionBoard.errors[10865029].message }}

Trending Topics