Difference between revisions of "PHP 0"
From Ilianko
(→Сесии) |
(→форма) |
||
| Line 514: | Line 514: | ||
== Сесии == | == Сесии == | ||
=== форма === | === форма === | ||
| + | <pre> | ||
| + | <html> | ||
| + | <body> | ||
| + | |||
| + | <?php | ||
| + | |||
| + | error_reporting( E_ALL ^ E_NOTICE); | ||
| + | session_start(); | ||
| + | |||
| + | |||
| + | if(isset($_GET['errorList'])) | ||
| + | { | ||
| + | print $_GET['errorList']; | ||
| + | }; | ||
| + | ?> | ||
| + | |||
| + | <div style="position:relative; top:100px; left:100px"> | ||
| + | <form method="post" name="comment" enctype="multipart/form-data" action="form8a.php" > | ||
| + | <table> | ||
| + | <tr> | ||
| + | <td>Email: </td> | ||
| + | <td><input name="email" type="text" size="20" maxlength="199" value="<?= $_SESSION['email'] ?>" /></td></tr> | ||
| + | <tr> | ||
| + | <td>Име </td> | ||
| + | <td><input name="name" type="text" size="20" maxlength="60" value="<?= $_SESSION['name'] ?>" /></td></tr> | ||
| + | <tr> | ||
| + | <td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"><?= $_SESSION['comment'] ?></textarea></td></tr> | ||
| + | <tr> | ||
| + | <tr> | ||
| + | <td>Картинка: </td><td><input type="file" name="image" title="Select Image"></td></tr> | ||
| + | <tr> | ||
| + | |||
| + | <td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати"> | ||
| + | </td></tr></table> | ||
| + | </form> | ||
| + | </div> | ||
| + | |||
| + | </body> | ||
| + | </html> | ||
| + | </pre> | ||
| + | |||
=== обработка === | === обработка === | ||
<pre> | <pre> | ||
Revision as of 10:28, 7 October 2011
Contents
HTML форма
<body>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" action="" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"></textarea></td></tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати">
</td></tr></table>
</form>
</div>
</body>
</html>
PHP вградени промеливи
<html>
<body>
<?php
print "POST ->";
print_r( $_POST);
print "GET ->";
print_r($_GET);
print "SERVER ->";
print_r($_SERVER);
print "SESSION ->";
print_r($_SESSION);
?>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" action="" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"></textarea></td></tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати">
</td></tr></table>
</form>
</div>
</body>
</html>
Запис във файл
<html>
<body>
<?php
print "POST ->";
print_r( $_POST);
print "GET ->";
print_r($_GET);
if (isset($_POST['submit']))
{
$fd = fopen('../user/data.txt' , 'a');
fwrite ( $fd , $_POST['email']."\n");
fwrite ( $fd , $_POST['email']."\n");
fwrite ( $fd , $_POST['comment']."\n");
fclose($fd);
}
?>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" action="" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"></textarea></td></tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати">
</td></tr></table>
</form>
</div>
</body>
</html>
Качване на файл
<html>
<body>
<?php
print "POST ->";
print_r( $_POST);
print "FILES ->";
print_r($_FILES);
if (isset($_POST['submit']))
{
$fd = fopen('../user/data.txt' , 'a');
fwrite ( $fd , $_POST['email']."\n");
fwrite ( $fd , $_POST['email']."\n");
fwrite ( $fd , $_POST['comment']."\n");
fclose($fd);
}
?>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" enctype="multipart/form-data" action="" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"></textarea></td></tr>
<tr>
<tr>
<td>Картинка: </td><td><input type="file" name="image" title="Select Image"></td></tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати">
</td></tr></table>
</form>
</div>
</body>
</html>
Запис на качен файл
<html>
<body>
<?php
print "POST ->";
print_r( $_POST);
print "FILES ->";
print_r($_FILES);
if (isset($_POST['submit']))
{
if($_FILES["image"]['name'] )
{
$tempFileName = $_FILES['image']['tmp_name']; // temporary file at server side
copy ( $_FILES['image']['tmp_name'], '../user/'.$_FILES["image"]['name'] );
}
}
?>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" enctype="multipart/form-data" action="" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"></textarea></td></tr>
<tr>
<tr>
<td>Картинка: </td><td><input type="file" name="image" title="Select Image"></td></tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати">
</td></tr></table>
</form>
</div>
</body>
</html>
Обработка на изображение
<html>
<body>
<?php
print "POST ->";
print_r( $_POST);
print "FILES ->";
print_r($_FILES);
if (isset($_POST['submit']))
{
$maxSize = 500; //kB maximum file size
$errorList = '';
$newImageHeight = 400;
$newImageWidth = 600; //px
if($_FILES["image"]['name'] )
{
$fileNameParts = explode( ".", $_FILES["image"]['name']);
$fileExtension = end( $fileNameParts ); // part behind last dot
$photo_type = $_FILES["image"]['type'];
}
else $errorList = "<br />Моля изберете файл";
if ( !$errorList && $fileExtension != "jpg" && $fileExtension != "JPG" )
{
$errorList .= "<br /> format is not supported";
}
$photoSize = $_FILES["image"]['size']; // size of uploaded file
if ( !$errorList && $photoSize == 0 )
{
$errorList = "<br />$photoFileName - wrong data transfer";
}
if ( !$errorList && $photoSize > $maxSize*1024)
{
$errorList = "<br>$photoFileName - image is to large";
}
if (!$errorList)
{
$tempFileName = $_FILES['image']['tmp_name']; // temporary file at server side
$tempFile = fopen( $tempFileName, "r" );
$binaryPhoto = fread( $tempFile, filesize( $tempFileName ) );
$src_img = imagecreatefromstring( $binaryPhoto ); // try to create image
if ( !$src_img )
{
$errorList = "<br>$photoFileName - wrong image format";;
exit( -1 );
}
$width = imagesx( $src_img ); // get original source image width
$height = imagesy( $src_img ); // and height
$yCrop = 0;
$xCrop = 0;
$in_prop = $width / $height;
$out_prop = $newImageWidth / $newImageHeight;
//resize main image, crop if nessery
$image = imagecreatetruecolor( $newImageWidth, $newImageHeight );
if ( $width > $height )
{
if ( $in_prop > $out_prop ) $xCrop = round( (($width - ( $height* $out_prop))/2) );
else $yCrop = round( (($height - ($width/$out_prop))/2));
}
else
{
$yCrop = round( (($height - ($width/$out_prop))/2));
}
imagecopyresampled( $image, $src_img, 0, 0, $xCrop, $yCrop,$newImageWidth, $newImageHeight,($width-$xCrop*2), ($height-$yCrop*2) );
$textColor= imagecolorallocate($image,255,0,0);
$font = "./VeraSe.ttf";
$font_size = 40;
$angle = 25;
$x = 40;
$y = 30;
imagettftext( $image, $font_size, $angle, $x, $y, $textColor, $font, 'TEST');
imagegammacorrect ( $image, 1.0, 1.15 );
if( imagejpeg( $image, '../user/test.JPG' )) $errorList = "Picture has been successfully uploaded!\n<br>\n";
}
}
print $errorList;
?>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" enctype="multipart/form-data" action="" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"></textarea></td></tr>
<tr>
<tr>
<td>Картинка: </td><td><input type="file" name="image" title="Select Image"></td></tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати">
</td></tr></table>
</form>
</div>
</body>
</html>
Прехвърляне данни между скриптове
форма
<html>
<body>
<?php
print "GET ->";
print_r( $_GET);
if(isset($_GET['errorList']))
{
print $_GET['errorList'];
};
?>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" enctype="multipart/form-data" action="form7a.php" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"></textarea></td></tr>
<tr>
<tr>
<td>Картинка: </td><td><input type="file" name="image" title="Select Image"></td></tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати">
</td></tr></table>
</form>
</div>
</body>
</html>
обработка
<?php
if (isset($_POST['submit']))
{
$fd = fopen('../user/data.txt' , 'a');
fwrite ( $fd , $_POST['email']."\n");
fwrite ( $fd , $_POST['email']."\n");
fwrite ( $fd , $_POST['comment']."\n");
fclose($fd);
$maxSize = 500; //kB maximum file size
$errorList = '';
$newImageHeight = 400;
$newImageWidth = 600; //px
if($_FILES["image"]['name'] )
{
$fileNameParts = explode( ".", $_FILES["image"]['name']);
$fileExtension = end( $fileNameParts ); // part behind last dot
$photo_type = $_FILES["image"]['type'];
}
else $errorList = "<br />Моля изберете файл";
if ( !$errorList && $fileExtension != "jpg" && $fileExtension != "JPG" )
{
$errorList .= "<br /> format is not supported";
}
$photoSize = $_FILES["image"]['size']; // size of uploaded file
if ( !$errorList && $photoSize == 0 )
{
$errorList = "<br /> wrong data transfer";
}
if ( !$errorList && $photoSize > $maxSize*1024)
{
$errorList = "<br>image is to large";
}
if (!$errorList)
{
$tempFileName = $_FILES['image']['tmp_name']; // temporary file at server side
$tempFile = fopen( $tempFileName, "r" );
$binaryPhoto = fread( $tempFile, filesize( $tempFileName ) );
$src_img = imagecreatefromstring( $binaryPhoto ); // try to create image
if ( !$src_img )
{
$errorList = "<br>$ wrong image format";;
exit( -1 );
}
$width = imagesx( $src_img ); // get original source image width
$height = imagesy( $src_img ); // and height
$yCrop = 0;
$xCrop = 0;
$in_prop = $width / $height;
$out_prop = $newImageWidth / $newImageHeight;
//resize main image, crop if nessery
$image = imagecreatetruecolor( $newImageWidth, $newImageHeight );
if ( $width > $height )
{
if ( $in_prop > $out_prop ) $xCrop = round( (($width - ( $height* $out_prop))/2) );
else $yCrop = round( (($height - ($width/$out_prop))/2));
}
else
{
$yCrop = round( (($height - ($width/$out_prop))/2));
}
imagecopyresampled( $image, $src_img, 0, 0, $xCrop, $yCrop,$newImageWidth, $newImageHeight,($width-$xCrop*2), ($height-$yCrop*2) );
$textColor= imagecolorallocate($image,255,0,0);
$font = "./VeraSe.ttf";
$font_size = 40;
$angle = 25;
$x = 40;
$y = 30;
imagettftext( $image, $font_size, $angle, $x, $y, $textColor, $font, 'TEST');
imagegammacorrect ( $image, 1.0, 1.15 );
if( imagejpeg( $image, '../user/test.JPG' )) $errorList = "Picture has been successfully uploaded!\n<br>\n";
}
else header("Location: form7.php?errorList=$errorList");
}
else header("Location: form7.php"); /* Redirect browser */
?>
<html>
<body>
<?php print $errorList ?>
<img src="../user/test.JPG" />
</body>
</html>
Сесии
форма
<html>
<body>
<?php
error_reporting( E_ALL ^ E_NOTICE);
session_start();
if(isset($_GET['errorList']))
{
print $_GET['errorList'];
};
?>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" enctype="multipart/form-data" action="form8a.php" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="<?= $_SESSION['email'] ?>" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="<?= $_SESSION['name'] ?>" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"><?= $_SESSION['comment'] ?></textarea></td></tr>
<tr>
<tr>
<td>Картинка: </td><td><input type="file" name="image" title="Select Image"></td></tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати">
</td></tr></table>
</form>
</div>
</body>
</html>
обработка
<?php
session_start();
if (isset($_POST['submit']))
{
$_SESSION['email'] = $_POST['email'];
$_SESSION['name'] = $_POST['name'];
$_SESSION['comment'] = $_POST['comment'];
$maxSize = 500; //kB maximum file size
$errorList = '';
$newImageHeight = 400;
$newImageWidth = 600; //px
if($_FILES["image"]['name'] )
{
$fileNameParts = explode( ".", $_FILES["image"]['name']);
$fileExtension = end( $fileNameParts ); // part behind last dot
$photo_type = $_FILES["image"]['type'];
}
else $errorList = "<br />Моля изберете файл";
if ( !$errorList && $fileExtension != "jpg" && $fileExtension != "JPG" )
{
$errorList .= "<br /> format is not supported";
}
$photoSize = $_FILES["image"]['size']; // size of uploaded file
if ( !$errorList && $photoSize == 0 )
{
$errorList = "<br /> wrong data transfer";
}
if ( !$errorList && $photoSize > $maxSize*1024)
{
$errorList = "<br>image is to large";
}
if (!$errorList)
{
$tempFileName = $_FILES['image']['tmp_name']; // temporary file at server side
$tempFile = fopen( $tempFileName, "r" );
$binaryPhoto = fread( $tempFile, filesize( $tempFileName ) );
$src_img = imagecreatefromstring( $binaryPhoto ); // try to create image
if ( !$src_img )
{
$errorList = "<br>$ wrong image format";;
exit( -1 );
}
$width = imagesx( $src_img ); // get original source image width
$height = imagesy( $src_img ); // and height
$yCrop = 0;
$xCrop = 0;
$in_prop = $width / $height;
$out_prop = $newImageWidth / $newImageHeight;
//resize main image, crop if nessery
$image = imagecreatetruecolor( $newImageWidth, $newImageHeight );
if ( $width > $height )
{
if ( $in_prop > $out_prop ) $xCrop = round( (($width - ( $height* $out_prop))/2) );
else $yCrop = round( (($height - ($width/$out_prop))/2));
}
else
{
$yCrop = round( (($height - ($width/$out_prop))/2));
}
imagecopyresampled( $image, $src_img, 0, 0, $xCrop, $yCrop,$newImageWidth, $newImageHeight,($width-$xCrop*2), ($height-$yCrop*2) );
$textColor= imagecolorallocate($image,255,0,0);
$font = "./VeraSe.ttf";
$font_size = 40;
$angle = 125;
$x = 140;
$y = 130;
imagettftext( $image, $font_size, $angle, $x, $y, $textColor, $font, 'TEST');
imagegammacorrect ( $image, 1.0, 1.15 );
if( imagejpeg( $image, '../user/test.JPG' )) $errorList = "Picture has been successfully uploaded!\n<br>\n";
}
else header("Location: form8.php?errorList=$errorList");
}
else header("Location: form8.php"); /* Redirect browser */
?>
<html>
<body>
<?php print $errorList ?>
<img src="../user/test.JPG" />
</body>
</html>