Jump to content

deleting DB entry


dannybrazil

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.