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

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 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率