我有以下代码用于上传图片,当我尝试上传jpg文件时,它失败了,但是当我尝试png文件时,它有效!可能是什么问题呢?非常感谢任何帮助! :)
<html>
<head>
<title>Banners</title>
</head>
<body>
<?php
isset($_REQUEST['action']) ? $action = $_REQUEST['action'] : $action = '';
if( $action == 'uploadfiles' ){
//define where the files will be uploaded
$upload_directory = 'uploads/banners/';
$x=0;
echo "</div>Uploaded Files:</div>";
foreach ( $_FILES['data']['name'] AS $key => $value ){
echo "<div>{$value}</div>";
//Move file to server directory
move_uploaded_file($_FILES["data"]["tmp_name"][$x], $upload_directory . $_FILES["data"]["name"][$x]);
$x++;
}
}
?>
<form enctype="multipart/form-data" action="#" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<div>Choose a file to upload:</div>
<div id="text">
<div ><input name="data[]" type="file" /></div>
<!-- This is where the new file field will appear -->
</div>
<input type="button" id="add-file-field" name="add" value="Add input field" />
<input type='hidden' name="action" value="uploadfiles" />
<input type="submit" value="Upload File" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
// This will add new input field
$("#add-file-field").click(function(){
$("#text").append("<div class='added-field'><input name='data[]' type='file' /><input type='button' class='remove-btn' value='Remove Field' /></div>");
});
// The live function binds elements which are added to the DOM at later time
// So the newly added field can be removed too
$(".remove-btn").live('click',function() {
$(this).parent().remove();
});
});
</script>
</body>
</html>
答案 0 :(得分:1)
此代码看起来很好,适用于我的机器。这可能是其他地方的问题。你确定你的档案小于100k吗?
答案 1 :(得分:0)
您的代码中没有按文件类型过滤的内容。但是,你完全没有错误处理,所以试试这个:
foreach ( $_FILES['data']['name'] AS $key => $value ){
if ($_FILES['data']['name']['error'][$key] === UPLOAD_ERR_OK) {
move_uploaded_file(...);
} else {
die("Got error code " . $_FILES['data']['name']['error'][$key] . ' on file ' . $value);
}
}
错误代码记录在案here。