Friday, 14 December 2018

Why can't write info into /tmp directory instead of /var/www/html with php?

LAMP installed on my local pc,as i know the string xxxx can be written into /tmp/test with below php function.

file_put_contents("/tmp/test","xxxx") 

Both ajax_get.html and ajax_get.php are in my local directory /var/www/html.

cat ajax_get.html

<form id="myForm">
    <input type="text" name="name">
    <input type="text" name="addr">
    <input type="button" value="submit" id="submit">
</form>
<p id="result"></p>
<script>
function show(){
    var formData = new FormData( document.querySelector("#myForm") );
    var query_str = "ajax_get.php?";
    for(var pair of formData.entries())
    {
        query_str += pair[0] + "=" + pair[1] + "&";
    }
    query_str = query_str.substr(0,query_str.length - 1);
    var obj = new XMLHttpRequest();
    obj.onreadystatechange = function(){
        if(obj.readyState == 4 &&  obj.status == 200){
            console.log(obj.responseText);
        }
    }
    obj.open("get",query_str);
    obj.send();
}

ob = document.getElementById("submit");
ob.addEventListener("click",show);
</script>

cat ajax_get.php

<?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    $str = implode(" ",$_GET);
    file_put_contents("/tmp/test",$str);
    print($str);
?>

Now type 127.0.0.1/ajax_get.html , and input test1 and test2 ,then click the submit button, the query_str passed into ajax_get.php,no error info shown at the webpage,why no string test1 test2 written into /tmp/test?
test1 test2 shown in the webpage of ajax_get.html's console,it means that info passed from ajax_get.html into ajax_get.php and parsed.

Why the command file_put_contents("/tmp/test",$str); in ajax_get.php can't work?
It is no use to replace file_put_contents with

$handle=fopen("/tmp/test","w");
fwrite($handle,$str);
fclose($handle);

Maybe it is a issue on directory permission,if i change below statement in ajax_get.php

    file_put_contents("/tmp/test",$str);

into

    file_put_contents("test",$str);

And run the previous process,ajax_get.php create a file in /var/www/html/test

cat /var/www/html/test
test1 test2

Show the permission for /tmp directory.

ls -al /tmp
total 76
drwxrwxrwt 16 root    root    12288 Dec 10 18:39 .
drwxr-xr-x 23 root    root     4096 Dec  1 08:03 ..

. is the current directory /tmp ,its permission is 777 (rwxrwxrwx), why can't write file into /tmp directory by php?



from Why can't write info into /tmp directory instead of /var/www/html with php?

No comments:

Post a Comment