如何重命名带有句点(.)的 PHP 图像的名称?

huangapple go评论75阅读模式
英文:

How do I rename a php image that has a period (.) in the name

问题

我正在尝试使用PHP的rename()函数来重命名一个文件名中带有句点的图像。

rename("images/cat.cute.jpg", "images/cute-cat.jpg");

目前出现了"找不到文件或目录"错误。

英文:

Im trying to rename an image that has a full stop in the name with php using the rename() function.

rename("images/cat.cute.jpg", "images/cute-cat.jpg");

Currently getting a "No such file or directory" error.

答案1

得分: 1

当你指定images/cat.cute.jpg时,你实际上是在指定一个相对于你的PHP脚本执行位置的路径。你收到的错误只是在告诉你,在与脚本执行位置相对的images文件夹中没有名为cat.cute.jpg的文件,或者根本不存在这样一个images文件夹。要解决这个问题,你需要指定正确的相对路径,或者你可以指定一个绝对路径。以下是一个使用绝对路径的rename()的示例:

rename("/images/cat.cute.jpg", "/images/cute-cat.jpg");

这假设相对于根目录存在一个images/文件夹,但这可能并非实际情况,你可以根据需要进行调整。

英文:

When you specify images/cat.cute.jpg, you are specifying a path relative to wherever your PHP script is actually executing. The error you are getting is simply telling you that there is no file called cat.cute.jpg in the folder images relative to the execution location of the script. Or, such a folder images does not exist. To fix this, you either need to specify the correct relative path, or you may specify an absolute path. Here is an example of using rename() with absolute paths:

rename("/images/cat.cute.jpg", "/images/cute-cat.jpg");

This assumes that there is an images/ folder relative to root, which may not be the case, but you can adjust as you need.

huangapple
  • 本文由 发表于 2020年1月3日 15:41:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定