PHP Remove File from Folder and Database - php

I am trying to delete image from folder and database using php unlink function and delete query. Here is my code,problem is i will not delete the image from folder. Please help.
$filetmp=$_FILES["images"]["tmp_name"];
$filename=$_FILES["images"]["name"];
$filetype=$_FILES["images"]["type"];
$filepath= "photo2/".$filename;
move_uploaded_file( $filetmp,$filepath);
$data=array($page_id,$filepath);
$add=add_data_event($data);
foreach ($errors as $value)
{
echo $value;
}
if(isset($_GET['id']))
{
$id=$_GET['id'];
delete_event1($id);
unlink("photo2/".$filename);
header('location:hotel2_galery_event.php?page_id=2');
}
HTML
<div class="form-group has-info">
<label>Event Related images</label>
<input type="file" name="images"><br>
</div>
<button type="submit" class="btn btn-primary">
<span>SUBMIT</span>
</button>

Have a look at this,
http://www.codingcage.com/2014/12/delete-uploaded-files-from-folder-in-php.html
Hope this helps.
Sohail.

Related

delete button in PHP website behaving like static when clicked

i have a simple delete button in PHP which uses simple SQL query to delete the data from database, I have used the following code:
$myid = $this->uri->segment('3');
if (isset($_POST['submit'])){
$ret = mysqli_query($con,"delete * from smart_category where name='$myid'");
header("Location: https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory");
}
<div style="margin-left:38%" class="clearfix">
<a href="https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory">
<button type="button" class="cancelbtn bemine">Cancel</button></a>
<form action="" method="post" ><button type="submit" name="submit" class="deletebtn bemine">Delete</button></form>
</div>
the problem here is if I click the delete button, its not responding, nothing is happening. the button is like static. can anyone please tell me what could be wrong here
If any Javasscript is not involved, than just put form action.
Also send myid in the request or in URI.
please see below:
<div style="margin-left:38%" class="clearfix">
<button type="button" class="cancelbtn bemine">Cancel</button>
<form action="abc.php" method="post" ><button type="submit" name="submit" class="deletebtn bemine">Delete</button></form>
</div>
You should add an action url/path to where your PHP file is located and add a Method to your form. Also, I noticed you deleting an entry where "name" is equal to $myid please check if that is correct.
<div style="margin-left:38%" class="clearfix">
<button type="button" class="cancelbtn bemine">Cancel</button>
<form action="url-to-server.php" method="post" >
<button type="submit" name="submit" class="deletebtn bemine">Delete</button>
</form>
</div>
$myid = $this->uri->segment('3');
if (isset($_POST['submit'])){
$ret = mysqli_query($con,"delete * from smart_category where name='$myid'");
header("Location: https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory");
}

Save picture from one database to another

