我正在尝试实现一个JQUERY插件,它允许我将图像上传到指定的文件夹,但是我想将其基于选择框,例如,如果选择了选择值“one”,图像将上传到文件夹一。我试图使用switch语句和IF语句来使其正常运行但到目前为止没有运气。有人能指出我正确的方向吗?
非常感谢任何帮助,
梅丽莎
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
<select>
<option value="one">one</option>
<option value="two">two</option>
</select>
<input type="file" size="60" name="myfile">
<input type="submit" value="Ajax File Upload">
</form>
<div id="progress">
<div id="bar"></div>
<div id="percent">0%</div >
</div>
<br/>
<div id="message"></div>
<script>
$(document).ready(function()
{
var options = {
beforeSend: function()
{
$("#progress").show();
//clear everything
$("#bar").width('0%');
$("#message").html("");
$("#percent").html("0%");
},
uploadProgress: function(event, position, total, percentComplete)
{
$("#bar").width(percentComplete+'%');
$("#percent").html(percentComplete+'%');
},
success: function()
{
$("#bar").width('100%');
$("#percent").html('100%');
},
complete: function(response)
{
$("#message").html("<font color='green'>"+response.responseText+"</font>");
},
error: function()
{
$("#message").html("<font color='red'> ERROR: unable to upload files</font>");
}
};
$("#myForm").ajaxForm(options);
});
</script>
Php如下;
<?php
$output_dir = "uploads/uploads";
if(isset($_FILES["myfile"]))
{
//Filter the file types , if you want.
if ($_FILES["myfile"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
//move the uploaded file to uploads folder;
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $_FILES["myfile"]["name"]);
echo "Uploaded File :".$_FILES["myfile"]["name"];
}
}
?>
答案 0 :(得分:1)
选择一个名称或ID,例如
<select name='folder'>
然后在你的PHP页面中,你将把它作为POST数据的一部分。检查它是否是允许的值,否则设置一个安全的默认值。
if($_POST['folder']=='one')
{
$output_dir='one';
}
elseif($_POST['folder'])=='two')
{
$output_dir='two';
}
else
{
$output_dir='default';
}
然后,
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.'/'.$_FILES["myfile"]["name"]);