最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting the file path of an uploaded file - Stack Overflow

programmeradmin2浏览0评论

I am currently doing a PHP project that requires me to make a logs of all the imported excels in the database.

I was able to get the tmp_name from the $_FILES global variable but not able to get the exact file path.

Here is my code snippet.

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

<?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. Thanks

I am currently doing a PHP project that requires me to make a logs of all the imported excels in the database.

I was able to get the tmp_name from the $_FILES global variable but not able to get the exact file path.

Here is my code snippet.

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

<?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. Thanks

Share Improve this question edited Jul 3, 2018 at 6:35 Miggy asked Jul 3, 2018 at 6:25 MiggyMiggy 8167 silver badges16 bronze badges 1
  • 1 Use move_uploaded_file DOC – Empty Brain Commented Jul 3, 2018 at 6:34
Add a ment  | 

3 Answers 3

Reset to default 5

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. The path of the temporary location will be provided in tmp_name option in $_FILES array.

By using the move_uploaded_file function, this file will be moved from the temporary location to the location of your choice. 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.

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'];
$base=$_SERVER['DOCUMENT_ROOT']; $filename=$_FILES['file']['name']; $path=$base."/trial/Uploads/Original_folder/signature/".$filename.""; if(file_put_contents($path,$image)!=false) { echo $path; }

发布评论

评论列表(0)

  1. 暂无评论