Log in

View Full Version : PHP - Deleting variable and file together



auriaks
01-10-2010, 03:46 AM
Hi,
I have file uploading system which saves file and some info about it:


$target_path = "uploads/"; // directory CHMODed to 777.

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
$filename = basename($_FILES['uploadedfile']['name']);

so I have file saved in uploads/ and also I have $filename
how I can delete file and all the row in mysql by pushing one button???

If you need more info -ask :) thanks ;)

Nile
01-10-2010, 03:51 AM
To delete a file, use unlink, to delete a variable use unset.

Maybe?:


unlink($target_path);

djr33
01-10-2010, 05:06 AM
1. File: as above, use unlink() in PHP-- use the filename AND path, so unlink('uploads/example.file');

2. To delete from a mysql database, use the DELETE command:
DELETE FROM `tablename` WHERE `something` = `somevalue`;

(For example, `id`=`8`, or `filename`=`example.file`)

auriaks
01-10-2010, 09:44 AM
I got unlinking problem

Warning: unlink() [function.unlink]: http does not allow unlinking in /home/freecorplietuv/domains/laisvasprotas.xz.lt/public_html/admin/patvirtinti.php on line 19

in row 19 is written: $del = unlink($file_path);

Whats wrong?

Nile
01-10-2010, 02:25 PM
You can't unlink 'http://'

auriaks
01-10-2010, 09:23 PM
then what I have to unlink??
Can you post some egzamples?

Nile
01-10-2010, 09:35 PM
unlink("file.php");

$filepath has a prefix of http://

bluewalrus
01-10-2010, 09:35 PM
<form method="post">
<input name="set" type="hidden" value="1" />
<input type="submit" value="Delete" />
</form>
if (isset($_POST['set'])) {
unlink($target_path . $filename);
SQL commands here to remove
}

djr33
01-11-2010, 07:01 AM
1. bluewalrus, that code is fine but very dangerous: having direct user input to deleting files is very messy. Some sort of error checking would be very wise. Make sure they aren't deleting a higher level folder, and make sure the folder exists before unlinking it, at the very least. Unless this is a very limited application, it's best to only allow that sort of thing for logged in admins, etc. Otherwise, having some sort of non-transparent system is best, like a form that corresponds to a database where the real path is stored-- then it is hidden from the users so they can't write in any folder to delete-- only the ones stored in the database.

2. "http:"-- You do not use the URL (like "http://example.com/file.php"). Instead, use the RELATIVE path: (file.php) or (directory/file.php), etc.

Nile
01-11-2010, 10:43 PM
djr33, you cannot delete higher level files. But yes - NOT secure.

djr33
01-12-2010, 03:56 AM
You can't delete the containing folder but why not a parallel file like:
dir/subdir2/example.php from dir/subdir1/del.php-- path: ../subdir2/example.php
similar to dir/ex2.php using ../ex2.php

I have never had the need to try it though. You could easily do it with an include, regardless.

Nile
01-12-2010, 04:04 AM
I don't understand... But say this is your root folder layout:
http://localhostr.com/files/18826c/capture.png

Within includes/unlinkthis.php, you could not do:


unlink("../examples/file.php");

auriaks
01-15-2010, 03:05 PM
can someone show me how to create form where you can choose in RADIO style.
Also it has two buttons and one value to send - ID.
One button deletes existing file, other renames it to good.png

but everythin in one form. Also some php before header I think :D thanks

Nile
01-17-2010, 05:15 PM
<?php
function get_files(){
$files = glob("*");
$radio = array();
foreach($files as $key => $value){
if(is_dir($files[$key])){
unset($files[$key]);
continue; //thanks to techietim
}
$radio[] = '<input type="radio" name="delete" id="'.$key.'" value="'.$value.'" /><label for="'.$key.'">'.$value.'</label><br />';
}
return implode($radio);
}
if(isset($_POST['submit'])){
unlink($_POST['delete']);
}
if(isset($_POST['rename'])){
rename($_POST['delete'], "good.png");
}
?>
<form action="" method="post">
<?php
echo get_files();
?>
<input type="submit" name="submit" value="Submit"/>
<input type="submit" name="rename" value="Rename"/>
</form>

auriaks
01-17-2010, 10:52 PM
ok, looks great... Can you explain how it works? I dont understand the function thing... (also, why you use global?)

Nile
01-17-2010, 11:04 PM
It's glob(). It gets all the files in the directory.

The first function of the code gets all the files, and displays them in radios.

And the next part renames/unlinks.

Nile
01-19-2010, 10:46 PM
How'd it work out for you?

auriaks
02-15-2010, 03:02 PM
I am really sorry Nile, that I haven't seen your last posts... I was working on something else...

So, it works great :D

But one more question. I have mysql information of file:

$name
$directory
$username - who uploaded.

How I can let to see only those files to owners, which only they uploaded.
I don't want that everyone could delete all files...