Forum Moderators: coopster
<?php
if($_GET['add']==1){
$name= $_FILES['avatarfile']['name'];
$type= $_FILES['avatarfile']['type'];
$size= $_FILES['avatarfile']['size'];
$tmpname= $_FILES['avatarfile']['tmp_name'];
$error= $_FILES['avatarfile']['error'];
if($name)
{
//conditions for the file
$allowed=array("image/jpeg", "image/jpg", "image/png", "image/gif");
if (in_array($type, $allowed))
{
$randomdig=rand(0000,9999);
$namenew=$randomdig.$name;
$picturelocation="avatar/".$namenew;
//include('thumbs.php');
move_uploaded_file($tmpname, $picturelocation);
}else{
echo"File extension not allowed.";
}
}else{
echo"No file selected.";
}
}
else{
echo'<form action="upload.php?add=1" method="post">
<input type="file" name="avatarfile">
<input type="submit" value="Add" name="submit">
</form>';
}
?>
if($_FILES['avatarfile']['error']) {
// Echo out some error message
} else {
// Continue processing
}
<?php
$errors=null;
// HERE you can check for an empty "avatarfile."
if (isset($_POST['add'] and empty($_FILES['avatarfile'])) {
$errors .= "<li>You didn't select a file to upload.</li>";
}
if($_FILES){
$name= $_FILES['avatarfile']['name'];
$type= $_FILES['avatarfile']['type'];
$size= $_FILES['avatarfile']['size'];
$tmpname= $_FILES['avatarfile']['tmp_name'];
$error= $_FILES['avatarfile']['error'];
if ($error) { $errors .= "<li>File upload error: $error</li>"; }
else {
// Checking $name is irrelevant, you want to check the file, right?
//conditions for the file
$allowed=array("image/jpeg", "image/jpg", "image/png", "image/gif");
if (in_array($type, $allowed)) {
$randomdig=rand(0000,9999);
$namenew=$randomdig.$name;
$picturelocation="avatar/".$namenew;
$full_path = $_SERVER['DOCUMENT_ROOT'] . "/$picturelocation";
//include('thumbs.php');
move_uploaded_file($tmpname, $full_path);
// You need a response here
echo "</p>The file $name was uploaded and saved as <a href=\"/$picturelocation\">$newname</a></p>";
}
else{ $errors .= "<li>File type not allowed.</li>"; }
} // End if $_FILES
if ($errors) {
echo "<p>The following errors have occurred: <ul> $errors </ul>";
}
echo '<form action="upload.php method="post" enctype="multipart/form-data">
<input type="hidden" name="add" value="1">
<input type="file" name="avatarfile">
<input type="submit" value="Add" name="submit">
</form>';
?>