For a file management I use a foreach loop in which all the files are displayed. Now I want foreach to delete files with $_POST.
When I echo pathinfo($dir.'/'.$file, PATHINFO_DIRNAME).'/'.$file; in the foreach near each file, it shows me the exact location of the files.
Now for each file I want do something like this in the foreach loop:
if(isset($_POST['delete'])) {
unlink($_POST['unlink']);
}
<form method="post">
<input type="hidden" name="unlink" value="<?php pathinfo($dir.'/'.$file, PATHINFO_DIRNAME).'/'.$file; ?>" />
<input type="submit" name="delete" value="Delete">
</form>
I know this is not correct but i hope you understand that what i want to achieve is clear.
How can I do this?
DISCLAIMER
The method outlined below is 100% insecure and if the code makes it into the public realm then you can kiss your server goodbye.
For deleting one file you can do this:
index.php
<form method="POST" action="delete.php">
<input type="hidden" name="delete_path" value="/path/to/your/file.pdf">
File.pdf <button type="submit">Delete</button>
</form>
delete.php
<?php
$delete_result = false;
$unlink_error = '';
if(isset($_POST['delete_path']) && is_file($_POST['delete_path']))
{
$delete_result = unlink($_POST['delete_path']);
$unlink_error = error_get_last();
}
if($delete_result)
{
echo 'Successfully deleted '.$_POST['delete_path'];
}
else
{
echo 'Uh oh, Spaghettios :(<br><br>';
echo 'Last known error<br>'.$unlink_error;
}
I am use path unlink(FILE)
echo $new_file_dert = FILE . '/'.$template_name;
<form method="post">
<input type="hidden" name="unlink" value="<?php echo $new_file_dert; ?>" />
<input name="delete" type="submit" class="btn-delete submitdelete" value="Delete">
</form>
<?php
echo $new_file_dert = FILE . '/'.$template_name;
if( isset( $_POST['delete'] ) ) {
unlink($_POST['unlink']);
}
Related
I have two php files insert.php and function.php
In insert.php
<form action="<?php echo htmlspecialchars( $_SERVER['PHP_SELF'] ); ?>"
method="post">
<input type="hidden" name="Entry" value="<?php print $data; ?>"/>
<hr>
<h3>Entry your new entry</h3>
<?php
include('function.php');
$query_start = "SHOW COLUMNS FROM $data";
$result = mysqli_query( $conn, $query_start );
while($row = mysqli_fetch_array($result)){?>
<label for="<?php echo "$row[0]";?>"><?php display_text($row[0]);?></label>
<br>
<?php display_value($row[0]);
} ?>
<input class="btn btn-primary" type="submit" name="add" value="Add Entry">
</form>
And in function.php
<?php
function display_value($row){?>
<input class='form-control' type='text' placeholder='".$row."' name='".$row."'>
}?>
But I am not getting the form on the web page.
firstly you need to add a <form> tag
<?php
include('function.php');
$query_start = "SHOW COLUMNS FROM $data";
$result = mysqli_query( $conn, $query_start );
while($row = mysqli_fetch_array($result)){
$data = display_value($row[0]);
echo $data;
}
?>
you're just calling that function, for printing the values inside the function, you need to return data from function and store or print the data from you're calling.
$data = display_value($row[0]);
here variable $data will store the output, also you need to return the result from your function
<?php
function display_value($row) {
$data = '<input class="form-control" type="text" placeholder="'. $row .'" name="'. $row .'"><br /><br />';
return $data; //this return will be printed on another page
} ?>
I guess with "form" he meant the fields not the form tag.
#Rohit Sahu answer is wrong.. you dont need to pass the output with a return. Your way of doing it is not a beautiful way. But it is even working this way.
Are you sure your Mysql Query give results?
Example Code:
<?php
// functions.php
function display_value($row) { ?>
<input class='form-control' type="text" placeholder="<?php echo $row;?>" name="<?php echo "$row";?>"><br><br>
<?php } ?>
// insert.php
<form action="....">
<?php
$arr = array(1,2,3,4);
foreach($arr as $row) {
display_value($row);
}?>
</form>
Output:
<form action="....">
<input class='form-control' type="text" placeholder="1" name="1"><br><br>
<input class='form-control' type="text" placeholder="2" name="2"><br><br>
<input class='form-control' type="text" placeholder="3" name="3"><br><br>
<input class='form-control' type="text" placeholder="4" name="4"><br><br>
</form>
Result: It is working fine. You should better post all code to understand where is your mistake. It could be some whitespaces in files, it could be no results from MYSQL.. there are many options. It is difficult to say this way.
i have an image from my database in a different table displayed as $draft['draft_image'] and i want to send this data to a different table but im having a problem cause this code doesnt seems to work.. when i tried to click the published button there is no data that is being sent.i use var_dump to show the inserted data but nothing shows up in the inspect element.. any idea how can i get the image? this is what's inside my draft_image.. ../images/article/md.png
<form method="POST" enctype="multipart/form-data">
<img data-name="title_image" src="./<?= $draft['draft_image'];?>" style="margin-top:5px;" width="70%" height="55%"><br><br>
<?php
}
?>
<button type="submit" name="submit" class="btn3 btn-default">Publish</button>
</form>
if (isset($_POST['submit'])) {
$id = $session_id;
$file = $_POST['title_image'];
$ress = $db->draftpublished($id,$file);
echo $ress;
}
?>
You can pass the value using hidden type ex:
<form method="POST" enctype="multipart/form-data">
<img data-name="title_image" src="./<?= $draft['draft_image'];?>" style="margin-top:5px;" width="70%" height="55%"><br><br>
<input type="hidden" name="imgsrc" value="<?= $draft['draft_image'];?>">
<?php
}
?>
<button type="submit" name="submit" class="btn3 btn-default">Publish</button>
</form>
if (isset($_POST['submit'])) {
$id = $session_id;
$file = $_POST['imgsrc'];
$ress = $db->draftpublished($id,$file);
echo $ress;
}
?>
<form method="POST" enctype="multipart/form-data">
<!-- added ----------------- -->
<input type="hidden" name="title_image" value="<?=$draft['draft_image']?>">
<img data-name="title_image" src="./<?= $draft['draft_image'];?>" style="margin-top:5px;" width="70%" height="55%"><br><br>
<?php
}
?>
<button type="submit" name="submit" class="btn3 btn-default">Publish</button>
</form>
if (isset($_POST['submit'])) {
$id = $session_id;
$file = $_POST['title_image'];
$ress = $db->draftpublished($id,$file);
echo $ress;
}
?>
I have tried looking through other posts here but I haven't been able to find a solution to my particular problem.
Issue: After selecting a checkbox or more and submitting nothing happens to the images and all form elements disappear.
Goal: I want the images selected to be delete when submitted to the $_POST array. Additionally, I want the remaining files to be renamed (like an array shift) For example: If I have 6 images, Image-3, and Image-5 to be deleted, image 6 should be renamed to image 5, and etc.
Here is my current code:
PHP:
if($_POST['delete_img']){
if ($images){
if (!empty($_POST['delete'])){
$delete = $_POST['delete'];
print_r($delete);
for ($i =(count($delete) - 1); $i >= 0; $i--){
// Determine the images to be deleted
$images_to_delete = "profiles/".$user_id."/".$user_id."-".$delete($i).".jpg";
// Delete the images
echo "Deleting image: " . $i;
recursiveDelete($images_to_delete);
for ($j = $delete($i); $j < $images; $j++){
echo "checkpoint";
// Rename the files
rename("profiles/".$user_id."/".$user_id."-".$j+1 . ".jpg");
// Decrement the images number for the db
$new_images_num = $images-1;
// Update the database
$image_info = array($new_image_num,$user_id);
$result = pg_execute($conn, "update_images", $image_info);
}
}
}
else{
$error .= "You must select an image to delete.";
}
}
else{
echo "There are no images to delete";
}
}
HTML:
<form id="uploadform" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="profile_images">
<?php
echo $profile_images;
?>
</div>
<br/>
<strong> Select image to upload: </strong>
<input name="uploadfile" type="file" id="uploadfile" />
<input type="submit" value="Upload" name="upload" />
<input type="submit" value="Delete" name="delete_img" />
<input type="submit" value="Save" name="save" />
</form>
I think this is what you are trying to do which is 1) delete any selected file(s), then reset all the names to incrementing. You will have to change the directory names and naming conventions:
function getFileList($dir)
{
// Filter out the inevitable dots..
$filter = array(".","..");
// Scan the target directory
$fileList = scandir($dir);
// Just return false if folder empty
if(empty($fileList))
return false;
// Return filtered array
return array_values(array_diff($fileList,$filter));
}
$dir = __DIR__.'/testimg';
if(!empty($_POST['delete'])) {
foreach($_POST['delete'] as $i => $dVal) {
if(!empty($_POST['delete'][$i])) {
$fName = $dir.'/user'.$i.".jpg";
if(is_file($fName)) {
if(unlink($fName))
echo 'Deleted: '.$fName;
}
}
}
// See if any files remain in folder
$files = getFileList($dir);
// Rename any files in the folder
if($files) {
if(count($files) > 0) {
$i = 1;
foreach($files as $name) {
if(rename($dir."/".$name, $dir.'/user'.$i.".jpg"))
$i++;
}
}
}
}
// Check one last time
$files = getFileList($dir);
?>
<form id="uploadform" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="profile_images">
<?php
if($files) {
$i = 1;
foreach($files as $key => $value) {
if(is_file($img = $dir."/".$value)) {
echo '<img src="'.str_replace(__DIR__,"",$img).'" style="max-height: 100px;" />IMAGE '.$i.'<input type="checkbox" name="delete['.$i.']" />';
$i++;
}
}
}
?>
</div>
<br/>
<strong> Select image to upload: </strong>
<input name="uploadfile" type="file" id="uploadfile" />
<input type="submit" value="Upload" name="upload" />
<input type="submit" value="Delete" name="delete_img" />
<input type="submit" value="Save" name="save" />
</form>
I'm trying to delete specific images in a database through PHP.
I have a page where all images in the database are displayed and I wanted a button under each one of them so I could delete them individually through their id but I don't know how.
Here's the PHP code for showing all images:
<?php
$result = mysqli_query($con, "SELECT * FROM galeria");
?>
<h5>Images:</h5>
<?php
while ($row = mysqli_fetch_array($result)) {
?><h6> <?php echo $row['titleimg']; ?></h6>
<p><?php echo $row['events_id']; ?></p>
<img src="../images/<?php echo $row["img"]; ?>" width="301px" height="200px"/>
<form action="delete_images.php" method="post">
<input type="submit" name="delete" value="Delete" />
</form>
<?php
echo "<br>";
echo "<br>";
}
?>
So now, what's the code I should have in my "delete_images.php" file?
Your form needs an additional piece of information, an identifier for the image to be deleted. Something like:
<form action="delete_images.php" method="post">
<input type="hidden" name="id" value="<?php echo $row['img_id'] ?>" />
<input type="submit" name="delete" value="Delete" />
</form>
Naturally, I'm guessing on the column name (img_id), but any identifier for that specific image will do the trick. With that, your POST to delete_images.php will have that value (in $_POST['id']) and can use it in the DELETE query to the database.
Put a hidden input field that will contain the imageName to which you want to delete.
<input type="hidden" value="'.$row["img"].'" name="imageName" />
// Now write some server side code in delete_images.php that will delete file
if (array_key_exists('imageName', $_POST)) {
$filename = $_POST['imageName'];
if (file_exists($filename)) {
unlink($filename);
// Write Mysql query that will delete the row from database
echo 'File '.$filename.' has been deleted';
} else {
echo 'Could not delete '.$filename.', file does not exist';
}
}
i wrote a piece of code to do multiple row deletions based on ticked check boxes.
Now i need to mordify it to do multiple row updates.
i have been failing to access the textbox values for each row.
here's my code, in general.
<?php
if (isset($_POST["SaveComments"]) && isset($_POST["SaveEachComment"]))
{
while(list($key, $val) = each($_POST["SaveEachComment"]))
{
if($val == 'commentbox')
{
// do the update here. $val contains the ID
}
}
}
?>
<form id="form1" name="form1" method="post" action="test.php">
<input type="checkbox" <?php echo 'name="SaveEachComment['.$row["comment"].']" ';?> value="commentbox" id="commentbox" />
<input type="text" name="rowcomment" size="55" value="<?php echo $comment;?>" />
<input type="submit" name="SaveComments" value="submit" />
</form>
I just added a for loop to print multiple form fields. Obviously your iteration would be as many as number of rows, so you can change that part of the code. But try this:
<?php
if (isset($_POST["SaveComments"]) && isset($_POST["SaveEachComment"]))
{
while(list($key, $val) = each($_POST["SaveEachComment"]))
{
if($val == 'commentbox')
{
echo $_POST['rowcomment'][$key] . "<br />\n";
// do the update here. $val contains the ID
}
}
}
?>
<form id="form1" name="form1" method="post" action="test.php">
<?php for ($i=0; $i<11; $i++) { ?>
<input type="checkbox" <?php echo 'name="SaveEachComment['.$i.']" ';?> value="commentbox" id="commentbox" />
<input type="text" name="rowcomment[<? echo $i?>]" size="55" value="<?php echo $comment;?>" />
<br />
<?php } ?>
<input type="submit" name="SaveComments" value="submit" />
</form>