I'm creating a save button for user to save into their database, but I having a difficulty on getting the name of the image i select.
Save button
I know using ` $image=$_GET["image"]; will get the name if the image but I dont know how to get the name outside the php because I'm using popup module.
<div id="modal01" class="w3-modal " style="padding-top:0"
onclick="this.style.display='none'">
<span class="w3-button w3-black w3-xlarge w3-display-topright">×</span>
<div class="w3-modal-content w3-animate-zoom w3-center w3-transparent w3-padding-64">
<img id="img01" class="w3-image">
<p id="caption"></p>
<form action="save.php" method="GET">
<input type="submit" name="save" value="Save"/>
</form>
</div>
Here is my code on retrieving picture from database:
<?php
while($row = mysqli_fetch_array($result)) {
echo "<div class='grid-item'><img src='images/{$row['image']}' onclick=onClick(this) style='width:98%' /></div>
";}
mysqli_close($db);
?>

Images not uploading to server path PHP

I have the directory photo_gallery (with 777 permissions) created in the same level as the Noticias.php file
It gives me no error, but the file is not showing in the photo_gallery folder.
This is my PHP code:
$error=array();
$extension=array("jpeg","jpg","png","gif");
foreach($_FILES["imagenes"]["tmp_name"] as $key=>$tmp_name)
{
$file_name=$_FILES["imagenes"]["name"][$key];
$file_tmp=$_FILES["imagenes"]["tmp_name"][$key];
$ext=pathinfo($file_name,PATHINFO_EXTENSION);
if(in_array($ext,$extension))
{
if(!file_exists("photo_gallery/".$file_name))
{
move_uploaded_file($file_tmp=$_FILES["imagenes"]["tmp_name"][$key],"photo_gallery/".$file_name);
}
else
{
$filename=basename($file_name,$ext);
$newFileName=$filename.time().".".$ext;
move_uploaded_file($file_tmp=$_FILES["imagenes"]["tmp_name"][$key],"photo_gallery/".$newFileName);
}
}
else
{
array_push($error,"$file_name, ");
}
}
And HTML:
<form action="Noticias.php" method="post" id="myform" enctype="multipart/form-data">
div class="form-group">
<p>Imágenes</p>
<input type="file" name="imagenes[]" class="form-control" id="imagenes" placeholder="Imágenes" multiple="multiple"/>
</div>
<button type="submit" class="btn btn-primary">Guardar</button>
</form>
move_uploaded_file() needs as second parameter the folder and the filename of the new location.
You should to do something like this:
move_uploaded_file($_FILES["imagenes"]["tmp_name"][$key], "photo_gallery/your_filename.png");
I hope that help you.
Regards.
give the file path as
./photo_gallery/
try liker this. This may help you.

Notice: Undefined index: ($_FILES)

There are multiple questions[duplicates] about this same error but I can't find a solution in any of them. I`m trying to save an image in my local directory but 'Undefined index' keeps showing up. I´m a noob in php and stack overflow. I would really appreciate any of your help.
HTML:
<form method="post" action="process.php" enctype="multipart/form-data">
<fieldset>
<div class="col-xs-12 col-sm-push-5 image-div form-control">
<div class="input-margin">
<input type="file" name="imagenIngreso" class="filestyle" onchange="readURL(this);" data-buttonText="Subir Imagen" data-buttonName="btn-primary" data-size="nr" data-buttonBefore="false">
</div>
<div>
<img class="image-preview" id="blah" src=""/>
</div>
</div>
<div class="col-xs-12 col-sm-12 submit-button">
<button type="submit" class="btn btn-primary btn-block" name="submit" value="ingreso">Ingresar</button>
</div>
</fieldset>
</form>
PHP:
// Previous Validations
else if($_POST['submit'] == 'ingreso') {
$fileName = $_FILES['imagenIngreso']['name']; // Line throwing error
$uploadDirectory = "images/uploadedImages/Ingresos/";
if($uploadedFileName!='') {
$targetPath=time().$uploadedFileName;
if(move_uploaded_file($_FILES['imagenIngreso']['tmp_name'], $uploadDirectory.$targetPath)) {
$queryIngresos = "INSERT INTO ingresos (fechaIngreso, responsable, proyecto, items, imagePath) VALUES (STR_TO_DATE('$fechaEgreso','%d/%m/%Y'), '$responsable', '$proyectoIngreso', '$itemsArray', '$targetPath')";
if(!mysqli_query($con, $queryEgresos)) {
echo "Error while uploading file";
die('Error :'.mysqli_error($con));
} else {
echo 'Upload done';
exit();
}
}
}
You can also find what error are you getting using the Error Codes available.
Put this and make a try.
<?php
if($_FILES['userfile']['error']) {
// handle the error
} else {
// process
}
?>

jQuery ajax error when data fetching

I am using bootstrap 3.2.0 and jQuery. I can't get fetch data via jQuery and PhP
function postolayi(){
$.ajax({
type:'POST',
url:'giriskontrol.php',
data:$('#girisForm').serialize(),
success:function(cevap){
$("#uyari").html(cevap)
}
})
}
index.php
<form class="form-signin" method="POST" id="girisForm">
<input id="isim" name="isim" type="text" class="form-control" placeholder="isim" required autofocus>
<input name="sifre" type="password" class="form-control" placeholder="şifre" id="sifre" required>
<button class="btn btn-lg btn-primary btn-block" type="submit" onclick="postolayi();">
Giriş Yap</button>
</form>
<div class="alert alert-warning" id="uyari"></div>
giriskontrol.php
<?php
$POST["isim"] = $isim;
echo $isim;
?>
I can't fetch data. Please somebody help.
I changed as
<?php
$isim = $POST["isim"];
echo $isim;
?>
But still not working.
You are overwriting the post variable here:
$_POST["isim"] = $isim;
What you probably wanted was:
$isim = $_POST["isim"];
echo $isim;
In your php file
Switch:
$POST["isim"] = $isim;
with
$isim = $POST["isim"];
You need to switch your assignment on your php page to
<?php
$isim = $_POST["isim"];
echo $isim;
?>

Categories