Jump to content

how to delelte a file with if() unlink function


lukelee

Recommended Posts

Here is my codes, when the selected file is not blank.jpg, deleted the file, so I used if($query['imagedata']!=$myFile){

    unlink($path); but it doesnt work, can anyone tell me how to write the codes properly

<?php
require_once('db.php');	
$address = $_POST[address];
$imagedata = $_POST[imagedata];
$path="upload/$imagedata";
$myFile = "blank.jpg";
$image1 = $_FILES['image1']['name'];
$new_name1 = "upload/" . md5(uniqid(rand(), true)) . substr($image1, strrpos($image1, "."));

move_uploaded_file($_FILES['image1']['tmp_name'], $new_name1);

$filesize1=$_FILES['image1']['size'];

if($filesize1!= 0)
{
    $image1 = basename($new_name1);
    $query = mysql_query("UPDATE house SET imagedata='$image1' where address='$address' && thumb='1'");
$query2 = mysql_query("SELECT * FROM house where address='$address' && thumb='1'");
if($query2['imagedata']!=$myFile){
    unlink($path);
}
echo "image has been changed, you will be redirecting to previous page in 3 seconds";
}
else {
echo "ERROR..... you cant leave the first image empty";
}

?>

here is my codes: it displays the imagedata whatever the $row['imagedata'] is blank.jpg or not. and the unlink always work。 I think I must did something wrong in if condition

$query2 = mysql_query("SELECT imagedata FROM house WHERE address='$address' && thumb='1'");

while($row = mysql_fetch_array($query2)) {

if($row['imagedata']!=$myFile){

echo $row['imagedata'];

    unlink($path);

  }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.