xavier.rayne Posted September 13, 2007 Share Posted September 13, 2007 Hi,there. I am doing on a php photo album. Here is my table structure for images: CREATE TABLE `images` ( `Id` int(11) NOT NULL auto_increment, `name` text, PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; I save the images as a path(e.g. http://localhost/album/images/pic.jpg) in field 'name' of table 'images'. and now i would like to know how to delete the images from both the DB and the directory that i saved the images. Any ideas? Thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/69162-delete-imagesfilename/ Share on other sites More sharing options...
shab Posted September 13, 2007 Share Posted September 13, 2007 To delete files from the server you need to use unlink($path) To delete from the db you need to use "DELETE FROM table_name WHERE column_name = some_value" have a look at: http://www.w3schools.com/sql/sql_delete.asp Quote Link to comment https://forums.phpfreaks.com/topic/69162-delete-imagesfilename/#findComment-347605 Share on other sites More sharing options...
xavier.rayne Posted September 14, 2007 Author Share Posted September 14, 2007 Hi, shab. I can delete the files from db but then i'm facing with problems when i try to delete files from server. It shows the error message like below: Warning: unlink(localhost/album/mall/images/No_2897.jpg) [function.unlink]: No such file or directory I put all my images in ../album/mall/images/ directory and I've checked that the image is in the directory. Below is my code for deleting images: <?php if (isset($_POST['delete'])) { $query = "SELECT * FROM images"; $result = mysql_query($query)or die(mysql_error()); $row = mysql_fetch_array($result); $id = $row['Id']; $path = "mall/smallthumbnail.php?i=http://localhost/album/mall/images/"; $name = $row['name']; $path = $path.$name; // Delete the record from database. $query2 = "DELETE FROM images WHERE Id='$id'"; $result2 = mysql_query($query2)or die(mysql_error()); //delete record from server unlink($path); } ?> <body> <p align="right"><a href="gallery.php">Back To Gallery</a></p> <form name="delete" action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> <input type="hidden" name="id" value="<?php echo $id ?>"> <center> <img src="<?php echo $path ?>" width="400" height="400"> <br> <input type="submit" name="delete" value="Delete"> </center> </form> Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/69162-delete-imagesfilename/#findComment-348169 Share on other sites More sharing options...
jitesh Posted September 14, 2007 Share Posted September 14, 2007 give root path to unlink as argument. unlink($_SERVER['DOCUMENT_ROOT']."album/images/pic.jpg"); Quote Link to comment https://forums.phpfreaks.com/topic/69162-delete-imagesfilename/#findComment-348174 Share on other sites More sharing options...
redarrow Posted September 14, 2007 Share Posted September 14, 2007 try this aswell @chmod( $path, 0777 ); unlink( $path); Quote Link to comment https://forums.phpfreaks.com/topic/69162-delete-imagesfilename/#findComment-348182 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.