PHP Unlinking

Need some help with php unlinking

Here is my php delete script

[php]<?php

$dir = dirname(FILE);
echo $dir;
$dir = “/Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/”;
$pictures = $_POST[‘data’];
//print_r ($pictures);
$imageone = $pictures[0];

echo “”. $imageone;

$filename = $dir . $imageone;



echo '</br>' .$filename;

if (is_file($filename)) {

chmod($filename, 0777);

if (unlink($filename)) {
echo ‘File deleted’;
} else {
echo ‘Cannot remove that file’;
}

} else {
echo ‘File does not exist’;
}[/php]

This cannot find the file, however if i include the filename like so

[php]

<?php $dir = dirname(__FILE__); echo $dir; $dir = "/Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/"; $pictures = $_POST['data']; //print_r ($pictures); $imageone = $pictures[0]; echo "". $imageone; $filename = $dir . test.jpg echo '' .$filename; if (is_file($filename)) { chmod($filename, 0777); if (unlink($filename)) { echo 'File deleted'; } else { echo 'Cannot remove that file'; } } else { echo 'File does not exist'; }[/php] It works fine, why can I not use a variable in the filename?

can you post the result of print_r ($pictures); so i can see the array.

Here is the result of my echo

added in print array

/[php]Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/themes/twentythirteen/incArray
(
[0] => 1420376775-medi.jpg

)
1420376775-medi.jpg
/Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/1420376775-medi.jpg
File does not exist[/php]

try this:
[php]
$filename = $dir.’/’.$_POST[‘data’];

if(is_file($filename)) {
if (unlink($filename)) {
echo ‘File deleted’;
} else {
echo ‘Cannot remove that file’;
}
}
else {
echo ‘File does not exist’;
}[/php]

Let me know how you get on with this snippet.
Red :wink:

Sorry for the late reply,

Surely that will break as you are trying to pass an Array to unlink and not a string?

I will give it a go though and let you know

It still has to be a string even though it is in an array ( I believe I’m correct???). I don’t see where it would break? However, I would just get rid of the that filename in the array if it is no longer needed

[php]unset($myFileNamesArray[‘example’]);[/php]

Indeed it will, (well it won’t break, but it won’t delete the file) what i was trying to find out if it is indeed an array.
Which leads me to, why have you created an array called data stored in an array called $_POST?

If it does balk on the $_POST, then add 0 like so [php]$_POST[‘data’][0][/php]

Let me know how you get on.
Red :wink:

PS: sorry for the edit, i spotted a typo.

Agreed. 8)
I had already whittled down some of the OP’s post so I kept that line in (well the $filename reference) to keep it relevant to their post.

Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service