Solved

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

Posted on 2011-09-09
6
206 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

815 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

12 Experts available now in Live!

Get 1:1 Help Now