简体   繁体   English

获取上载文件的文件路径

[英]Getting the file path of an uploaded file

I am currently doing a PHP project that requires me to make a logs of all the imported excels in the database. 我目前正在做一个PHP项目,要求我在数据库中记录所有导入的Excel。

I was able to get the tmp_name from the $_FILES global variable but not able to get the exact file path. 我能够从$_FILES全局变量中获取tmp_name ,但无法获取确切的文件路径。

Here is my code snippet. 这是我的代码片段。

index.php 的index.php

<form role="form" method="post" action="post_data.php" enctype="multipart/form-data">
    <input type="file" name="file" required>
    <button>Submit</button>
</form>

post_data.php post_data.php

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        //var_dump($_FILES['file']);
        //var_dump($_FILES['file']['name']); #gets the file name
        var_dump($_FILES['file']['tmp_name']); #gets the temp file path and name
    }
?>

Any help would be much appreciated. 任何帮助将非常感激。 I can also work with javascript if there are any available solutions for this problem. 如果有任何可用的解决方案,我也可以使用javascript。 Thanks 谢谢

You'll not get the file path. 你不会得到文件路径。 The File Upload in PHP works such that when you upload a file, it'll be uploaded to a temporary location and then your form will be posted. PHP中的文件上传工作,当您上传文件时,它将被上传到临时位置,然后您的表单将被发布。 The path of the temporary location will be provided in tmp_name option in $_FILES array. 临时位置的路径将在$_FILES数组的tmp_name选项中提供。

By using the move_uploaded_file function, this file will be moved from the temporary location to the location of your choice. 通过使用move_uploaded_file函数,此文件将从临时位置移动到您选择的位置。 But you'll have to provide the location (including the filename) where you want to move the file from temporary location. 但是您必须提供要从临时位置移动文件的位置(包括文件名)。

So if you are looking for a path where you want to move the file from then it'll be present in tmp_name . 因此,如果您要查找要从中移动文件的路径,则它将出现在tmp_name

Hope this helps. 希望这可以帮助。

<?php
//variable containing path of Server's folder where you want to upload your file 
$path;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    //var_dump($_FILES['file']);
    //var_dump($_FILES['file']['name']); #gets the file name
    var_dump($_FILES['file']['tmp_name']); #gets the temp file path and name
    if (move_uploaded_file($_FILES['file']['tmp_name'], $path . DS . $_FILES['file']['name'])) {
        //file uploaded successfully
        //your file is uploaded at $path . DS . $_FILES['file']
   }
   else {
        //error in uploading file
  }
}
?>

$image=$_FILES['file']; $图像= $ _ FILES [ '文件'];
$base=$_SERVER['DOCUMENT_ROOT']; $基地= $ _ SERVER [ 'DOCUMENT_ROOT']; $filename=$_FILES['file']['name']; $文件名= $ _ FILES [ '文件'] [ '名称']; $path=$base."/trial/Uploads/Original_folder/signature/".$filename.""; $路径= $基 “/审判/上传/ Original_folder /签名/".$文件名。”“。 if(file_put_contents($path,$image)!=false) { echo $path; if(file_put_contents($ path,$ image)!= false){echo $ path; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM