Wednesday, 15 January 2014

PHP unlink error -



PHP unlink error -

i've got problem deleting uploaded images.

it inserts in database , uploads fine won't delete.

example: have 2 tables, modelimages , model. in model save path of 1 main image, in modelimage there can lot of them. tables linked via modelid.

this illustration how path looks like:img/1/1/someimagename.png.

(first 1 - makerid, sec 1 - modelid).

include('connect.php'); //connect database $modelid=$_get['modelid']; $makerid=$_get['makerid']; $path="img/".$makerid."/".$modelid."/"; if ($stmt = $mysqli->prepare("select images modelimages modelid='$modelid'")) { $stmt->execute(); $stmt->bind_result($images); while ($stmt->fetch()) { if($images!=$path){ unlink($images); } } $stmt->close(); } else { printf("prepared statement error: %s\n", $mysqli->error); } if ($stmt = $mysqli->prepare("select mainimage model makerid='$makerid' , modelid='$modelid'")) { $stmt->execute(); $stmt->bind_result($mainimage); while ($stmt->fetch()) { if($mainimage!=$path){ unlink($mainimage); } } $stmt->close(); } else { printf("prepared statement error: %s\n", $mysqli->error); }

when utilize first code , deletes images modelimages table , if utilize sec code, deletes 1 image model table.

but if utilize them unlink() error:

warning: unlink(img/1/1/image1.png) [function.unlink]: no such file or directory in /home/... warning: unlink(img/1/1/image2.png) [function.unlink]: no such file or directory in /home/... warning: unlink(img/1/1/image3.png) [function.unlink]: no such file or directory in /home/... warning: unlink(img/1/1/image4.png) [function.unlink]: no such file or directory in /home/...

conclusion: unlink won't work if utilize both of querys together.

don't utilize relative paths unless a) absolutely sure, current work directory (see getcwd()), or b) intended, path located relative difference work directories (like in workdirectory-aware cli-tools). use, or create absolute paths

"/path/to/img/".$makerid."/".$modelid."/"; __dir__ . "/../path/to/img/".$makerid."/".$modelid."/";

i prefer sec one, because more portable.

php unlink

No comments:

Post a Comment