function check_image($filename,$max_width,$max_height,$valid_types="") {
$err = '';
if (!is_array($valid_types)) $valid_types=array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG');
if (!is_readable($filename)) return "Server error";
$info = getImageSize($filename);
if (!isset($valid_types[$info[2]])) {
return 'Invalid file type. Valid are: '. implode(', ', $valid_types);
}
if ($info[0] > $max_width) $err = 'Image Width > '.$max_width.' pixels <br>';
if ($info[1] > $max_height) $err.= 'Image Height > '.$max_height.' pixels';
return $err;
}
$err = check_image($filename,$max_image_width,$max_image_height);
<?php
$destination_dir = "c:/";
$err = "";
$fieldname = "userfile";
$max_file_size = 64 * 1024;
$max_image_width = 800;
$max_image_height = 600;
if ($_SERVER['QUERY_STRING'] == 'OK') {
echo 'File uploaded sucessfully.<br>';
echo '<a href="'.$_SERVER['PHP_SELF'].'">Upload more</a>';
exit;
}
if (isset($_FILES[$fieldname])) {
$file=$_FILES[$fieldname];
if ($file['error'] == 4) {
$err = "File wasn't uploaded yet";
} elseif ($file['error']) {
$err = 'There was error while uploading file';
} elseif ($file['size'] == 0) {
$err = 'File is empty';
} elseif ($file['size'] > $max_file_size) {
$size = ceil($file['size'] / 1024);
$err = 'File size is '.$size.'K but maximum is '.($max_file_size / 1024)."K<br>";
/* image check begin. Can be removed */
} elseif ($err.= check_image($file['tmp_name'],$max_image_width,$max_image_height)) {
/* image check end */
} elseif (!is_writable($destination_dir.$file['name'])) {
$err = 'Server error 1.';
} elseif (!move_uploaded_file($file['tmp_name'], $destination_dir.$file['name'])) {
$err = 'Server error 2.';
} else {
header('Location: '.$_SERVER['PHP_SELF'].'?OK');
exit;
}
}
if ($err) echo '<b>Error:</b><br>'.$err;
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
Send this file: <input name="<? echo $fieldname; ?>" type="file">
<input type="submit" value="Send File">
</form>
<?
function check_image($filename,$max_width,$max_height,$valid_types="") {
$err = '';
if (!$valid_types) $valid_types=array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG');
if (!is_readable($filename)) return "Server error";
$info = getImageSize($filename);
if (!isset($valid_types[$info[2]])) {
return 'Invalid file type. Valid are: '. implode(', ', $valid_types);
}
if ($info[0] > $max_width) {
$err = 'Image Width is '.$info[0].' pixels but maximum is '.$max_width.'<br>';
}
if ($info[1] > $max_height) {
$err.= 'Image Height is '.$info[1].' pixels but maximum is '.$max_height.'<br>';
}
return $err;
}
?>
switch ($_FILES['file']['error']) {
case UPLOAD_ERR_INI_SIZE || UPLOAD_ERR_FORM_SIZE :
$errors['file'] = _("File must be lesser than 100 kB");
break;
case UPLOAD_ERR_PARTIAL:
$errors['file'] = _("There was error while uploading file");
break;
case UPLOAD_ERR_NO_FILE:
$errors['file'] = _("File wasn't uploaded yet");
}
MAX_FILE_SIZE is advisory to the browserа гдеже
<input type="hidden" name="MAX_FILE_SIZE" value="1024000">
возражения?Получается, что из примера надо бы убрать это поле, а в примечании добавить, что если аплоад не проходит, то можно попробовать его добавить.
(!is_writable($destination_dir.$file['name']))
} elseif (!is_writable($destination_dir)) {
} elseif (file_exists($destination_dir.$file['name']) && !is_writable($destination_dir.$file['name'])) {
вот это-то меня и подкосило.любом случае move_uploaded_file вернет ошибку, если прав нет...