获取上传文件的内容并将内容放入具有现有格式的 table
Get content of an Uploaded File and place content into a table with existing format
我这里有这段代码可以处理我的表单上传,但我想更进一步,获取上传文件(.doc、docx、.txt)的内容并将内容行放在使用现有格式插入 table 或像预览一样在页面上回显。
<?php
$fileName = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true
if (!$fileTmpLoc) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
}
if(move_uploaded_file($fileTmpLoc, "uploads/$fileName")){
echo "$fileName upload is complete";
} else {
echo "move_uploaded_file function failed";
}
// store file content as a string in $str
$str = file_get_contents($_FILES["file1"]["name"]);
echo $str;
?>
最好,
安东尼
使用此代码。我通常使用此代码。看看,如果你想知道扩展名。然后,我给了你一个功能,在那里写下所有可能的扩展。有一种获取扩展名的方法,您也可以使用这种方法。但是,这就是我用来上传文件的方法。希望我理解你的问题。
function GetPropertyImageExtension($imagetype)
{
if(empty($imagetype)) return false;
switch($imagetype)
{
case 'image/bmp': return '.bmp';
case 'image/gif': return '.gif';
case 'image/jpeg': return '.jpg';
case 'image/png': return '.png';
default: return false;
}
}
if (!empty($_FILES['CustomImage']["name"]))
{
$file_name=$_FILES['CustomImage']["name"];
$temp_name=$_FILES['CustomImage']["tmp_name"];
$imgtype=$_FILES['CustomImage']["type"];
$ext= GetPropertyImageExtension($imgtype);
$imagename=date("d-m-Y")."-".time().$ext;
$target_path = "../Custom Cake Images/".$imagename;
$Rtarget_path = "Custom Cake Images/".$imagename;
if(move_uploaded_file($_FILES['CustomImage']['tmp_name'], $Rtarget_path ))
{
[..SQL Query..]
}
}
打开文件并在文件到达目的地后逐行读取,如下所示:
<?php
$fileName = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true
if (!$fileTmpLoc) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
}
if(move_uploaded_file($fileTmpLoc, "uploads/$fileName")){
echo "$fileName upload is complete";
$handle = fopen("uploads/$fileName", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
}
fclose($handle);
} else {
// error opening the file.
}
} else {
echo "move_uploaded_file function failed";
}
// store file content as a string in $str
$str = file_get_contents($_FILES["file1"]["name"]);
echo $str;
?>
我这里有这段代码可以处理我的表单上传,但我想更进一步,获取上传文件(.doc、docx、.txt)的内容并将内容行放在使用现有格式插入 table 或像预览一样在页面上回显。
<?php
$fileName = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true
if (!$fileTmpLoc) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
}
if(move_uploaded_file($fileTmpLoc, "uploads/$fileName")){
echo "$fileName upload is complete";
} else {
echo "move_uploaded_file function failed";
}
// store file content as a string in $str
$str = file_get_contents($_FILES["file1"]["name"]);
echo $str;
?>
最好, 安东尼
使用此代码。我通常使用此代码。看看,如果你想知道扩展名。然后,我给了你一个功能,在那里写下所有可能的扩展。有一种获取扩展名的方法,您也可以使用这种方法。但是,这就是我用来上传文件的方法。希望我理解你的问题。
function GetPropertyImageExtension($imagetype)
{
if(empty($imagetype)) return false;
switch($imagetype)
{
case 'image/bmp': return '.bmp';
case 'image/gif': return '.gif';
case 'image/jpeg': return '.jpg';
case 'image/png': return '.png';
default: return false;
}
}
if (!empty($_FILES['CustomImage']["name"]))
{
$file_name=$_FILES['CustomImage']["name"];
$temp_name=$_FILES['CustomImage']["tmp_name"];
$imgtype=$_FILES['CustomImage']["type"];
$ext= GetPropertyImageExtension($imgtype);
$imagename=date("d-m-Y")."-".time().$ext;
$target_path = "../Custom Cake Images/".$imagename;
$Rtarget_path = "Custom Cake Images/".$imagename;
if(move_uploaded_file($_FILES['CustomImage']['tmp_name'], $Rtarget_path ))
{
[..SQL Query..]
}
}
打开文件并在文件到达目的地后逐行读取,如下所示:
<?php
$fileName = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true
if (!$fileTmpLoc) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
}
if(move_uploaded_file($fileTmpLoc, "uploads/$fileName")){
echo "$fileName upload is complete";
$handle = fopen("uploads/$fileName", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
}
fclose($handle);
} else {
// error opening the file.
}
} else {
echo "move_uploaded_file function failed";
}
// store file content as a string in $str
$str = file_get_contents($_FILES["file1"]["name"]);
echo $str;
?>