Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取上载文件的内容,并将内容放入具有现有格式的表中_Php_Post_File Get Contents - Fatal编程技术网

Php 获取上载文件的内容,并将内容放入具有现有格式的表中

Php 获取上载文件的内容,并将内容放入具有现有格式的表中,php,post,file-get-contents,Php,Post,File Get Contents,我这里有一段代码,用于处理表单的上传,但我想更进一步,获取上传文件(.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"][

我这里有一段代码,用于处理表单的上传,但我想更进一步,获取上传文件(.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;

?>

最好的,
安东

使用此代码。我通常使用这个代码。看,如果你想知道分机号码。然后,我给你们的一个函数,写下所有可能的扩展。有一种方法可以获得扩展,您也可以使用这种方法。。但是,这就是我上传文件的方式。希望我理解你的问题

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;
?>


这是我上传后收到的消息。“Test.txt上传完成警告:文件获取内容(Test.txt):无法打开流:第18行的/home/toneking1989/everestt.co/action.php中没有此类文件或目录”您正在使用的操作系统?windows 7。文件可以正常上传。我只想抓取文件的内容并将其放在一个表中或在网页上回显。这显然不起作用,但它让我思考,所以我更改了$str=file_get_content(“uplaods/$filename”);它从我的上传目录中抓取它。虽然我可能没有正确使用$\u SERVER['DOCUMENT\u ROOT'],但谢谢。在最后一行的第二秒出现错误。出于某种原因,它在这个目录中查找,/home/toneking1989/everestt.co/action.php?而不是在我的Web服务器上的uploads目录中。不过上传到没有问题。下面是错误警告:file_get_contents(Test.txt):无法打开流:第29行的/home/toneking1989/everestt.co/action.php中没有这样的文件或目录。这可能是路径问题,php需要完整路径与相对路径。因此,您通常可以使用类似于
$\u SERVER['DOCUMENT\u ROOT']的东西上传/'以确保您始终在同一位置进行书写和阅读。虽然此代码可以回答此问题,但提供有关此代码为什么和/或如何回答此问题的附加上下文可提高其长期价值。@JAL此代码我从1年以来一直在使用。所以,这段代码在各个方面都帮助了我。现在是用户视图。。他们如何使用。谢谢你的帮助。我会以另一种方式使用它,但疯狂给了我现在需要的答案。最佳安东酒店