in this php ,i want get a "ID" and find the Img1 for this ID from database.(The url of the Img1 is stored in the database).
and then delete this file.but the img1 dont deleted
function delete(){
$connection = connectToDatabase();
$ID = $_REQUEST['ID'];
if($ID!==""){
$result1 = mysqli_query($connection,"select Img1 from banners where ID='$ID' ");
$row = mysqli_fetch_array($result1);
$namefile1 = $row[0];//(in $namefile1 the url address of img1 is saved like this:http://bestabsd.com/bestfile/pics/jan18-11-28-20-46-36.jpg)
$files = glob($namefile1);
foreach($files as $file){
if(is_file($file))
unlink($file);}
mysqli_close($connection);
}
EDIT: i edit my code to new for make a path, but the jpg dont deleted yet....
function delete(){
$connection = connectToDatabase();
$ID = $_REQUEST['ID'];
if($ID!==""){
$result1 = mysqli_query($connection,"select Img1 from banners where ID='$ID' ");
$row = mysqli_fetch_array($result1);
$namefile1 = $row[0];
$namefile11=str_replace("http://bestabsd.com/bestfile/pics/", "", $namefile1);
$base_directory = '/home/bestabsd/public_html/bestfile/pics/';
unlink($base_directory.$namefile11);
mysqli_close($connection);
}else {
print "null";
}
Related
I want to have a separate query that updates only the imagename and imagetext when the imagefile is left blank and update all the imagename,imagetext and the image itself when the user selects a new image here is my query:
if (isset($_POST['update'])) {
$update_id = $_POST["id"];
$update_imagename = $_POST['imagename'];
// Get image name
$update_imagefile = $_FILES['imagefile']['name'];
// Get text
$update_imagetext = mysqli_real_escape_string($db, $_POST['imagetext']);
// image file directory
$target = "images/portfolio/" . basename($update_imagefile);
$update_query = "UPDATE `images` SET `imagename`='$update_imagename',`imagetext`='$update_imagetext',`imagefile`= '$update_imagefile' WHERE `imageid`='$update_id'"; //delete query
$run = mysqli_query($db, $update_query);
if ($run) {
//javascript function to open in the same window
echo "<script>window.open('artworksupdate.php?=image has been updated','_self')</script>";
}
to determine if you have uploadFile, just do !empty($_POST['imagefile']), following you have sample.
if (!empty($_POST)) {
$update_id = $_POST["id"];
// Get image name
$update_imagename = $_POST['imagename'];
// Get image description
$update_imagetext = $_POST['imagetext'];
// Determine if we have input file set
$haveUploadedFile = !empty($_POST['imagefile']);
if( $haveUploadedFile ) {
// [...] do your upload stuff here.
$update_imagefile = 'myAwesomeImage.png';
$update_query = "UPDATE `images` SET `imagename`='$update_imagename',`imagetext`='$update_imagetext',`imagefile`= '$update_imagefile' WHERE `imageid`='$update_id'"; //delete query
}
else {
$update_query = "UPDATE `images` SET `imagename`='$update_imagename',`imagetext`='$update_imagetext' WHERE `imageid`='$update_id'"; //delete query
}
$run = mysqli_query($db, $update_query);
//[...]
}
I'm kind of a noob so I don't know if I'm missing something small. The images are uploaded to the database just fine, but using this just gets me a blank square. I don't know if I'm missing something obvious?
$name = $_SESSION['username'];
$sql="SELECT pic FROM userinfo WHERE username = '$name'";
$result = mysqli_query($link,$sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$pic = base64_encode($row['pic']);
echo "<img height='300' width='300' style='padding-left:10px;' src='data:image/jpeg;base64,".$pic."'>";
}
}
The upload code, I'm 95% sure that the issue is somewhere in the upload process. I commented out what I was previously using to send the query.
$name = $_SESSION['username'];
$bio = $_POST['bio'];
$sql="UPDATE userinfo SET pic='$pic' WHERE username='$name'";
$stmt = $link->prepare($sql);
$stmt->bind_param("b", $pic);
$pic = base64_encode($_FILES['fileToUpload']['tmp_name']);
//$query = mysqli_query($link,$sql);
$stmt->execute();
I have a variable $target inside an IF - Statement in my login.php. I created the folders and sub-folders based on this variable. Now i want to move the uploaded file to this location. How can I do that?
here is the code
$upload = "E:/demons";
if(isset($_POST['userid'], $_POST['pid']))
{
$userid = trim($_POST["userid"]);
$pid = trim($_POST["pid"]);
$sql = "SELECT * FROM template WHERE uname = '$userid' and pword = '$pid'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result);
echo "公司".'<br/>';
echo $row['client'].'<br/>'.'<br/>';
echo "第".'<br/>';
echo '<a href="upload.html"/>'.$row['day1'].'</a>'.'<br/>';
$target = $upload.'/'.$row['week'].'/'.$row['day1'].'/'.$row['client'].'/'.$row['brand'].'/'.$row['sc'].'/';
$imagename = $row['week'].'.'.$row['day1'].'.'.$row['client'].'.'.$row['brand'].'.'.$row['sc'].'.'.'jpg';
if(!file_exists($target))
{
mkdir($target,null,true);
}
}
else if(isset($_FILES['image']))
{
$image = basename($_FILES["image"]["name"]);
echo $image;
//$target4 = $upload.'/'.$row['week'].'/'.$row['day1'].'/'.$row['client'].'/'.$row['brand'].'/'.$row['sc'].'/';
move_uploaded_file($_FILES['image']['tmp_name'], $target);
}
else
{
echo "asdfg";
}
Userid and Pid comes from login.html and Image value comes from upload.html
Nest the else-if as an if in the first if. Otherwise it won't be executed.
$upload = "E:/demons";
if(isset($_POST['userid'], $_POST['pid']))
{
$userid = trim($_POST["userid"]);
$pid = trim($_POST["pid"]);
$sql = "SELECT * FROM template WHERE uname = '$userid' and pword = '$pid'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result);
echo "公司".'<br/>';
echo $row['client'].'<br/>'.'<br/>';
echo "第".'<br/>';
echo '<a href="upload.html"/>'.$row['day1'].'</a>'.'<br/>';
$target = $upload.'/'.$row['week'].'/'.$row['day1'].'/'.$row['client'].'/'.$row['brand'].'/'.$row['sc'].'/';
$imagename = $row['week'].'.'.$row['day1'].'.'.$row['client'].'.'.$row['brand'].'.'.$row['sc'].'.'.'jpg';
if(!file_exists($target))
{
mkdir($target,null,true);
}
if(isset($_FILES['image']))
{
$image = basename($_FILES["image"]["name"]);
echo $image;
move_uploaded_file($_FILES['image']['tmp_name'], $target);
}
}
$upload = "Your desired location";
//comes from the login.html page
if(isset($_POST['userid'],$_POST['pid']))
{
$userid = trim($_POST["userid"]);
$pid = trim($_POST["pid"]);
$sql = "SELECT * FROM template WHERE uname = '$userid' and pword = '$pid'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result);
echo "Whatever coloumn you wish to echo from database".'<br/>';
echo $row['col1'].'<br/>'.'<br/>';
echo "Second Coloumn".'<br/>';
echo '<a href="upload.html"/>'.$row['col2'].'</a>'.'<br/>';
//create the folders and subfolders based on the data from the database
$target = $upload.'/'.$row['col1'].'/'.$row['col2'].'/'.$row['col3'].'/'.$row['col4'].'/'.$row['col5'].'/';
//for renaming the image.
$imagename = $row['col1'].'.'.$row['col2'].'.'.$row['col3'].'.'.$row['col4'].'.'.$row['col5'].'.'.'jpg';
//create the folders and subfolders
if(!file_exists($target))
{
mkdir($target,null,0777);
}
//start session and store the value of $target and $imagename in a variable
session_start();
$_SESSION['str'] = $target;
$_SESSION['img'] = $imagename;
//This comes from other HTML page but to the same PHP.
if(isset($_FILES['image']))
// image upload from upload.html
// Want the value of target here.
{
session_start();
$_SESSION['str'];
$_SESSION['img'];
$image = basename($_FILES["image"]["name"]);
//Move the uploaded file to the desired location.
move_uploaded_file($_FILES['image']['tmp_name'], $_SESSION['str'].$_SESSION['img']);
echo "Upload Successful";
Hope this would be helpful for all.
I am using echo $fss its display data
"11-V5703211-V5703211-V5703211-V5703211-V5703211-V5703211-V5703211-V5703211-V5703211-V5703211-V5703211-V5703211-V5703211-V5703211-V5703211-V5703211-V5703211-V5703211-V57032"
but i am using delete query using this variable but its not working
<?php
//connect to the database
$connect = mysql_connect("localhost","root","");
mysql_select_db("cityshoes",$connect); //select the table
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES['csv']['tmp_name'];
$handle = fopen($file,"r");
do {
if ($data[0]) {
$fss = addslashes($data[1]);
$result = mysql_query("DELETE from contacts where articleno = . $fss . ");
echo $fss;
}
} while ($data = fgetcsv($handle,1000,",","'"));
}
?>
You are appending variable in the query in wrong way. Try with:
$result = mysql_query("DELETE from contacts where articleno = $fss");
or
$result = mysql_query("DELETE from contacts where articleno ='".$fss ."'");
Turn this line
$result = mysql_query("DELETE from contacts where articleno = . $fss . ");
To this
$result = mysql_query("DELETE from contacts where articleno = $fss ");
You have to remove the periods.
I have this code which will delete images with the category name from my database and but it only unlink one image from my images folder but I need it to unlink multiple images at once can anyone help here is an example of my code.
if(isset($_GET['delete'])) {
$delete_id = $_GET['delete'];
$sql = "SELECT image FROM images WHERE category = '$delete_id'";
$query = mysqli_query($connection,$sql) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)){
$image = $row['image'];
$location_full_image = "../images/$image";
$location_thumb_image = "../images/thumbnails/$image";
#unlink($location_full_image);
#unlink($location_thumb_image);
$sql = "DELETE FROM images WHERE category = '$delete_id'";
$query = mysqli_query($connection,$sql) or die (mysqli_error());
}
}
try below code and make sure you are fetching right column from database which contains image name.
if(isset($_GET['delete'])) {
$delete_id = $_GET['delete'];
$sql = "SELECT image FROM images WHERE category = '$delete_id'";
$query = mysqli_query($connection,$sql) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)){
$image = $row['image'];
#unlink('../images/'.$image);
#unlink('../images/thumbnails/'.$image);
$sql = "DELETE FROM images WHERE category = '$delete_id'";
$query = mysqli_query($connection,$sql) or die (mysqli_error());
}
}
Try this,
while ($row = mysqli_fetch_array($query)){
$image = $row['img'];
#unlink("images/".$image);
}