我尝试将文件存储在服务器" public / image"文件夹,它存储文件。但当我尝试阅读或检查文件是否存在使用url :::" http://www.myapp.net/image/image_654391973.txt"它返回 不存在。并且图像文件夹和文件的所有权限都设置为读写(" 0777")。
这是我的测试代码:
<?php
$FileName="/home/a/public_html/image/"."image_".rand().".txt";
$binary="hello";
$Event_file = fopen($FileName, "wb");
fwrite($Event_file, $binary);
fclose($Event_file);
$Path=realpath($FileName);
//get image file
$get_path="http://www.myapp.net/image/image_654391973.txt";
echo $get_path ."<br>";
if(is_file($get_path))
{
echo "file exists on server";
}
else
{
echo "file not exists on server ";
}
?>
文件存储在文件夹中,但它返回&#34;文件不存在于服务器&#34;
答案 0 :(得分:0)
如果您要检查is_file,您会看到它将获得文件的路径,而不是文件的URL。
您需要相应地更改代码:
<?php
$FileName="/home/a/public_html/image/"."image_".rand().".txt";
$binary="hello";
$Event_file = fopen($FileName, "wb");
fwrite($Event_file, $binary);
fclose($Event_file);
$Path=realpath($FileName);
//get image file
$get_path="/home/a/public_html/image/image_654391973.txt";
echo $get_path ."<br>";
if(is_file($get_path))
{
echo "file exists on server";
}
else
{
echo "file not exists on server ";
}
?>
答案 1 :(得分:0)
http:
包装器不支持stat()
,因此is_file
无法判断该网址是否指向真实文件。尝试:
if ($temp = fopen($get_path, 'r')) {
close($temp);
echo "file exists on server";
} else {
echo "file not exists on server";
}