I have a file which can possibly be moved into either 2 places depending on which button the user clicked on a seperate page:
At the moment I have stated if the the file moves into ‘ImageFiles’ folder then move the folder and insert a database row.
But I want to also include that if the file hasn’t been uploaded into ‘ImageFiles’ folder, then delete database row using this code below:
$imagecancelsql = "DELETE FROM Image
WHERE ImageFile = 'ImageFiles/".
mysql_real_escape_string($_FILES['fileImage']['name'])."'";
mysql_query($imagecancelsql);
Is this possible to do and if so how can it be written in the current if/else statement I have in the php script?
Below is php script:
<?php
session_start();
...//connect to DB
$result = 0;
if( is_file("ImageFiles/".$_FILES['fileImage']['name'])) {
$parts = explode(".",$_FILES['fileImage']['name']);
$ext = array_pop($parts);
$base = implode(".",$parts);
$n = 2;
while( is_file("ImageFiles/".$base."_".$n.".".$ext)) $n++;
$_FILES['fileImage']['name'] = $base."_".$n.".".$ext;
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
$imagesql = "INSERT INTO Image (ImageFile)
VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')";
mysql_query($imagesql);
}
else
{
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
$imagesql = "INSERT INTO Image (ImageFile)
VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')";
mysql_query($imagesql);
}
If I’ve understood correctly, the following code should work as intended. If the file has been found, the new file will replce it and inserts a new entry to your database.
If the file wasnt found, the database entry will be removed from you table.