- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个网站,该网站将向夜间狂欢者展示大城市夜总会场所和事件的列表。我正在尝试构建一个后端页面,管理员可以在其中添加俱乐部并输入信息,例如机构名称、位置、相对价格等,当然还有俱乐部的一些图片。
每个俱乐部都必须至少有一张图片,即主图片。可以有额外的 5,但这些是可选的,即。数据库字段可以为空。
我有一个看起来像这样的表单:
Establishment Main Photo
TextField: name = "establishment_image"
Establishment Photo 1
TextField: name = "establishment_image"
Establishment Photo 2
TextField: name = "establishment_image"
Establishment Photo 3
TextField: name = "establishment_image"
Establishment Photo 4
TextField: name = "establishment_image"
Establishment Photo 5
TextField: name = "establishment_image"
它有点像雅虎邮件的附加文件形式。如您所见,文本字段目前具有相同的文本字段名称。
我需要知道我需要对我的提交表单进行哪些更改才能:例如;
放一张主图,其他文本框留空,放一张主图,图 1 和图 2,其他文本字段留空,放置一张主要图片,图片 1、2 和 3,并将其他文本字段留空等。
我的 establishment_submit.php 看起来像这样:
<?php require_once('../Connections/connections.php'); ?>
<?php //maintain the session
if (!isset($_SESSION))
{
session_start();
}
?>
<?php
//retrieve data from Query String
$establishment_name= $_POST['establishment_name'];
$short_description= $_POST['short_description'];
$long_description= $_POST['long_description'];
$location= $_POST['location'];
$url_link= $_POST['url_link'];
$establishment_address= $_POST['establishment_address'];
$establishment_pricing= $_POST['establishment_pricing'];
$establishment_telephone= $_POST['establishment_telephone'];
$establishment_contact= $_POST['establishment_contact'];
$establishment_email= $_POST['establishment_email'];
$establishment_image = $_FILES['establishment_image']['name'];
//escape User Input to help prevent SQL Injection
$establishment_name= mysql_real_escape_string($establishment_name);
$short_description= mysql_real_escape_string($short_description);
$long_description = mysql_real_escape_string($long_description);
$location= mysql_real_escape_string($location);
$url_link= mysql_real_escape_string($url_link);
$establishment_address= mysql_real_escape_string($establishment_address);
$establishment_pricing= mysql_real_escape_string($establishment_pricing);
$establishment_telephone= mysql_real_escape_string($establishment_telephone);
$establishment_contact= mysql_real_escape_string($establishment_contact);
$establishment_email= mysql_real_escape_string($establishment_email);
$establishment_image= mysql_real_escape_string($establishment_image);
//redirect when successful
$establishmentAddSuccess = "establishment_add_success.php";
?>
<?php
//define a maximum size for the uploaded images
define ("MAX_SIZE","10000");
// note that these dimensions are considered the maximum and are not fixed
// because we have to keep the image ratio intact
//define a maximum size for the uploaded images
define ("LARGE_WIDTH","500");
define ("LARGE_HEIGHT","390");
define ("WIDTH","100"); //set here the width you want your thumbnail to be
define ("HEIGHT","100"); //set here the height you want your thumbnail to be.
// this is the function that will create the appropriately sized images from the upload
// the resize will be done considering the width and height defined, but without deforming the image
function make_largeimage($img_name,$filename,$new_large_w,$new_large_h)
{
//get image extension.
$ext=getExtension($img_name);
//creates the new image using the appropriate function from gd library
if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
$src_img=imagecreatefromjpeg($img_name);
if(!strcmp("png",$ext))
$src_img=imagecreatefrompng($img_name);
if(!strcmp("gif",$ext))
$src_img=imagecreatefromgif($img_name);
//gets the dimensions of the image
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
// next we will calculate the new dimensions for the large image
// the next steps will be taken:
// 1. calculate the ratio by dividing the old dimensions with the new ones
// 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable
// and the height will be calculated so the image ratio will not change
// 3. otherwise we will use the height ratio for the image
// as a result, only one of the dimensions will be from the fixed ones
$ratio1_large=$old_x/$new_large_w;
$ratio2_large=$old_y/$new_large_h;
if($ratio1_large>$ratio2_large)
{
$large_w=$new_large_w;
$large_h=$old_y/$ratio1_large;
}else
{
$large_h=$new_large_h;
$large_w=$old_x/$ratio2_large;
}
// we create a new image with the new dimensions
$dst_large_img=ImageCreateTrueColor($large_w,$large_h);
// resize the big image to the newly created one
imagecopyresampled($dst_large_img,$src_img,0,0,0,0,$large_w,$large_h,$old_x,$old_y);
// output the created image to the file. Now we will have the image into the file named by $filename
if(!strcmp("png",$ext))
imagepng($dst_large_img,$filename);
else
imagejpeg($dst_large_img,$filename);
if (!strcmp("gif",$ext))
imagegif($$dst_large_img,$filename);
//destroys source and destination images.
imagedestroy($dst_large_img);
imagedestroy($src_img);
}
function make_thumb($img_name,$filename,$new_w,$new_h)
{
//get image extension.
$ext=getExtension($img_name);
//creates the new image using the appropriate function from gd library
if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
$src_img=imagecreatefromjpeg($img_name);
if(!strcmp("png",$ext))
$src_img=imagecreatefrompng($img_name);
if(!strcmp("gif",$ext))
$src_img=imagecreatefromgif($img_name);
//gets the dimmensions of the image
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
// next we will calculate the new dimensions for the thumbnail image
// the next steps will be taken:
// 1. calculate the ratio by dividing the old dimensions with the new ones
// 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable
// and the height will be calculated so the image ratio will not change
// 3. otherwise we will use the height ratio for the image
// as a result, only one of the dimensions will be from the fixed ones
$ratio1=$old_x/$new_w;
$ratio2=$old_y/$new_h;
if($ratio1>$ratio2)
{
$thumb_w=$new_w;
$thumb_h=$old_y/$ratio1;
}else
{
$thumb_h=$new_h;
$thumb_w=$old_x/$ratio2;
}
// we create a new image with the new dimensions
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
// resize the big image to the newly created one
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
// output the created image to the file. Now we will have the thumbnail into the file named by $filename
if(!strcmp("png",$ext))
imagepng($dst_img,$filename);
else
imagejpeg($dst_img,$filename);
if (!strcmp("gif",$ext))
imagegif($dst_img,$filename);
//destroys source and destination images.
imagedestroy($dst_img);
imagedestroy($src_img);
}
// This function reads the extension of the file.
// It is used to determine if the file is an image by checking the extension.
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
// This variable is used as a flag. The value is initialized with 0 (meaning no error found)
//and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.
$errors=0;
// if it is not empty
for($i=0;$i<count($_FILES['establishment_image']["name"]);$i++)
{
// get the original name of the file from the clients machine
$filenames = stripslashes($_FILES['establishment_image']['name'][$i]);
// get the extension of the file in a lower case format
$extensions = getExtension($filenames);
$extensions = strtolower($extensions);
// if it is not a known extension, we will suppose it is an error, print an error message
//and will not upload the file, otherwise we continue
if (($extensions != "jpg") && ($extensions != "jpeg") && ($extensions != "png") && ($extensions != "gif"))
{
$warning = ("File extension of an image(s) not allowed");
header("location:establishment_add.php?warning=$warning");
$errors=1;
exit();
}
else
{
// get the size of the image in bytes
// $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which the uploaded file was stored on the server
$size=getimagesize($_FILES['establishment_image']['tmp_name'][$i]);
$sizekb=filesize($_FILES['establishment_image']['tmp_name'][$i]);
//compare the size with the maxim size we defined and print error if bigger
if ($sizekb > MAX_SIZE*1024)
{
$warning = ("Images have exceeded the size limit of 10MB");
header("location:establishment_add.php?warning=$warning");
$errors=1;
exit();
}
$rand= rand(0, 100000);
//we will give an unique name, for example a random number
$image_name=$rand.'.'.$extension;
//the new name will be containing the full path where the image will be stored (images folder)
$consname="C:/wamp/www/NNL/Administrator/Establishment_Images/".$image_name; //change the image/ section to where you would like the original image to be stored
$consname2="C:/wamp/www/NNL/Administrator/Establishment_Images/Thumbs/".$image_name;
//change the image/thumb to where you would like to store the new created thumbnail of the image
$copied = copy($_FILES['establishment_image']['tmp_name'][$i], $consname);
$copied = copy($_FILES['establishment_image']['tmp_name'][$i], $consname2);
//localhost calling of images
$img_large="../Establishment_Images/".$image_name; //change the image/ section to where you would like the original image to be stored
$img_thumb="../Establishment_Images/Thumbs/".$image_name;
//we verify if the image has been uploaded, and print error instead
if (!$copied) {
$warning = ("Unable to upload image file");
header("location:establishment_add.php?warning=$warning");
$errors=1;
exit();
}else{
// the new large image will be placed in Images/ folder
$imagelarge_name=$consname ;
// call the function that will create the thumbnail. The function will get as parameters
// the image name, the thumbnail name and the width and height desired for the thumbnail
$imagelarge=make_largeimage($consname,$imagelarge_name,LARGE_WIDTH,LARGE_HEIGHT);
// the new thumbnail image will be placed in Images/Thumbs/ folder
$thumb_name=$consname2 ;
// call the function that will create the thumbnail. The function will get as parameters
// the image name, the thumbnail name and the width and height desired for the thumbnail
$thumb=make_thumb($consname,$thumb_name,WIDTH,HEIGHT);
}
}
}
?>
<?php
//If no errors registered, redirect page
if(isset($_POST['Submit']) && !$errors)
{
//insert into database
$query2 = "INSERT INTO establishment(establishment_name,
establishment_short_description,
establishment_long_description,
establishment_address,
establishment_telephone,
establishment_contact,
establishment_email,
location_id,
establishment_pricing,
establishment_url_link,
establishment_mainphoto_url,
establishment_thumb_url)
VALUES
('$establishment_name',
'$short_description',
'$long_description',
'$establishment_address',
'$establishment_telephone',
'$establishment_contact',
'$establishment_email',
'$location',
'$establishment_pricing',
'$url_link',
'$img_large[0]',
'$img_thumb[0]')";
//Execute query
$qry_result2 = mysql_query($query2) or die(mysql_error());
header("Location: " . $establishmentAddSuccess);
}
else
{
$establishment_msg = ("Unable to add establishment");
header("location:establishment_add.php?establishment_msg=$establishment_msg");
exit();
}
?>
这对于单张图片上传效果很好,但现在不起作用。我知道我需要从行中进行更改;
for($i=0;$i<count($_FILES['establishment_image']["name"]);$i++)
{
如何使此表单能够上传多张图片。我将不胜感激任何帮助。
最佳答案
您可以像在常规表单字段中一样使用 PHP 的数组表示法:
Pic 1: <input type="file" name="establishment_image[]" />
Pic 2: <input type="file" name="establishment_image[]" />
但是,PHP 中的文件处理方式与服务器端的处理方式略有不同:
$_FILES = array(
'establishment_image' => array(
'name' => array(
0 => 'name of Pic 1 file',
1 => 'name of Pic 2 file'
),
'error' => array(
0 => error code for pic1 upload,
1 => error code for pic2 upload
etc...
);
不过,它很容易处理:
foreach(array_keys($_FILES['establishment_image']['name']) as $idx) {
....
}
另一种选择是为每个文件输入一个唯一的名称并使用该服务器端。如果您在每个中硬编码一个数字“子键”:
<input type="file" name="establishment_image_1" />
<input type="file" name="establishment_image_2" />
然后你可以简单地做
for ($i = 1; $i <= 5; $i++) {
echo "Name of file is ", $_FILES["establishment_image_$i"]['name'];
...
}
关于PHP多图片文件上传存储到文件夹和数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4923866/
我在 JavaScript 文件中运行 PHP,例如...... var = '';). 我需要使用 JavaScript 来扫描字符串中的 PHP 定界符(打开和关闭 PHP 的 )。 我已经知道使
我希望能够做这样的事情: php --determine-oldest-supported-php-version test.php 并得到这个输出: 7.2 也就是说,php 二进制检查 test.
我正在开发一个目前不使用任何框架的大型 php 站点。我的大问题是,随着时间的推移慢慢尝试将框架融入应用程序是否可取,例如在创建的新部件和更新的旧部件中? 比如所有的页面都是直接通过url服务的,有几
下面是我的源代码,我想在同一页面顶部的另一个 php 脚本中使用位于底部 php 脚本的变量 $r1。我需要一个简单的解决方案来解决这个问题。我想在代码中存在的更新查询中使用该变量。 $name)
我正在制作一个网站,根据不同的情况进行大量 PHP 重定向。就像这样...... header("Location: somesite.com/redirectedpage.php"); 为了安全起见
我有一个旧网站,我的 php 标签从 因为短标签已经显示出安全问题,并且在未来的版本中将不被支持。 关于php - 如何避免在 php 文件中写入
我有一个用 PHP 编写的配置文件,如下所示, 所以我想用PHP开发一个接口(interface),它可以编辑文件值,如$WEBPATH , $ACCOUNTPATH和 const值(value)观
我试图制作一个登录页面来学习基本的PHP,首先我希望我的独立PHP文件存储HTML文件的输入(带有表单),但是当我按下按钮时(触发POST到PHP脚本) )我一直收到令人不愉快的错误。 我已经搜索了S
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: What is the max key size for an array in PHP? 正如标题所说,我想知道
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我在 MySQL 数据库中有一个表,其中存储餐厅在每个工作日和时段提供的菜单。 表结构如下: i_type i_name i_cost i_day i_start i_
我有两页。 test1.php 和 test2.php。 我想做的就是在 test1.php 上点击提交,并将 test2.php 显示在 div 中。这实际上工作正常,但我需要向 test2.php
我得到了这个代码。我想通过textarea更新mysql。我在textarea中回显我的MySQL,但我不知道如何更新它,我应该把所有东西都放进去吗,因为_GET模式没有给我任何东西,我也尝试_GET
首先,我是 php 的新手,所以我仍在努力学习。我在 Wordpress 上创建了一个表单,我想将值插入一个表(data_test 表,我已经管理了),然后从 data_test 表中获取所有列(id
我有以下函数可以清理用户或网址的输入: function SanitizeString($var) { $var=stripslashes($var); $va
我有一个 html 页面,它使用 php 文件查询数据库,然后让用户登录,否则拒绝访问。我遇到的问题是它只是重定向到 php 文件的 url,并且从不对发生的事情提供反馈。这是我第一次使用 html、
我有一个页面充满了指向 pdf 的链接,我想跟踪哪些链接被单击。我以为我可以做如下的事情,但遇到了问题: query($sql); if($result){
我正在使用 从外部文本文件加载 HTML/PHP 代码 $f = fopen($filename, "r"); while ($line = fgets($f, 4096)) { print $l
我是一名优秀的程序员,十分优秀!