用PHP替换文件 [英] Replace file in PHP

查看:136
本文介绍了用PHP替换文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在PHP中用另一个图片文件替换。两者都有相同的名称(123),但他们在不同的目录中,应该有不同的扩展名。我想用第二张图片替换第一张图片。


  1. ../ images / 123.gif

  2. ../ images / xxx / 123.png

是否可以使用任何函数?感谢您。

解决方案

移动,删除,复制等等都是基本的动作每当使用文件系统时都需要。因为这样的文件无疑将有你需要的所有信息。


  1. http://php.net/rename http://php.net/copy

  2. http:/ /php.net/unlink

你说你想用第二个文件替换第一个文件..但是你如果你重命名(即移动)它,那么该文件将不再存在于它的内容中,不要提到你想要发生的第二个图像的原始副本?



起始位置。如果你想让这个文件保留在这两个目录中,那么你应该使用 copy 来替代。

在这种情况下,所有你需要的是:

  rename('/ path / to / get / file.from','/ path / to / put /file.to'); 

注意:您可以使用相对的拍子(例如<$ c
$ b $ hr
$ b $ b

附加代码



 重命名('/ path / to / get / file.b','/ path /to/put/file.b'); 
unlink('/ path / to / remove / file.a');



工作示例



  rename('../ image / new / 8.jpg','../image/8.jpg'); //将新(jpg)文件移动到`../ image`目录
unlink('../ image / 8.gif'); //用gif扩展名删除旧文件


I would like to replace one image file with another in PHP. Both have same name (123), but they are in different directory and should have different extension. I want to replace first image with second image.

  1. ../images/123.gif
  2. ../images/xxx/123.png

Is it possible with any function? Thank you.

解决方案

Moving, deleting, copying etc... are all basic actions that are needed whenever working with file systems. As such the documentation will undoubtedly have all of the information you need.

  1. http://php.net/rename
  2. http://php.net/copy
  3. http://php.net/unlink

You say that you want to replace the first file with the second.. But you don't mention what you want to happen to the original copy of the second image?

If you rename (i.e. move) it then the file will no longer exist in it's starting location. If you want the file to remain in both directories then you should use copy instead.

In this case, all you need is:

rename('/path/to/get/file.from', '/path/to/put/file.to');

NOTE: You are able to use relative pats (e.g. ./ and ../)


Additional code

rename('/path/to/get/file.b', '/path/to/put/file.b');
unlink('/path/to/remove/file.a');

Working example

rename('../image/new/8.jpg', '../image/8.jpg'); //Moves new (jpg) file to `../image` directory
unlink('../image/8.gif');                       //Delete old file with gif extension

这篇关于用PHP替换文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