У меня есть этот код здесь, который обрабатывает загрузку для моей формы, но я хочу сделать еще один шаг и захватить содержимое загруженного файла (.doc, docx,.txt) и разместить содержимое по строкам в таблицу с существующий формат или просто откликнуться на странице, как просмотр.
<?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;
?>
Лучший, Antone
Откройте и прочитайте файл строки за строкой, когда он находится в этом месте назначения, например:
<?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;
?>
Используйте этот код. Обычно я использую этот код. См., Если вы хотите узнать расширение. Затем одна функция, которую я дал вам, напишите там все возможные расширения. Существует способ получить расширение, вы также можете использовать этот способ.. но, это способ, как я использую для загрузки файла. Надеюсь, я понял ваш вопрос.
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..]
}
}