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 Quote Link to comment 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. Quote Link to comment 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') Quote Link to comment 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. Quote Link to comment 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 ? Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
dannybrazil Posted September 14, 2009 Author Share Posted September 14, 2009 any help ? Quote Link to comment 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. Quote Link to comment 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 ? Quote Link to comment 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. Quote Link to comment 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 ? Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
dannybrazil Posted September 14, 2009 Author Share Posted September 14, 2009 help ? Quote Link to comment 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 "; } ?> Quote Link to comment 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.