I have an upload script and I want to require that certain things be filled in but leaving the preview image upload optional.
The problems I've had is leaving it optional but if uploaded it needs to check if it's in .gif format and if it has the correct file size limit.
Currently here is what I am using.
<?php
$title=addslashes(strip_tags($_POST['title']));
$desc=addslashes(strip_tags($_POST['description']));
$width=addslashes(strip_tags($_POST['width']));
$height=addslashes(strip_tags($_POST['height']));
$max_filesize = 10000000;
$filename=rand(500,2000).$_FILES['file']['name'];
$max_filesize2 = 500000;
$filename2=rand(500,2000).$_FILES['preview']['name'];
if ($title == ""){
echo "The title cannot be left blank.";
}
elseif($desc == ""){
echo "The description cannot be left blank.";
}
elseif ($width == ""){
echo "The width cannot be left blank.";
}
elseif ($height == ""){
echo "The height cannot be left blank.";
}
elseif ($_FILES['file']['name'] == "") {
echo "You did not select a file.";
}
elseif ($_FILES['file']['size'] > $max_filesize){
echo "The file cannot be bigger than 10 MB.";
}
elseif ($_FILES['preview']['size'] > $max_filesize2){
echo "The preview image must not be bigger than 50 KB.";
}
elseif ($_FILES['file']['type'] != 'application/x-shockwave-flash')
{
echo "The file must be in .swf format.";
}
elseif ($_FILES['preview']['type'] != 'image/gif')
{
echo "The image must be in .gif format.";
}
else{
$upload = dirname(__FILE__)."/uploads/";
copy($_FILES['file']['tmp_name'],$upload.$filename) or die("<br>The flash could not be uploaded.");
$upload2 = dirname(__FILE__)."/uploads/preview/";
copy($_FILES['preview']['tmp_name'],$upload2.$filename2) or die("<br>Couldn't upload preview image.");
$size=$_FILES['file']['size'];
$date=date("m/d/Y");
$sql=mysql_query("INSERT INTO `flash` ( `title` , `desc` , `wide` , `high` , `date` , `file` , `img` , `size` , `id` ) VALUES ('$title', '$desc', '$wide', '$high', '$date', '$filename', '$filename2', '$size', '0', '0', '')");
if ($sql){
echo "Your flash has been successfully submitted!<script Language=\"JavaScript\">
var URL = \"index.php\"
var speed = 0
function reload(){
location = URL
}
setTimeout(\"reload()\",speed);
</script>";
}
else {
echo "Failed";
}
}
}
else {
?>