尝试构建一个多图像上传,从我的表单中获取 3 个提交的文件并将它们存储在我的服务器上。
我有以下内容,但无论我收到什么“无效文件”,任何人都可以看到我哪里出错了吗?
for($i = 0; $i < 3; $i++) {
$aFile = $_FILES['file'][$i];
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $aFile["file"]["name"]));
if ((($aFile["file"]["type"] == "image/gif")
|| ($aFile["file"]["type"] == "image/jpeg")
|| ($aFile["file"]["type"] == "image/png")
|| ($aFile["file"]["type"] == "image/pjpeg"))
&& ($aFile["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($aFile["file"]["error"] > 0)
{
echo "Return Code: " .$aFile["file"]["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $aFile["file"]["name"]))
{
echo $aFile["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($aFile['tmp_name'],
"upload/" . date('U')."-".$aFile["file"]["name"]); // add a unique string to the uploaded filename so that it is unique.
echo "Image Uploaded Successfully";
}
}
}
else
{
echo "Invalid file";
}
}
** HTML **
<li>
<label for="picture_1">picture 1 : </label>
<input type="file" name="file">
</li>
<li>
<label for="picture_2">picture 2 : </label>
<input type="file" name="picture_2">
</li>
<li>
<label for="picture_3">picture 3 : </label>
<input type="file" name="picture_3">
</li>
最佳答案
我猜,主要问题是您的 HTML 元素名称和 $_FILES 中使用的名称无法匹配,即您使用了第一个文件输入名称"file"。它应该是“picture_1”。并且您在文件处理部分使用了索引 0 到 2。匹配“picture_1”、“picture_2”和“picture_3”应该是1到3。
请注意,您的表单应包含 enctype="multipart/form-data" ,否则您的文件将不会被上传。这是正确的:
有两种实现方法:
(1)分别命名输入的文件如picture_1、picture_2等
(2) 将文件输入命名为一个组,例如 file[] .
方法 1:分别命名文件输入
HTML
<form method="post" enctype="multipart/form-data">
<li>
<label for="picture_1">picture 1 : </label>
<input type="file" name="picture_1" id="picture_1" />
</li>
<li>
<label for="picture_2">picture 2 : </label>
<input type="file" name="picture_2" id="picture_2" />
</li>
<li>
<label for="picture_3">picture 3 : </label>
<input type="file" name="picture_3" id="picture_3" />
</li>
<input type="submit" name="submit" value="Upload" />
</form>
PHP
if(sizeof($_FILES)){
for($i = 1; $i <= 3; $i++) {
$aFile = $_FILES['picture_'.$i];
if(empty($aFile['tmp_name'])) continue; # skip for empty elements
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $aFile["name"]));
if ((($aFile["type"] == "image/gif")
|| ($aFile["type"] == "image/jpeg")
|| ($aFile["type"] == "image/png")
|| ($aFile["type"] == "image/pjpeg"))
&& ($aFile["size"] < 20000)
&& in_array(strtolower($extension), $allowedExts))
{
if ($aFile["error"] > 0)
{
echo "Return Code: " .$aFile["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $aFile["name"]))
{
echo $aFile["name"] . " already exists. ";
}
else
{
move_uploaded_file($aFile['tmp_name'],
"upload/" . date('U')."-".$aFile["name"]);
echo "Image Uploaded Successfully";
}
}
}
else
{
echo "Invalid file";
}
}
}
方法 2:将文件输入命名为一个组
HTML
<form method="post" enctype="multipart/form-data">
<li>
<label for="picture_1">picture 1 : </label>
<input type="file" name="file[]" id="picture_1" />
</li>
<li>
<label for="picture_2">picture 2 : </label>
<input type="file" name="file[]" id="picture_2" />
</li>
<li>
<label for="picture_3">picture 3 : </label>
<input type="file" name="file[]" id="picture_3" />
</li>
<input type="submit" name="submit" value="Upload" />
</form>
PHP
if(sizeof($_FILES)){
for($i = 0; $i < 3; $i++) {
$name = $_FILES['file']['name'][$i];
$type = $_FILES['file']['type'][$i];
$tmp_name = $_FILES['file']['tmp_name'][$i];
$error = $_FILES['file']['error'][$i];
$size = $_FILES['file']['size'][$i];
if(empty($name)) continue; # skip for empty element
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $name));
if (( ($type == "image/gif")
|| ($type == "image/jpeg")
|| ($type == "image/png")
|| ($type == "image/pjpeg"))
&& $size < 20000
&& in_array(strtolower($extension), $allowedExts) )
{
if ($error > 0)
{
echo "Return Code: " .$error . "<br>";
}
else
{
if (file_exists("upload/" . $name))
{
echo $aFile["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($tmp_name,
"upload/" . date('U')."-".$name);
echo "Image Uploaded Successfully";
}
}
}
else
{
echo "Invalid file";
}
}
}
学分:
<label for="some_id"> , 你可以
在各自的 HTML 元素中具有相同的 ID 属性,例如 <input
type="file" name="..." id="some_id" /> .点击标签时,会触发元素的onclick事件。关于php - 多张图片上传不移动目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13937694/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta