Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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_File_Operation - Fatal编程技术网

Php 对文件的操作

Php 对文件的操作,php,file,operation,Php,File,Operation,我有一个程序可以把文件上传到目录。现在,我需要构建一个程序,根据文件的内容以及$thefile、$thefile\u name、$thefile\u size、$thefile\u type来识别当前目录中的文件,并以图标、大小和名称的形式显示其类型的表。还添加了下载、删除..选项 这是我到目前为止的代码 第一个地点: <html> <body> <form action="plikownia.php" method="post" enctype="mult

我有一个程序可以把文件上传到目录。现在,我需要构建一个程序,根据文件的内容以及$thefile、$thefile\u name、$thefile\u size、$thefile\u type来识别当前目录中的文件,并以图标、大小和名称的形式显示其类型的表。还添加了下载、删除..选项

这是我到目前为止的代码

第一个地点:

<html>
 <body>

 <form action="plikownia.php" method="post"
 enctype="multipart/form-data">
 <label for="file">Filename:</label>
 <input type="file" name="file" id="file"><br>
 <input type="submit" name="submit" value="Submit">
 </form>

 </body>
 </html>

文件名:

第二点:

<?php
 $allowedExts = array("gif", "jpeg", "jpg", "png");

 $tmp = explode(".", $_FILES["file"]["name"]);

 $extension = end($tmp);
 if ((($_FILES["file"]["type"] == "image/gif")
 || ($_FILES["file"]["type"] == "image/jpeg")
 || ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
 || ($_FILES["file"]["type"] == "image/png"))
 && ($_FILES["file"]["size"] < 200000000)
 && in_array($extension, $allowedExts))
   {
   if ($_FILES["file"]["error"] > 0)
     {
     echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
     }
   else
     {
     echo "Upload: " . $_FILES["file"]["name"] . "<br>";
     echo "Type: " . $_FILES["file"]["type"] . "<br>";
     echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
     echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

     if (file_exists("C:\Users\Adam\Desktop\upload" . $_FILES["file"]["name"]))
       {
       echo $_FILES["file"]["name"] . " already exists. ";
       }
     else
       {
       move_uploaded_file($_FILES["file"]["tmp_name"],
       "C:\Users\Adam\Desktop\upload" . $_FILES["file"]["name"]);
       echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
       }
     }
   }
 else
   {
   echo "Invalid file";
   }
 ?>

好的解决方案是将文件信息保存在数据库中。您应该保存详细信息,如id、文件名、文件大小、文件路径、扩展名、mime

此外,最好将文件保存在磁盘上,并使用uniqid()+扩展名命名它们。要获取文件类型,最好使用
mime\u content\u type()
扩展名可以更改。您还可以使用
pathinfo()
获取有关文件扩展名、基本名称、文件名等的更多信息

例如:

<?php
echo mime_content_type('php.gif') . "\n";
echo mime_content_type('test.php');
?>
使用数据库时,可以提供以下链接:

只需从db中选择此文件,获取其位置并使用这部分代码:

header("Content-type: $mime"); // the mime from your db should be in $mime
header('Content-Disposition: attachment; filename="$pathtofile"'); // $pathtofile from your db
readfile('$pathtofile');

使用
mime\u content\u type()
我以前没有在数据库上工作过:(我的项目需要这个:),所以我必须将文件放入文件夹并从中下载;]Panie Adamie:)这是一个糟糕的解决方案,但您需要列出文件,例如使用DirectoryIterator,并且您需要在链接中提供文件名。要下载文件,您可以使用我在回答中提供的带有“标题”的代码。
header("Content-type: $mime"); // the mime from your db should be in $mime
header('Content-Disposition: attachment; filename="$pathtofile"'); // $pathtofile from your db
readfile('$pathtofile');