du8791069 2019-02-19 23:30
浏览 237

PHP - 获取文件/文件夹相对路径

With JavaScript, if you have the input tag set up like this...

<input type="file" multiple webkitdirectory directory />

You able to get the relative path of a selected file by doing this...

document.getElementsByTagName('INPUT')[0].files[0].webkitRelativePath

My question is how do you get the relative path of a file from a input tag just like this with PHP? With PHP your able to get a file name, type, size but how do you get its relative path?

  • 写回答

2条回答 默认 最新

  • douchao0358 2019-02-19 23:52
    关注

    Relative directories don't work the way you think they should in PHP (according to your JavaScript way of thinking) -- Mainly because PHP is server side and theoreticaly can access any file within the operating system (unless jailed etc) -- The closest you're going to find is something like $_SERVER['DOCUMENT_ROOT'] which will give you the relative path to the web root -- However if you are running PHP client based -- There is no "relative root" -- In cli, you simply use ./ (or nothing), ../, / to tell php "related to the same directory as the script" IE

    ./directory/yourfile.txt   //This starts in the SAME directory as the php file
    
    ../directory/yourfile.txt  // This goes up one directory, and then you can drill back down.
    
    ../../../directory/directory/directory/file.txt  // Just like above, but up 3 directories
    
    /directory.file.txt // Starts from the server root -- IE /var/www/etc/etc/etc
    

    Other than using these methods you can write your own way of handling this with your own methods etc -- There are many schools of though on this as well. Some say using absolute paths is the way to go ... But others argue that for portability relative is the way. Really it depends on your program and what you're using it for .. Whether it's getting deployed etc .. If it's the latter, and it's web-based, $_SERVER['DOCUMENT_ROOT'] will suffice, but you should structure your program to make classes, controllers, modules etc etc easily accessible when coding. (hence write methods to handle directory locations)


    UPDATE per your comment

    You asked if it was possible to pass this to php, and the answer is - yes, and no.

    Yes, if the page has already loaded and you want to send the info as part of an action (form submit / AJAX call)

    Yes, if you already have loaded the page, and have stored a cookie with this information on a previous page / session

    No if a user has never been to the page and first pageload has already happened, and no other actions are expected to happen such as AJAX or form submissions.

    Based on what you're asking you should be able to simply:

    var mypath = document.getElementsByTagName('INPUT')[0].files[0].webkitRelativePath
    
     $.post("send_path_to_php.php", {path: mypath}, function(result){
         alert(result);  // result of php page response
     });
    

    This will send the var mypath to a php file called send_path_to_php.php. The php file will be able to extract $_POST['path] Then you do whatever you want in said php -- Whatever you echo/return will be in the callback of result

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog