lukelee Posted December 3, 2008 Share Posted December 3, 2008 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/135310-how-to-delelte-a-file-with-if-unlink-function/ Share on other sites More sharing options...
balistic Posted December 3, 2008 Share Posted December 3, 2008 debug it by checking what $query['imagedata'] holds at that point in time, echo it out if($query2['imagedata']!=$myFile){ unlink($path); } else{ echo $query2['imagedata']; } Link to comment https://forums.phpfreaks.com/topic/135310-how-to-delelte-a-file-with-if-unlink-function/#findComment-704801 Share on other sites More sharing options...
lukelee Posted December 3, 2008 Author Share Posted December 3, 2008 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); } Link to comment https://forums.phpfreaks.com/topic/135310-how-to-delelte-a-file-with-if-unlink-function/#findComment-704819 Share on other sites More sharing options...
balistic Posted December 3, 2008 Share Posted December 3, 2008 still, whats the output on both $myFile and $query2['imagedata'] are they giving the values that they are expected to give, if not which is wrong and what is it opposed to what should it be? Link to comment https://forums.phpfreaks.com/topic/135310-how-to-delelte-a-file-with-if-unlink-function/#findComment-704821 Share on other sites More sharing options...
lukelee Posted December 3, 2008 Author Share Posted December 3, 2008 thanksfor help, I solved it Link to comment https://forums.phpfreaks.com/topic/135310-how-to-delelte-a-file-with-if-unlink-function/#findComment-704895 Share on other sites More sharing options...
balistic Posted December 3, 2008 Share Posted December 3, 2008 you're welcome Link to comment https://forums.phpfreaks.com/topic/135310-how-to-delelte-a-file-with-if-unlink-function/#findComment-704896 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.