简体   繁体   中英

php&mysql fopen cannot open the file

I am using PHP & MySQL to generate a dynamic web page. Now I want to make the search result into a file. Firstly, I use

 $query = "select * from database into outfile 'query.txt'";@mysql($query);

BUt it cannot work; Then, I try to use the "fopen" function.

$fp=fopen("query.txt","w+") or exit("Unable to open file!");
if($result_specific){
while( $row = mysql_fetch_array( $result_specific,MYSQL_ASSOC )){
    echo fwrite($fp,$row["p1"]."\t".$row["p2"]."\t".$row["p3"]."\n");
}
}
fclose($fp);

Unfortunately, it tells me "Unable to open file!". Maybe it is a wrong Url? But I don't know how to specify the correct URL.

select * from database into outfile 'query.txt'

You haven't specified a path - only a filename. The file will be written to the current working directory of the MySQL instance. It will be written with the uid of the the user running the instance. It's impossible to say from the information you've provided what permissions the resultant file will have - on a Linux/BSD/Posix system the permissions will be based on he the umask inherited by the DBMS instance.

$fp=fopen("query.txt","w+")

Is you PHP looking in the right directory? What are the permissions on the file?

or exit("Unable to open file!");

That 'or' will not do what you think it does - should be '||'

if($result_specific){ ...

What has this got to do with the problem?

Go back and use full paths and check the permissions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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