Вот тебе готовый работающий код. Загрузка фотографии. (что первое попалось) Разбирайся сам.
new_photo.php
Код | <html> <head> <title><?=$name_title?> • Сайт «Фоторепортер»</title> <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"> <link rel=stylesheet href="/style.css" type="text/css"> <meta name="Robots" content="NOINDEX, FOLLOW">
</head> <body style="margin: 5px 10px 5px 10px;"> <h1>Загрузить новые файлы</h1> <p><?=$error_echo?> <? include $_SERVER['DOCUMENT_ROOT']."/xxxxx.php"; // переменные + подключаемся к базе
$sql_u = " SELECT * FROM stock_user WHERE login='$autor' "; $result_u = mysql_query($sql_u,$connection);//получаем данные пользователя
while ($row_in = mysql_fetch_array($result_u)) { $fam= $row_in['fam']; $io = $row_in['io']; $user_num = $row_in['user_num']; } ?>
<p>Автор <?=$fam?> <?=$io?> (<?=$autor?>). <p><form action="new_photo_add.php" method="post" enctype="multipart/form-data" name="form1"> <input name="autor" type="hidden" value="<?=$autor?>"> <input name="id_photo" type="hidden" value="<?=time()?>"> <table width="100%" border="1" cellspacing="2" cellpadding="2" bordercolor=#996600 style="border-collapse:collapse; font: normal 11px tahoma,Verdana,Arial,Helvetica,Geneva,sans-serif;"> <tr> <td>Файл</td> <td><input name="file_in" type="file" size="60"></td> <td> </td> </tr> <tr> <td>Раздел</td> <td><select name="dir_in" size="5"> selected <? $sqld = " SELECT * FROM stock_dir "; $resultd = mysql_query($sqld,$connection); //список разделов
while ($rowd = mysql_fetch_array($resultd)) { $id_dir = $rowd['id_dir']; $name_dir = $rowd['name_dir']; if ($id_dir == $dir_in){$selected = "selected";} echo "<option ".$selected." value=\"$id_dir\">$name_dir</option>"; unset ($dir, $name_dir, $id_dir, $selected); } ?> </select></td> <td> </td> </tr> <tr> <td>Название</td> <td><input type=text name="name_photo" id="name_photo" value="<?=$name_photo?>" size=40></td> <td> </td> </tr> <tr> <td>Цена</td> <td><input type="text" name="price" id="price" value="<?=$price?>"> <select name="currency" id="currency"> <option value="$" <?if ($currency == "$"){echo "selected";}?>>$</option> <option value="руб." <?if ($currency == "руб."){echo "selected";}?>>руб.</option> </select></td> <td> </td> </tr> <tr> <td>Исходный носитель </td> <td><input type=text name="film" id="film" value="<?=$film?>" size=40></td> <td> </td> </tr> <tr> <td>Максимальный размер</td> <td><input type=text name="size" id="size" value="<?=$size?>" size=40></td> <td> </td> </tr> <tr> <td>Возможность исключительного права </td> <td><select name="ex" id="ex"> <option value="да" <?if ($ex == "да"){echo "selected";}?>>да</option> <option value="нет" <?if ($ex == "нет"){echo "selected";}?>>нет</option> </select></td> <td> </td> </tr> <tr> <td>Срок получения</td> <td><input type=text name="srok" id="srok" value="<?=$srok?>"></td> <td> </td> </tr> <tr> <td>Дополнительная информация</td> <td><textarea name="text" cols="60" rows="3" id="text"><?=$text?></textarea></td> <td> </td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="Загрузить"></td> <td> </td> </tr> </table>
</form> <p> </p>
|
new_photo_add.php
Код | <html> <head> <title><?=$name_title?> • Сайт «Фоторепортер»</title> <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"> <link rel=stylesheet href="/style.css" type="text/css"> <meta name="Robots" content="NOINDEX, FOLLOW">
</head> <body style="margin: 5px 10px 5px 10px;">
<? include $_SERVER['DOCUMENT_ROOT']."/xxxxx.php"; // переменные
if ($_FILES['file_in']['size'] <1) { $error_echo = "<p align=center><font color=800000><b>Файл не загружен.<br> Вероятно, вы забыли указать файл для загрузки.</b></font>"; include "new_photo.php"; return; }
If ($_FILES['file_in']['size'] > 102400) { $error_echo = "<p align=center><font color=800000><b>Ваш файл больше чем допустимо.<br> Размер вашего файла ".$_FILES[file_in]['size']."байт.<br> Максимальный разрешенный размер файла 102400 байт.</b></font>"; include "new_photo.php"; return; }
$text = trim ($text); $text = str_replace ("\"", """, $text); $text = str_replace ("\n", "<br>", $text); $text = str_replace ("\r", "<br>", $text); $text = str_replace ("\\", "", $text); $text = str_replace ("</P>", "", $text); $text = str_replace ("<p><p", "<p", $text); $text = str_replace ("?>", ">", $text); $text = str_replace ("<p><br>", "<p>", $text); $text = str_replace ("<br><br>", "<br>", $text); //сокращено
$name_photo = trim ($name_photo);
$image = $file_in; $o_file = $image;
$image_info = getImageSize($o_file) ; // see EXIF for faster way switch ($image_info['mime']) { case 'image/gif': $tip = "gif"; if (imagetypes() & IMG_GIF) { // not the same as IMAGETYPE $o_im = imageCreateFromGIF($o_file) ; } else { $ermsg = 'GIF images are not supported<br />'; } break; case 'image/jpeg': $tip = "jpg"; if (imagetypes() & IMG_JPG) { $o_im = imageCreateFromJPEG($o_file) ; } else { $ermsg = 'JPEG images are not supported<br />'; } break; case 'image/png': $tip = "png"; if (imagetypes() & IMG_PNG) { $o_im = imageCreateFromPNG($o_file) ; } else { $ermsg = 'PNG images are not supported<br />'; } break; /*case 'image/wbmp': $tip = "wbmp"; if (imagetypes() & IMG_WBMP) { $o_im = imageCreateFromWBMP($o_file) ; } else { $ermsg = 'WBMP images are not supported<br />'; } break;*/ default: $error_echo = "<p align=center><font color=800000><b>Такой тип файла запрещен для загрузки.</b></font>"; include "new_photo.php"; return; break; } $name_new_file_0 = time(); $name_new_file = $name_new_file_0.".".$tip; $uploadfile = $_SERVER['DOCUMENT_ROOT']."/путь_до_/catalogue/".$dir_in."/".$name_new_file; //echo "<p> $uploadfile"; if (!isset($ermsg)) { $max_width = 800; // размер фото горизонтальной $max_height = 600;// размер фото вертикальной $width = imagesx($o_im) ; $height = imagesy($o_im) ;
// если размер не больше if ($max_width>=$width && $max_height>=$height) { if(copy ($file_in, $uploadfile)) {
chmod ($uploadfile, 0777); $sql = " INSERT INTO `stock_photo` ( `id_photo` ,`dir` , `name_photo` , `autor` , `price` , `currency` , `film` , `size` , `ex` , `srok` , `text` ) VALUES ( '$name_new_file_0', '$dir_in', '$name_photo', '$autor', '$price', '$currency', '$film', '$size', '$ex', '$srok', '$text' )"; $result = mysql_query($sql,$connection) or die ('<p>Не возможно подключиться к базе данных. <br>Ошибка: ' . mysql_error()); if ($result) { $error_echo = "<p align=center><font color=800000><b>Фотография загружена удачно.</font></b>"; } else { $error_echo = "<p align=center><font color=800000><b>Ошибка записи в базу</font></b>"; } } else { $error_echo = "<p align=center><font color=800000><b>Ошибка: файл не загружен.</b></font>"; } include "new_photo.php"; return; }
// если размер больше $x_ratio = $max_width / $width; $y_ratio = $max_height / $height;
if ( ($width <= $max_width) && ($height <= $max_height) ) { $tn_width = $width; $tn_height = $height; } else if (($x_ratio * $height) < $max_height) { $tn_height = ceil($x_ratio * $height); $tn_width = $max_width; } else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height; }
$t_im = imageCreateTrueColor($tn_width,$tn_height); imageCopyResampled($t_im, $o_im, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height); switch ($tip) { case 'jpg':ImageJpeg($t_im, $uploadfile, 50);break; case 'gif':imagegif($t_im, $uploadfile);break; case 'png':imagepng($t_im, $uploadfile);break; } chmod ($uploadfile, 0777); imageDestroy($o_im); imageDestroy($t_im); $sql = " INSERT INTO `stock_photo` ( `id_photo` ,`dir` , `name_photo` , `autor` , `price` , `currency` , `film` , `size` , `ex` , `srok` , `text` ) VALUES ( '$name_new_file_0', '$dir_in', '$name_photo', '$autor', '$price', '$currency', '$film', '$size', '$ex', '$srok', '$text' )"; $result = mysql_query($sql,$connection) or die ('<p>Не возможно подключиться к базе данных. <br>Ошибка: ' . mysql_error()); if ($result) { $error_echo = "<p align=center><font color=800000><b>Фотография загружена удачно.</font></b>"; } else { $error_echo = "<p align=center><font color=800000><b>Ошибка записи в базу</font></b>"; } include "new_photo.php"; return; } return isset($ermsg)?$ermsg:NULL; ?>
| |