Closed Thread
Thread Tools Search this Thread
Unread 21st Sep 2016, 01:13 AM   #1
Warrior Member
 
Join Date: 2016
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default
Use PHP Variable as Button Name Issue
Share on: 
fb share twitter share gplus share more share

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


Last edited on 23rd Sep 2016 at 02:21 AM.
alexwaston is offline  
Unread 27th Sep 2016, 09:15 PM   #2
Active Warrior
 
Join Date: 2016
Posts: 57
Thanks: 1
Thanked 11 Times in 10 Posts
Default
Re: Use PHP Variable as Button Name Issue
Share on: 
fb share twitter share gplus share more share

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
sunsetcoder is offline  
Unread 28th Sep 2016, 05:54 AM   #3
Bananas are Yellow!
Registered Member
War Room Member
 
David Beroff's Avatar
 
Join Date: 2013
Location: Wyoming, PA, USA
Posts: 1,046
Thanks: 2,549
Thanked 1,600 Times in 928 Posts
Blog Entries: 4
Default
Re: Use PHP Variable as Button Name Issue
Share on: 
fb share twitter share gplus share more share

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.

Put MY voice on YOUR video: AwesomeAmericanAudio.com
David Beroff is offline  
Closed Thread


Bookmarks

Tags
button, issue, php, variable

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 08:44 PM.