attach.php
- Code: Select all
<head><!-- set style and make upload form -->
<style type='text/css'>
a{text-decoration:none}
#msg{height:30px; float:right; background-color:yellow; box-shadow: 10px 10px 5px #888888; opacity:0.6;}</style>
<script src='http://code.jquery.com/jquery-latest.js'></script>
<script>$('#msg').fadeOut(5000);</script></head>
Choose a file to attach:<br />
<form enctype='multipart/form-data' action='upload.php' method='post'>
<input name='uploaded_file' type='file' size='3' /><br />
<input type='submit' value='Upload' />
</form><hr />
<?php
// open upload directory and get each entry
$upDirectory = opendir("/***/***/website/test/email/upload");
while($entryName = readdir($upDirectory)) {
$dirArray[] = $entryName;
closedir($upDirectory);
// count
$indexCount = count($dirArray);
$num = $indexCount-2;
if ($num == '1'){
$plu = '';}
else {$plural = 's'};
sort($dirArray);
// make upload contents table
print ("<table border='1' cellpadding='2' cellspacing='0'>\n");
print ("<tr><th>$num File$plural <a href='del.php'><button>Delete</button></a></th></tr>\n");
// loop through the array of files and print them all
for ($index=0; $index < $indexCount; $index++) {
if (substr("$dirArray[$index]", 0, 1) != "."){
print ("<tr><td><a href='upload/$dirArray[$index]' target='_blank'>$dirArray[$index]</a></td></tr>\n");
}
}
print ("</table>\n");
?>
So then if you then pressed 'upload' the form will submit the data. It sends the file to a temporary directory and from there I want to check a, is it there (in the tmp dir)? b, is it too big? c, does the filename already exist? If all these conditons are satisfied then move the file to the 'upload' folder. If any condition is not met, it should print the error, then run the attach.php again.
upload.php
- Code: Select all
<?php
$filename = basename($_FILES['uploaded_file']['name']);
//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
//Check if the file's size is 2000Kb or less
if ($_FILES["uploaded_file"]["size"] <= 2000000) {
//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'/upload/'.$filename;
//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 "<div id='msg'><p>Upload success!</p></div>";
include("attach.php");
}
} else {
echo "<div id='msg'><p>File already exists!</p></div>";
include("attach.php");
}
} else {
echo "<div id='msg'><p>File over 2Mb!</p></div>";
include("attach.php");
}
} else {
echo "<div id='msg'><p>No file uploaded!</p></div>";
include("attach.php");
}
?>
If I try to upload a file thats too big it doesn't move it from the tmp folder, but it returns the error message "No file uploaded!" instead of the 2Mb message
I thought maybe I messed up the else if nesting, I re-wrote them over and over, didn't help.
This is my first post, so I don't know the rules yet (sorry) can I post a link to the offending php?
Thanking you in advance, Kim

