我正在编写一个相当简单的文件服务器,并且在上传单个文件,将其移动到服务器上的文件夹并在数据库中保存信息方面做得很好。现在,当我尝试修改它以接受来自单个输入字段的多个文件时,我无法让它在第一次测试错误之后继续进行。
这是我的index.php:
<body>
<img src="style/images/sitename.gif" alt="sitename" align="absmiddle" class="displayed" />
<div id="sidediv">
<ul>
<li>Multiple files uploaded at once will return a link to a zip archive of those files.
</ul>
</div><!--close the sidediv-->
<div id="container">
<div id="content">
<!--form starts here-->
<form action="upload.php" id="group" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
<p id="f1_upload_process">Loading...<br/><img src="loader.gif" /><br/></p>
<p id="f1_upload_form" align="center"><br/>
<label>File:
<input name="myfile[]" type="file" size="30" multiple="multiple" />
</label>
<label>
<input type="submit" name="submitBtn" class="sbtn" value="Upload" multiple="multiple" />
</label>
</p>
<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
<!--form ends here-->
</div>
<!--<div id="footer"><a href="" target="_blank">sitename</a></div>-->
</div>
<div id="link"></div>
</body>
我的upload.php在这里:
<?php
//database
$username="";
$password="";
$database="";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$message = array();
$result = array();
$fileName = array();
$ext = array();
$tmpName = array();
$path = array();
$target_path = array();
$count = count($_FILES['myfile']['name']);
for($i=0;$i<$count;$i++)
{
//file info
$fileName[$count] = $_FILES['myfile']['name'][$count]; // Get the name of the file (including file extension).
$ext[$count] = pathinfo($fileName[$count], PATHINFO_EXTENSION); // Get the extension from the filename.
$tmpName[$count] = $_FILES['myfile']['tmp_name'][$count];
$fileSize[$count] = $_FILES['myfile']['size'][$count];
$fileType[$count] = $_FILES['myfile']['type'][$count];
//file info
/* $fileName = $myfile['name']; // Get the name of the file (including file extension).
$ext = pathinfo($fileName, PATHINFO_EXTENSION); // Get the extension from the filename.
$tmpName = $myfile['tmp_name'];
$fileSize = $myfile['size'];
$fileType = $myfile['type'];*/
// Edit upload location here
$destination_path = './files/';
$allowed_filetypes = array('idx','sub','txt','srt');
$max_filesize = 5242880; //bytes
$prefix = substr(md5(time()),0,7); //new name of the file
$target_path[$count] = $destination_path . $prefix .".".$ext[$count];
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext[$count],$allowed_filetypes)){
$result[$count] = 2;
$message[$count] = "The file you attempted to upload is not allowed.".$fileName[$count];}
// Now check the filesize, if it is too large then DIE and inform the user.
else if(filesize($_FILES['myfile']['tmp_name'][$count]) > $max_filesize){
$result[$count] = 3;
$message[$count] = "The file you attempted to upload is too large.";}
else if(!file_exists($destination_path)){
$result[$count] = 4;
$message[$count] = "The upload path does not exist";}
// Check if we can upload to the specified path, if not DIE and inform the user.
else if(!is_writable($destination_path)){
$result[$count] = 5;
$message[$count] = "You cannot upload to the specified directory, please CHMOD it to 777.";}
else
{
@move_uploaded_file($tmpName[$count], $target_path[$count]);
$file_info = pathinfo($fileName[$count]);
$sql = "INSERT INTO Files SET
uploader_ip = '".$_SERVER['REMOTE_ADDR']."',
File_Name = '".$fileName[$count]."',
File_Type = '".$fileType[$count]."',
File_Size = '".$fileSize[$count]."',
File_Hash = '".$prefix.".".$ext[$count]."',
File_Extension = '".$file_info['extension']."'";
$sqlresult = mysql_query($sql);
// If the query was successful, give success message
if(!$sqlresult){
$result[$count] = 6;
$message[$count] = "Could not add this file.";//not actually displayed
exit;
}
else{
$message[$count] = "New file successfully added.";//not actually displayed
$result[$count] = 1;
$path[$count] = 'Your file upload was successful, view the file <a href="' . $target_path[$count] . '" title="Your File">here</a>';
}
}//closes last else (all the writing to the db)
}
sleep(1);
?>
<script language="javascript" type="text/javascript">window.top.window.stopUpload(
<?php echo json_encode($result[$count]); ?>,
<?php echo json_encode($message[$count]); ?>,
<?php echo json_encode($path[$count]); ?>,
<?php echo json_encode($count); ?>,
<?php echo json_encode($fileName[$count]); ?>,
<?php echo json_encode($ext[$count]); ?>);
</script>
每当我收到错误“你上传的文件是不允许的”时,它应该通过该测试。非常感谢任何帮助。
答案 0 :(得分:0)
我认为您在$count
循环内的数组中使用for
的任何地方,都需要使用$i
代替$count
。
$count
始终相同(并且超出了数组的范围)。
试一试,看看你是否还有运气。