php 上传图片,一直揭示Warning: move_uploaded_file(.upload/en.jpg): failed to open stream
程序员文章站
2024-02-05 20:31:28
...
php 上传图片,一直提示Warning: move_uploaded_file(.upload/en.jpg): failed to open stream
运行以下代码,浏览器提示
Upload: en.jpg
Type: image/jpeg
Size: 54.5830078125 Kb
Temp file: /tmp/phpk0nLl5
Warning: move_uploaded_file(.upload/en.jpg): failed to open stream: No such file or directory in /var/www/html/bbs/upload_file.php on line 24 Warning: move_uploaded_file(): Unable to move '/tmp/phpk0nLl5' to '.upload/en.jpg' in /var/www/html/bbs/upload_file.php on line 24 Stored in: upload/en.jpg
求教,该怎样解决???我的代码运行在linux上
upload_file.php:
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "
";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "
";
echo "Type: " . $_FILES["file"]["type"] . "
";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
------解决方案--------------------
目标目录不存在
请不要自行处理错误
贴出 print_r($_FILES["file"]); 的结果
------解决方案--------------------
$type=strrchr($_FILES["file"]["name"],"."); //获取扩展名
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . time().$type); //以当前时间戳命名
------解决方案--------------------
以时间戳明名是不错,但是有其他的好的方法吗
------解决方案--------------------
当然能,跟原来一样,还是在upload文件夹里面。
------解决方案--------------------
在文件名的前面加上了时间, 可以看看time()函数.
------解决方案--------------------
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . time().$type); //以当前时间戳命名
当你用这一行代码的时候,文件名加上时间戳.好好检查一遍吧.
------解决方案--------------------
如果出现错误,一步一步的来排查
运行以下代码,浏览器提示
Upload: en.jpg
Type: image/jpeg
Size: 54.5830078125 Kb
Temp file: /tmp/phpk0nLl5
Warning: move_uploaded_file(.upload/en.jpg): failed to open stream: No such file or directory in /var/www/html/bbs/upload_file.php on line 24 Warning: move_uploaded_file(): Unable to move '/tmp/phpk0nLl5' to '.upload/en.jpg' in /var/www/html/bbs/upload_file.php on line 24 Stored in: upload/en.jpg
求教,该怎样解决???我的代码运行在linux上
upload_file.php:
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "
";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "
";
echo "Type: " . $_FILES["file"]["type"] . "
";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
------解决方案--------------------
目标目录不存在
请不要自行处理错误
贴出 print_r($_FILES["file"]); 的结果
------解决方案--------------------
$type=strrchr($_FILES["file"]["name"],"."); //获取扩展名
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . time().$type); //以当前时间戳命名
------解决方案--------------------
以时间戳明名是不错,但是有其他的好的方法吗
------解决方案--------------------
当然能,跟原来一样,还是在upload文件夹里面。
------解决方案--------------------
在文件名的前面加上了时间, 可以看看time()函数.
------解决方案--------------------
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . time().$type); //以当前时间戳命名
当你用这一行代码的时候,文件名加上时间戳.好好检查一遍吧.
------解决方案--------------------
如果出现错误,一步一步的来排查
相关文章
相关视频
上一篇: PHP 代码加密
推荐阅读
-
php 上传图片,一直揭示Warning: move_uploaded_file(.upload/en.jpg): failed to open stream
-
php 上传图片,一直提示Warning: move_uploaded_file(.upload/en.jpg): failed to open stream
-
php 上传图片,一直揭示Warning: move_uploaded_file(.upload/en.jpg): failed to open stream
-
php 上传图片,一直提示Warning: move_uploaded_file(.upload/en.jpg): failed to open stream