我正在使用以下代码将txt文件上传到我的服务器。。
<form style="margin-bottom:2px;" method="post" enctype="multipart/form-data" name="formUploadFile">
<label>Select txt file to upload:</label>
<br>
<input type="file" name="files[]" /> <input type="submit" value="Upload" name="Submit"/>
<br>
</form>
<?php
if(isset($_POST["Submit"]))
{
$errors = array();
$uploadedFiles = array();
$extension = array("txt");
$bytes = 1024;
$KB = 1024;
$totalBytes = $bytes * $KB;
$UploadFolder = "tmp_txt_store";
$counter = 0;
foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name){
$temp = $_FILES["files"]["tmp_name"][$key];
$name = $_FILES["files"]["name"][$key];
if(empty($temp))
{
break;
}
$counter++;
$UploadOk = true;
if($_FILES["files"]["size"][$key] > $totalBytes)
{
$UploadOk = false;
array_push($errors, $name." file size is larger than the 1 MB.");
}
$ext = pathinfo($name, PATHINFO_EXTENSION);
if(in_array($ext, $extension) == false){
$UploadOk = false;
array_push($errors, $name." invalid file type.");
}
if(file_exists($UploadFolder."/".$name) == true){
$UploadOk = false;
array_push($errors, $name." file already exists.");
}
if($UploadOk == true){
$name = "my.txt";
move_uploaded_file($temp,$UploadFolder."/".$name);
array_push($uploadedFiles, $name);
}
}
if($counter>0){
if(count($errors)>0)
{
echo "<b>Errors:</b>";
foreach($errors as $error)
{
echo " ".$error.",";
}
echo "<br/>";
}
if(count($uploadedFiles)>0){
echo "<b>Uploaded:</b>";
echo "=";
foreach($uploadedFiles as $fileName)
{
echo " ".$fileName.",";
}
echo "<br/>";
echo "DONE!";
}
}
else{
echo "ERROR: Please press the browse button and select a txt file to upload.";
}
}
?>
而且我只想保留文本的前4行,因此在上载之前,请修剪(删除)所有文本行(在第4行之后)。
所以
line 1 text
line 2 text
line 3 text
line 4 text
line 5 text
line 6 text
line 7 text
<!--And so on-->
成为
line 1 text
line 2 text
line 3 text
line 4 text
我该如何去做?我已经尝试了一些建议来寻找其他(类似)问题的答案,但是作为php的新手,我真的对此一无所知。
您可以只读取前四行,然后覆盖文件。