![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You're using three if statements in there. Use if - else if - else if.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2005
Posts: 12
Rep Power: 0
![]() |
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!" |
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
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. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Feb 2005
Posts: 12
Rep Power: 0
![]() |
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!" |
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
I have no idea what you're talking about, looks fine to me :p
__________________
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Mar 2005
Posts: 5
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0
![]() |
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! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|