Click to See Complete Forum and Search --> : unlink() problem


bustya
09-30-2007, 05:22 AM
I'm relatively new to PHP but for the most part I'm catching on. I'm currently customizing a flat-file guest-book. The guest-book writes input values to an added line in a text file and then retrieves them to display the HTML, although you probably already knew that. Well, I added a user-file upload to the form so that users could add an avatar. It works great, no errors there. Now, I'm trying to add the unlink() function to another function that deletes a given post, in essence killing two birds with one stone, i.e. removing the post and the avatar from it's folder in one go. I've tried several syntaxes and almost had it once, (going by the error message) except I couldn't call just the one input value (image name), but rather the entire line. I hope this is enough info + the code below to figure out where I'm stuck.


upload script
There's a couple of echos in there I need to remove but...

<?php

//Check that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
//Check if the file is JPEG/GIF and it's size is less than 30Kb
$filename = basename($_FILES['uploaded_file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "gif") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") || ($_FILES["uploaded_file"]["type"] == "image/gif") &&
($_FILES["uploaded_file"]["size"] < 30000)) {
$random_digit=rand(0000,9999);

//combine random digit to you file name to create new file name
//use dot (.) to combile these two variables
$newplus=$random_digit.".";
$newfilename=$newplus.$ext;


//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'/images/'.$newfilename;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname)) {
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
echo "It's done! The file has been saved as: ".$newname;
} else {
echo "Error: A problem occurred during file upload!";
}
} else {
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
} else {
echo "Error: Only .jpg or .gif images please.";
}
} else {
echo "Error: No file uploaded, it was probably too big. It needs to be less than 30kb.";
}
?>



And here's the post deleting function I'm trying to add the unlink() to.


function doDelete($pass,$num) {
global $settings;
if ($pass != $settings[apass]) {problem('Wrong password! The entry hasn\'t been deleted.');}
$lines=file($settings['logfile']);

if (isset($_POST['addban']) && $_POST['addban']=='YES') {
gbook_banIP(trim(array_pop(explode("\t",$lines[$num]))));
}

unset($lines[$num]);
$lines=implode('',$lines);
$fp = fopen($settings['logfile'],'wb') or problem("Couldn't open links file ($settings[logfile]) for writing! Please CHMOD all $settings[logfile] to 666 (rw-rw-rw)!");
fputs($fp,$lines);
fclose($fp);
?>


The "images" folder is in the same directory as both of these files.

The image's name is the first in "$lines". For example, when I got the error that came closest to collecting the file's name, I had added this to the function above:


unlink($lines[images/$newfilename]);


And I got this warning:


Warning: unlink(4529.gif somename somemessage September 30, 2007 0 75.109.26.51 ) [function.unlink]: No such file or directory in...


Any help is appreciated. Thanks in advance.

Webnerd
09-30-2007, 06:25 AM
Can you post a sample from the file that stores the data and image path that has been uploaded?

NightShift58
10-01-2007, 02:31 AM
Try:

$thisIMG = "images/" . $newfilename;
unlink($lines[$thisIMG]);

or just:

unlink($lines['images/' . $newfilename]);

bustya
10-01-2007, 04:21 AM
I got it, finally. Here's the solution:


$myline=explode("\t",$lines[$num]);
$avadel='images/'.rtrim($myline[0]);
if (empty($myline[0])) {$avadel='No Avatar to delete';}
else
{
unlink($avadel);
}



Thanks for your help though.

bustya
10-01-2007, 02:11 PM
Ah, just when I thought I had it all worked out I tried it in IE and one of my recent changes really does a number on IE. The CPUs max and the page won't load. FF doesn't have this issue with the page though.

bustya
10-01-2007, 02:50 PM
Ah, i figured out why IE had a problem with my changes... Since I had made changes to the guestbook and I had posts before the new changes, I thought i'd be slick and edit the txt file that the posts are written to directly by opening it and adding the changes to the old posts. Well, IE didn't like that at all. I deleted all the old posts and now IE doesn't have a problem with the latest version of the script. Silly IE.