/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Delete previously uploaded file from server?

Ok, so I have a page where an authorized user can upload a file… there is also a list of uploaded files (each has info in the database) and a “delete” check-box to remove it… It removes the info from the database, but how do I remove the file itself from the “docs” folder on the server?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 08.2012 — [url=http://php.net/unlink]unlink[/url]()
Copy linkTweet thisAlerts:
@amandaNHTauthorMay 08.2012 — I tried that, but it's not working. There's a "close file" thingy that gets put in before that tries to open the file and returns the error "can't open file" -- probably because the file is a PDF... should the file-type be causing a problem with the unlink() function?

Here's what I've got to delete the file:
[code=php]
$delFile = "docs/".$rowfile;
$fh = fopen($delFile, 'w') or die("can't open file");
fclose($fh);
unlink($delFile);
[/code]


...and it just returns the "can't open file" error.
Copy linkTweet thisAlerts:
@NogDogMay 08.2012 — Well, if it's dieing on that line, then the following lines never get executed. So you might want to add some debug code to find out why the fopen is failing.
[code=php]
$delFile = "docs/".$rowfile;
if( ! file_exists($delFile)) {
die("Cannot find file '$delFile'");
}
if( ! is_writable($delFile)) {
die("File '$delFile' is not writable");
}
ini_set('display_errors', true);
error_reporting(E_ALL);
$fh = fopen($delFile, 'w') or die("can't open file");
fclose($fh);
unlink($delFile);
[/code]
Copy linkTweet thisAlerts:
@amandaNHTauthorMay 08.2012 — Error message:
[CODE]Warning: fopen(docs/) [function.fopen]: failed to open stream: Is a directory in /home/mywebsite/public_html/sites/misc/testfolder/adm_docs.php on line 142
can't open file[/CODE]
Copy linkTweet thisAlerts:
@svidgenMay 08.2012 — Sounds like [B]$rowfile[/B] isn't defined ...
Copy linkTweet thisAlerts:
@amandaNHTauthorMay 08.2012 — Except that "$rowfile" is created just a few lines up from the filename from the database. It [I]should[/I] work, but it doesn't. I'm going to try tweaking a couple of other things & then I'll get back to you guys. Thanks a ton for the help thus far.
Copy linkTweet thisAlerts:
@amandaNHTauthorMay 10.2012 — The script runs! Now i just have to fix the file-uploader to work properly so I can test this correctly! ?

Thanks!

The [B][I]problem [/I][/B]was this:
[CODE]$delFile = "docs/".$rowfile;[/CODE]
The [B][I]solution [/I][/B]was this:
[CODE]$delFile = "/docs/".$rowfile;[/CODE]
×

Success!

Help @amandaNHT spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...