Solved

PHP - How to use a textbox and a button to remove a certain file?

Posted on 2011-09-09
6
205 Views
Last Modified: 2012-05-12
Hello everyone! The title may not be that clear what I'm trying to do. First of all, I have created multiple paths like this:

F:\MyFolder\1\testfile.txt
F:\MyFolder\2\testfile.txt
F:\MyFolder\3\testfile.txt

You notice that the only thing that changes in the path is the middle. Now my question. Can I use a textbox to determine the numbers 1, 2, 3 so If I for example type a 2 in the textbox and then click a button, the file
F:\MyFolder\2\testfile.txt will be removed. And if I type a 3 the file F:\MyFolder\3\testfile.txt will be removed.
Basically everything in the path stays the same only the second folder changes.

Thank you for your support,

einstein10
0
Comment
Question by:einstein10
  • 3
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
OmniUnlimited earned 500 total points
ID: 36515398
Sure.  You could set up PHP like this:

<?php

if (isset($_POST)) {
    $fileroot = "F:\MyFolder\";
    $filename = "\testfile.txt";
    $filepath = $fileroot . $_POST['fileindex'] . $filename;
    unlink($filepath);
}
echo '<form method="post" action="">';
echo '<label for "fileindex">Index of file to be deleted: <input type="text" name="fileindex" id="fileindex" value="' . $_POST['fileindex'] . '" />';
echo '<input type="submit" name="submit" value="Submit" />';
echo '</form>';
?>

Open in new window

0
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36515400
Sorry, forgot to close the <label> tag on line 10:
 
echo '<label for "fileindex">Index of file to be deleted: <input type="text" name="fileindex" id="fileindex" value="' . $_POST['fileindex'] . '" /></label>';

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36515405
This is the form:

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="filenumber" value="" />
<input type="submit" name="submit" value="Submit" />
</form>

This isthe php code at the top of the same page:

<?php
if isset($_POSR['submit']){
  $number=$_POST['filenumber'];
  $fname="F:\MyFolder\" . $number . "\testfile.txt";
  unlink($fname);
  echo "File deleted."
}
?>

Cheers
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36515408
Oooops: didn't refresh...
0
 

Author Closing Comment

by:einstein10
ID: 36515495
Thank you so much! You have done a great job.
0
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36515501
No problem.  Best of luck to you on your project!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now