unlink causes prior file_exists to fail
Posted: Wed Aug 22, 2007 3:18 am
After uploading an image, I have the following code:
It seems pretty straightforward to me, but I'm having two separate problems:
1) If the image size is wrong, the database is updated and image deleted, but $errorMsg contains "File does not exist" instead of "Wrong size". If I remove the unlink($imgPath) portion, everything works fine (except, of course, the unlink doesn't happen), so I can only assume that unlink() is somehow affecting the initial file_exists(). Is it possible for one function to be affected by a function later in the code? How can I both get the correct error message and delete the image?
2) If $action="add" and $errorMsg is empty, I get a "duplicate entry" error. Everything works fine when $action="update". It seems like the db insert is being attempted twice, but why would that happen?
I've been tearing my hair out for the last two days, so any help would be greatly appreciated!!
Code: Select all
if (file_exists($imgPath))
{
$imgSize = getImageSize($imgPath);
$imgWidth = $imgSize[0];
$imgHeight = $imgSize[1];
if (($imgWidth != 150) || ($imgHeight != 150))
{
$errorMsg[] = "Wrong size";
}
}
else
{
$errorMsg[] = "File does not exist";
}
if (!empty($errorMsg))
{
foreach ($errorMsg as $error) print("<p>$error</p>");
if ($action == "update")
{
$sql = "delete from...";
$result = mysql_query($sql, $conn) or die(mysql_error());
}
if (file_exists($imgPath))
{
unlink($imgPath);
}
}
else
{
if ($action == "add")
{
$sql = "insert into...";
}
elseif ($action == "update")
{
$sql = "update...";
}
$result = mysql_query($sql, $conn) or die(mysql_error());
print("<p><img src='$imgPath' /></p>\n");
}1) If the image size is wrong, the database is updated and image deleted, but $errorMsg contains "File does not exist" instead of "Wrong size". If I remove the unlink($imgPath) portion, everything works fine (except, of course, the unlink doesn't happen), so I can only assume that unlink() is somehow affecting the initial file_exists(). Is it possible for one function to be affected by a function later in the code? How can I both get the correct error message and delete the image?
2) If $action="add" and $errorMsg is empty, I get a "duplicate entry" error. Everything works fine when $action="update". It seems like the db insert is being attempted twice, but why would that happen?
I've been tearing my hair out for the last two days, so any help would be greatly appreciated!!