Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 22nd, 2005, 10:31 AM   #1
TCStyle
Programmer
 
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0 TCStyle is on a distinguished road
file_exists() problemo

We'll I'm programming a file uploader with php. I found a simple script on the net and I'm adding onto it. I've run into a problem though, my if statement that contains my file_exists check isn't working. Heres the code I have:

<?php
$filename = str_replace(' ', '', $_FILES['upload_file']['name']);
$tblw = strlen($filename);
$ext = substr($filename, $tblw-3, $tblw);
$filesize= $HTTP_POST_FILES['upload_file']['size'];
$name= $_POST['FName'];
$stripName= strip_tags($name);
if ($filesize > 262144) {
echo "Sorry your file size is to big!";
echo "<br />";
echo "File size limit is 262144 bytes (or .25 MB)";
}
if ($filesize = 0) {
echo "No file was uploaded!";
}
if (file_exists("/upload/".$stripName)) {
echo "A file with this exact name already exists. Please go back and rename your file!";
}
if ($ext == 'png' OR $ext == 'gif' OR $ext == 'jpg' || $filesize <= 262144 || $filesize != 0 || !file_exists("/upload/".$stripName)) {
$uploaddir = "htdocs/upload/";
$uploadfile = $uploaddir . $stripName . "." . $ext;
$file = 'htdocs/upload/' . $stripName . '.' . $ext;
move_uploaded_file($_FILES['upload_file']['tmp_name'], $uploadfile);
echo "File was uploaded successfully!";
};
?>

Basically I want it so that if a file with the same name tries to get uploaded to my "upload" folder, then and error message comes up. But, for some reason, if the file exists, it just says "file was uploaded sucessfully", and doesn't overwrite the file that was already there? Any help is apperciated!
TCStyle is offline   Reply With Quote
Old Feb 22nd, 2005, 12:46 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You're using three if statements in there. Use if - else if - else if.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 27th, 2005, 5:41 PM   #3
Vorlin
Newbie
 
Join Date: Feb 2005
Posts: 12
Rep Power: 0 Vorlin is on a distinguished road
From what I see, there's a few things that need to be fixed along with the file_exists issue.

if ($filesize > 262144) {
echo "Sorry your file size is to big!";
echo "<br />";
echo "File size limit is 262144 bytes (or .25 MB)";
}
if ($filesize = 0) {
echo "No file was uploaded!";
}
if (file_exists("/upload/".$stripName)) {
echo "A file with this exact name already exists. Please go back and rename your file!";
}

I'd change the above to:

$stripName2 = "htdocs/upload/" . $stripName;

if ($filesize == 0 || $filesize > 262144) {
    echo "No file was uploaded or the file's size was too big (.25mb allowed).<br />";
} else if (file_exists($stripName2)) {
    echo "A file already exists by that name. Please go back and rename.";
} else {
    // do some successful stuff
}

The $filesize = 0 check was incorrect because you were assigning the filesize to 0 instead of comparing. One = means assignment, two = mean comparison.

In your file_exists check, you have an absolute directory called upload and you append the filename onto it whereas in your successful file upload, you have a relative directory called htdocs/upload to which you append the filename and extension.

Hope this helps!
__________________
--Vorlin

"Systems administration - it's not just my job, it's how I buy video games!"
Vorlin is offline   Reply With Quote
Old Feb 27th, 2005, 7:42 PM   #4
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Do the file exists check first!

$stripName2 = "htdocs/upload/" . $stripName;

if (file_exists($stripName2)) {
    echo "A file already exists by that name. Please go back and rename.";
} else if ($filesize == 0 || $filesize > 262144) {
    echo "No file was uploaded or the file's size was too big (.25mb allowed).<br />";
} else {
    // do some successful stuff
}
__________________


Last edited by tempest; Feb 27th, 2005 at 10:41 PM.
tempest is offline   Reply With Quote
Old Feb 27th, 2005, 10:00 PM   #5
Vorlin
Newbie
 
Join Date: Feb 2005
Posts: 12
Rep Power: 0 Vorlin is on a distinguished road
Hehe, you forgot to switch the echo statements around in that rewrite.
__________________
--Vorlin

"Systems administration - it's not just my job, it's how I buy video games!"
Vorlin is offline   Reply With Quote
Old Feb 27th, 2005, 10:41 PM   #6
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
I have no idea what you're talking about, looks fine to me :p
__________________

tempest is offline   Reply With Quote
Old Mar 2nd, 2005, 2:27 PM   #7
ChefBoiAreDee
Newbie
 
Join Date: Mar 2005
Posts: 5
Rep Power: 0 ChefBoiAreDee is on a distinguished road
Take this line
[php]if ($ext == 'png' OR $ext == 'gif' OR $ext == 'jpg' || $filesize <= 262144 || $filesize != 0 || !file_exists("/upload/".$stripName)) {[/php]
and try replacing it with this line
[php]
if (($ext == 'png' || $ext == 'gif' || $ext == 'jpg') && ($filesize <= 262144 && $filesize != 0 && !file_exists("/upload/".$stripName))) {[/php]


You should then add an else or as Ooble said several eleseif statements.
The else should handle what to do if the file was not of the correct type or the size was innapropriate or the file already exists.

Personally I would rewrite this entire snippet.

It would look like this.
[php]
<?php
$filename = str_replace(' ', '', $_FILES['upload_file']['name']);
$tblw = strlen($filename);
$ext = substr($filename, $tblw-3, $tblw);
$filesize= $HTTP_POST_FILES['upload_file']['size'];
$name= $_POST['FName'];
$stripName= strip_tags($name);
if ($filesize > 262144) {
echo "Sorry your file size is to big!";
echo "<br />";
echo "File size limit is 262144 bytes (or .25 MB)";
} elseif ($filesize = 0) {
echo "Gah! Something bad happened and your file was truncated.<br>\n";
} elseif (file_exists("/upload/".$stripName)) {
echo "A file with this exact name already exists. Please go back and rename your file!";
} elseif ($ext != "png" && $ext != "gif" && $ext != "jpg") {
echo "You have to upload an image file k.thx.";
} else {
$uploaddir = "htdocs/upload/";
$uploadfile = $uploaddir . $stripName . "." . $ext;
$file = 'htdocs/upload/' . $stripName . '.' . $ext;
move_uploaded_file($_FILES['upload_file']['tmp_name'], $uploadfile);
echo "File was uploaded successfully!";
}
?>
[/php]

Last edited by ChefBoiAreDee; Mar 2nd, 2005 at 3:06 PM.
ChefBoiAreDee is offline   Reply With Quote
Old Mar 3rd, 2005, 5:43 PM   #8
TCStyle
Programmer
 
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0 TCStyle is on a distinguished road
thanks

Sorry for not replying... my computer got robbed, haven't had a chance to be on the net for a long time. Anway, thanks for all the help. I made a few of my own modifications to the script in the previous reply and it works great. Thanks!
TCStyle is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:00 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC