html - finding if a file exists in a directory in php -



html - finding if a file exists in a directory in php -

i have exhausted brain , cant seem way construction code, solution have works need simplify it. ok have $id = $_get["id"]; , $id = 352 ... now,

scenario:

the code below user selects image upload:

$query = mysql_query("select * properties id = '$_get[id]'"); <input type="file" name="image1" id="image1" /> <?php if(isset($query) && file_exists("../uploads/properties/".mysql_result($query, 0, "id").".jpg")) { ?> <img height="100" src="../uploads/properties/<?php echo mysql_result($query, 0, "id"); ?>.jpg" /> <a target="_blank" href="delete_script/deletepic.php?id=<?php echo mysql_result($query, 0, "id"); ?>">delete image</a> <?php } ?> <input type="file" name="image2" id="image2" /> <?php if(isset($query) && file_exists("../uploads/properties/".mysql_result($query, 0, "id")."_2.jpg")) { ?> <img height="100" src="../uploads/properties/<?php echo mysql_result($query, 0, "id"); ?>_2.jpg" /> <a target="_blank" href="delete_script/deletepic2.php?id=<?php echo mysql_result($query, 0, "id"); ?>">delete image</a> <?php } ?>

if take @ src on img tag difference between 2 1 image uploaded 352.jpg , other saved 352_2.jpg. have 10 images user can upload images saved as:

1: 352.jpg

2: 352_2.jpg

3: 352_3.jpg

4: 352_4.jpg

5: 352_5.jpg

6: 352_6.jpg

7: 352_7.jpg

8: 352_8.jpg

9: 352_9.jpg

10: 352_10.jpg

now tricky part:

when link clicked page deletepic.php run next code:

<?php $id = $_get["id"]; $file_path = "../uploads/properties/".$id."_".$i.".jpg"; ($i = 2; $i < 11; $i++) { $file_path = "../uploads/properties/".$id."_".$i.".jpg"; homecoming $file_path; } if (file_exists($file_path)) { echo "file remove requested"; }else{ echo "no file requested removed"; } ?>

i know loop not going work need script @ $id."_".$i , if file exists on link clicked file must removed.

at moment solution working me, if create 10 delete.php scripts , run block of code in each one, part alter in script work $img setting each time $img = "../../uploads/properties/".$id.".jpg"; next delete.php value of $img $img = "../../uploads/properties/".$id."_2.jpg"; etc.

so within delete.php @ moment code looks this:

<?php $id = $_get["id"]; $current_page = 'properties_page.php'; $url = $_server['request_uri']; $clean_this = "delete_script/deletepic.php"; $url = str_replace($clean_this, "properties_page.php", $url); $img = "../../uploads/properties/".$id.".jpg"; unlink($img); header("location:$url"); ?>

your first code sample has issues, illustration sql injection. aware mysql_* functions deprecated; should @ using pdo instead. that's not question here so... delete.php script should this:

<?php $id = $_get['id']; $imagefolder = realpath('../../uploads/properties'); $imagepath = $imagefolder . '/' . $id . '.jpg'; if (file_exists($imagepath) && dirname(realpath($imagepath)) == $imagefolder) { unlink($imagepath); echo "file remove requested"; } else { echo "no file requested removed"; }

php html image

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -