PDA

View Full Version : uploading a file error ...


JaeSun
03-07-2005, 11:19 AM
i have this code ...

its a upload class (getting sick of my threads lately?) ...

everything works fine, except in one case ....

it does error checking, and if there is an error, it redirects and prints out an error message, and allows the user to try again ....

in the case that i am trying, it checks to see if the file exists, and if so, error's out ... (i havent checked other situations with other errors)

but when it error's out and gives the user another chance to upload a different file, it gives the user the same error everytime, even if the file doesnt exist .... and i dont know why

if you hit the back button, and try again, it works ....

upload_media.rtf has all the functions for the uploading and checking for errors ... it includes the class (class.upload.rtf) which holds the class of functions for uploading and error checking ...

upload.rtf is just the file that calls it, and displays the output ....

in upload_media.rtf, it is called everytime upload.php is called (or upload.rtf) .....

it has 3 if statements, one for if it needs to upload the file (which is checked based on whether a session "status" variable is set or not), one for if there is an error (which is checked on the session "status" variable being set and the $_GET action=sucess NOT being set, and a last one if it is successfull in uploading (which is by the SESSION status variable being set and the $_GET action=success being set ...

(the SESSION status variable is unset at every upload) so i know it is not being skipped when the user is trying to upload the second time (it only sets the SESSION status variable when it is trying to upload the file) ...

any ideas?

here is the code:

http://www.whiteazn.com/fbc/test/class.upload.rtf
http://www.whiteazn.com/fbc/test/upload.rtf
http://www.whiteazn.com/fbc/test/upload_media.rtf

scoutt
03-07-2005, 12:03 PM
without testing it it looks like it is erroring out on the directory.

if ($handle) {

that should be different I would think. you could use

if (is_dir($dir)) {

again, it is just looking at it. it checks for this first so if you want you can comment out the checking of the folder is set if you want to find out what is actually failing.

JaeSun
03-07-2005, 12:21 PM
in my code??

$directory = $dir;
if(!(is_dir($httpdir))) //directory doesn't exists, so need to create the directory
{
$directory = ftpmkdir("$ftppath", "$dir"); //creates the directory with appropriate 777 permissions
//if there is an error, the function will output the error message. If it gets
//to here, the directory was created successfully
}
$_SESSION['dir_loc'] = $directory;


or in the class.upload ??

most of that class is pre-written .... ill check again later when i retest it ?

scoutt
03-07-2005, 12:31 PM
the error is coming from the class isn't?

than the class it not written correctly.

JaeSun
03-07-2005, 12:46 PM
ya, its the class returning an error

elseif ($file_exists)
{
$result = FALSE;
$error = "This file already exists on the server, please try again.";
$_SESSION['status'] = "complete";
$_SESSION['error_msg'] = $error;
header("Location: upload.php");
}


its weird cuz it works the first time out, just, when i want it to do a second time after an error is detected, it doesnt work ....

the function is:

/***************************************************************************
Function: existing_file()
****************************************************************************
Instructions: This function will check to see if the file exists. If the file already exists on the
server in the upload directory, the function returns true, otherwise it returns false. No renaming
conventions where added because everyone has there own renaming techniques. You should
honestly come up with a system for systematically naming your files on the server. Such as
dropping the file name and adding the id number from the database to it and keeping the file
extension. You should pass unique names into the class, but if you don't it will be caught and
the file will not get uploaded.
****************************************************************************
Variables Required: $file_name, $upload_dir
****************************************************************************/

function existing_file() {
$file_name = trim($this->file_name); //Extract the file name
$upload_dir = $this->get_upload_directory(); //Extract the upload directory

if ($upload_dir == "ERROR") { //If directory not found
return true; //Return true
} else { //Else
$file = $upload_dir . $file_name; //Set file and file path
if (file_exists($file)) { //If file exists
return true; //Return true
} else { //Else
return false; //Return false
}
}
}


i guess ill go to phpfreaks and see if the author knows of anything ....

it somewhat looks right? i dont know how to check if a file exists yet, so maybe ill go learn and modify the code and see if that works ...

scoutt
03-07-2005, 01:20 PM
then you are not clearing the error variable

JaeSun
03-07-2005, 01:36 PM
that cant be it...unless your talking about something different than i am?

i even went back and unset $error everywhere...

if i try uploading an illegal file (only allow .gif extensions, and then try uploading a .jpg file), the first time trying to upload a file with an illegal extension around, it returns saying that it is not a valid extension, try uploading again with an illegal extension, and it returns with an illegal extension error message, as it should be ....

but then try uploading a valid extension, it returns with the "This file already exists on the server, please try again." message.

dont know why it errors out there

JaeSun
03-07-2005, 03:58 PM
i found the error.

in my upload form, it brings up 3 types of forums, one for video, one for audio, one for everything else.

in the first form, it has a hidden field specifying it.

when it goes to upload, based on that field, it sets the upload directory to the correct directory.

but on error, the form, i forgot to update the form with the new hidden field (i copied and pasted, made an update to the first one, but not the 2nd one) ..

so then, after trying to upload with after an error, it doesnt know which directory to upload to, and thus, in the class, errors out when it looking to check which upload directory to verify an existing file ....

man, talk about a headache for me!