dannybrazil Posted September 14, 2009 Share Posted September 14, 2009 Hello I wanted to as for help regarding uploaded files. i have a form that allows users to upload photos. after the photo was uploaded ,in the DB specific col' i have the "path_link" (where the photo was uploaded to) for example : uploaded(column) http://www.mysite.com/uploads/12345_mypic.jpg now the question is: when / if the user wants to delete the photo can i have a script that will delete the photo from the directory as well (with the "path_link" specified) ? hope you understood the question thatnks Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/ Share on other sites More sharing options...
Bricktop Posted September 14, 2009 Share Posted September 14, 2009 Hi dannybrazil, Simply use something like the below: $path_link = $_GET['filename']; //or use $_POST['filename'] depending on how you want to retrieve the filename $sql = mysql_query("DELETE FROM gallery WHERE path_link='".$path_link."' LIMIT 1"); unlink( $row['path_link'] ); You could put the above in a function and either use a form, where a user presses a button to delete the image, or call it via a URL like http://www.yoursite.com/script.php?function=delete&filename=12345_mypic.jpg Obviously the above is very basic code and you will need to buil din the appropriate checking and validation once you have decided how you wish to call the function. Hope this helps. Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918338 Share on other sites More sharing options...
dannybrazil Posted September 14, 2009 Author Share Posted September 14, 2009 Hello wow....quick just one thing : unlink( $row['path_link'] ); --> this is the command that will delete it FISCALLY from the folder ? (not just the database col') Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918347 Share on other sites More sharing options...
Bricktop Posted September 14, 2009 Share Posted September 14, 2009 Yes, that will remove the actual file from the server. Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918383 Share on other sites More sharing options...
dannybrazil Posted September 14, 2009 Author Share Posted September 14, 2009 Hello i keep getting this message : Warning: unlink() [function.unlink]: http does not allow unlinking in /home/rgajsgjb/public_html/classimoveisrn/upload_foto.php on line 16 any reason why ? Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918399 Share on other sites More sharing options...
Bricktop Posted September 14, 2009 Share Posted September 14, 2009 That's because your path_link data contains the full URL - sorry I didn't realsie. You can only unlink from a relative path - something like /gallery/pictures/picture.jpg Do you have a table which contains a path like this which you could use? Otherwise you could perform a str_replace on the path_link variable to remove the full URL or perhaps you could use file_info to get the path. Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918404 Share on other sites More sharing options...
dannybrazil Posted September 14, 2009 Author Share Posted September 14, 2009 so many things to learn let me try to str_replace brb Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918407 Share on other sites More sharing options...
dannybrazil Posted September 14, 2009 Author Share Posted September 14, 2009 still getting it Warning: unlink(ABVFP/uploads/669acd3c7f_knotthouse.jpg ) [function.unlink]: No such file or directory in /home/rgajsgjb/public_html/classimoveisrn/upload_foto.php on line 17 Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918417 Share on other sites More sharing options...
dannybrazil Posted September 14, 2009 Author Share Posted September 14, 2009 any help ? Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918445 Share on other sites More sharing options...
Bricktop Posted September 14, 2009 Share Posted September 14, 2009 Hi dannybrazil, The above error is basically saying that the directory/filename passed to the unlink function does not exist - check the value you're passing to the unlink function is correct and try again. Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918459 Share on other sites More sharing options...
dannybrazil Posted September 14, 2009 Author Share Posted September 14, 2009 Hello i checked it and its ok...the file link is : /ABVFP/uploads/name.jpg the unlink() suppose to be in the same directory as the file itself ? Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918461 Share on other sites More sharing options...
Bricktop Posted September 14, 2009 Share Posted September 14, 2009 Hi dannybrazil, Try it without the first forward slash, i.e.: ABVFP/uploads/name.jpg Unlink can be run from anywhere. Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918477 Share on other sites More sharing options...
dannybrazil Posted September 14, 2009 Author Share Posted September 14, 2009 Hello i discovered something weird: Like that its not working : $acao = $_POST['acao']; if ($acao=='Excluir') { $file = $_POST['file']; $file_clean=substr ($file, 30); --> here i get exactly this ABVFP/uploads/43e509f5c9_knotthouse.jpg if(unlink("$file_clean")) echo "Deleted file "; } Like that its working : $acao = $_POST['acao']; if ($acao=='Excluir') { $file = $_POST['file']; //$file_clean=substr ($file, 30); $file_clean='ABVFP/uploads/43e509f5c9_knotthouse.jpg'; if(unlink("$file_clean")) echo "Deleted file "; } when i write down the specific file name as variable ($path="path/to/file" it is working but when trying to pass just the variable it doesnt (unlink($path) any help ? Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918505 Share on other sites More sharing options...
dannybrazil Posted September 14, 2009 Author Share Posted September 14, 2009 May be im doing the file name cleaning with the substr() wrongly ? how can i get just the file name else? http://www.mysite.com/ABVFP/uploads/file_name.jpg thanks Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918511 Share on other sites More sharing options...
dannybrazil Posted September 14, 2009 Author Share Posted September 14, 2009 help ? Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918545 Share on other sites More sharing options...
dannybrazil Posted September 15, 2009 Author Share Posted September 15, 2009 when i do this it says that the file was deleted but it ISNT <? $acao = $_POST['acao']; if ($acao=='Excluir') { $file = $_POST['file']; //$len=strlen($file); $file_clean = str_replace('http://'.$_SERVER['HTTP_HOST'].'/','',$file); $myFile = $file_clean; $fh = fopen($myFile, 'w+') or die("can't open file"); fclose($fh); //$len_correct=$len - 1; // echo $len_correct; //$file_clean=substr ($file, 30 , $len_correct); echo $file_clean; //$file_clean='ABVFP/uploads/43e509f5c9_knotthouse.jpg'; if(unlink($file_clean)) echo "Deleted file "; } ?> Link to comment https://forums.phpfreaks.com/topic/174206-deleting-db-entry/#findComment-918633 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.