Having a few problems with this script. Images are being uploaded but the error control that I have setup is quirky and doens't function correctly.
First thing is, I want to display an error message if upload form is empty (if they haven't browsed for an image). Currently the error message for my null upload field is a php browser error 'Warning: Unable to open 'none' for reading: No such file or directory'. I obviously want to display my own custom error message instead (see below).
Also, ..if values $a, $b, $c, and $d are true I'd like to echo the success results but that doens't seem to print out.
<?php
if(isset($_POST['submit'])){
$path = "c:\\apache\htdocs\\sitename\\images\\bios\\";
$max_size = 71000;
if (!$_POST['userfile']){
$a = FALSE;
$message[] = "You must browse for a file";
} else {
$a = TRUE;
}
if ($HTTP_POST_FILES['userfile']['size']>$max_size) {
$b = FALSE;
$message[] = "The file is too big";
} else {
$b = TRUE;
}
if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg")) {
$c = TRUE;
} else {
$c = FALSE;
$message[] = "Sorry, your picture must be in either a jpg or gif format";
}
$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
if (!$res) {
$d = FALSE;
$message[] = "Upload Failed!";
} else {
$d = TRUE;
}
if ($a AND $b AND $c AND $d) {
$message[] = "Upload sucessful<br>\n";
$message[] = "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>\n";
$message[] = "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>\n";
$message[] = "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>\n";
}
}
?>
<FORM ENCTYPE="multipart/form-data" ACTION="<? echo"$PHP_SELF?" ?>" METHOD="POST">
The file: <INPUT TYPE="file" NAME="userfile">
<INPUT NAME="submit" TYPE="submit" VALUE="Upload">
</FORM>
<?
//print error messages here
if ($message) {
echo "<div align=\"left\"><font color=red></b><br />\n";
foreach ($message as $key => $value) {
echo "$value. ";
}
echo "<br><br></font>";
}
?>