php @touch,touch - [ php中文手册 ] - 在线原生手册 - php中文网

用户评论:

[#1]

csouth at xistins dot com [2014-12-14 08:41:04]

An earlier comment referenced a code snippet that showed file_put_contents() was faster the touch for creating files. I re-ran the same tests on PHP 5.5.9 and this seems to no longer be the case.

$startTime=microtime(true);

for($i=0;$i<100000;$i++)

{file_put_contents('dir/file'.$i,'');unlink('dir/file'.$i);

}

echo"Time: ".(microtime(true)-$startTime)."\n";// Time: 2.6902809143066$startTime=microtime(true);

for($i=0;$i<100000;$i++)

{touch('dir/file'.$i);unlink('dir/file'.$i);

}

echo"Time: ".(microtime(true)-$startTime)."\n";// Time: 2.3343770503998?>

[#2]

anon [2013-11-10 16:38:56]

Note that when PHP is called by f.e. apache or nginx instead of directly from the command line, touch() will not prefix the location of the invoking script, so the supplied filename must contain an absolute path.

With script started from /home/user/www, this will not touch "/home/user/www/somefile":

touch('somefile');?>

But this will:

touch(__DIR__.'/somefile');?>

[#3]

bosslog at gmail dot com [2010-10-08 14:11:13]

I has passed a small test to check which function is faster to create a new file.

file_put_contents vs touch

{file_put_contents('dir/file'.$i,'');

}?>

Average time: 0,1145s

{touch('dir/file'.$i);

}?>

Average time: 0,2322s

So, file_put_contents is faster than touch, about two times.

[#4]

chris dot dallaire at csquaredsystems dot com [2010-04-09 07:41:54]

I needed to use this to touch the /etc/cron.d directory when I updated some files in there. I know the docs say this isn't necessary, but I'm finding that i need to do it in order form my changes to be picked up quickly.

I ran into the permissions error as well and I found that using chmod 777 /etc/cron.d does the trick.

So, you should be able to use the PHP touch function on a directory that has open write access.

Of course, this isn't the most secure approach, but in our application it's not a big deal for that folder to not be super secure.

[#5]

ernst at cron-it dot de [2009-09-29 06:38:30]

To touch a file without being owner, it is much easier:

}?>

[#6]

info at archiwumrocka dot art dot pl [2008-12-01 13:59:50]

Only way to change modification date in catalogue is to create file in via touch() and dalete it with unlink():

$dir='temp';$files1=scandir($dir);$files1=array_slice($files1,2);

foreach ($files1as$key=>$val)

{

if (!is_dir($val)) continue;

if (!touch($val))

{touch($val."/plik.txt");unlink($val."/plik.txt");

}

}?>

[#7]

mrgrier at yahoo dot com [2008-11-29 10:29:55]

At least on Linux, touch will not change the time on a symlink itself, but on the file/directory it points to. The only way to work around this is to unlink the symlink, then recreate it.

It took a bit of searching to discover this. The OS itself provides no way to do it. Many people wondered why anyone would want to do this. I use symlinks inside a web tree to point to files outside the web tree. After a certain length of time has passed, I want the symlinks to die, so the files cannot be successfully hotlinked.

[#8]

ddalex at gmail dot com [2008-11-03 09:25:05]

Actually, Glen is right, PHP won't touch if it is not the current owner of the file, even if the directory and files are writeable by the PHP user.

[#9]

Radon8472 [2008-08-08 05:07:37]

Important info:

touch() used on a directory always returns FALSE and prints "Permission denied" on NTFS and FAT Filesystem (tested on winXP).

[#10]

Jeff [2008-06-26 12:29:10]

I've been trying to set a filemtime into the future with touch() on PHP5.

It seems touch $time has a future limit around 1000000 seconds (11 days or so). Beyond this point it reverts to a previous $time.

It doesn't make much sense but I could save you hours of time.

$time = time()+1500000;

touch($cachedfile,$time);

[#11]

Glen [2007-10-18 13:01:58]

In unix on the command-line, you can touch files you don't own - but like other comments on this page state - PHP's built in touch won't work.

I simple alternative (on unix):

{exec("touch{$filename}");

}?>

[#12]

Charles Belov [2006-07-18 18:10:14]

Update the access time without updating the modified time:

Unix command: touch -a filename

PHP: touch(filename, date('U', filemtime(filename)), time())

[#13]

spam at webmastersguide dot com [2005-09-01 05:09:00]

If you're going to go around deleting (unlinking) files

that you don't own just in order to change the modification

time on the file, you darn well better chown() the file

back to it's original ownership after you are done and

chmod() it back to it's correct permissions.  Otherwise

you will almost certainly break things.  Additionally the

code listed for touch()ing a file you don't own should

set the file creation time back to it's original time if

what is wanted is to just change the modification time.

Also, the code listed will break things if there is an i/o

error such as disk full or too many files in the directory.

Here's how the code SHOULD be written:

Create the new file FIRST, rather than last, with a different

name such as $file.tmp.

Read the ownership, permissions, and creation time of the old file.

Set permissions and creation time of the new file the same as the old.

Rename the new file to the name of the old.

chown() the new file to the user that owned the file it's replacing.

Please be careful adding to the documentation if you've

never taken programming 101.

[#14]

rf_public at yahoo dot co dot uk [2005-07-25 12:19:14]

Note: the script to touch a file you don't own will change it's owner so ensure permissions are correct or you could lose access to it

[#15]

guy at forster design dot com [2005-05-12 03:42:33]

Here's a little workaround that allows the PHP user to touch a file it doesn't own:

$target_file="/path/to/file/filename.txt";//system filepath to your file$file_content=implode("",file($target_file));

@unlink($target_file);

if($savetofile=fopen($target_file,"w")) {fputs($savetofile,$file_content);fclose($savetofile);

}$new_date=strtotime("23 April 2005");// set the required date timestamp heretouch($target_file,$new_date);?>

Of course, PHP needs to have write access to the folder containing the file you want to touch, but that should be easy to arrange.

[#16]

feathern at yahoo dot com [2002-08-13 15:31:07]

Neat little script that will give you a list of all modified files in a certain folder after a certain date:

$filelist = Array();

$filelist = list_dir("d:\\my_folder");

for($i=0;$i

$test = Array();

$test = explode("/",date("m/d/Y",filemtime($filelist[$i])));

//example of files that are later then

//06/17/2002

if(($test[2] > 2001) && ($test[1] > 16) && ($test[0] > 5)){

echo $filelist[$i]."\r\n";

}

clearstatcache();

}

function list_dir($dn){

if($dn[strlen($dn)-1] != '\\') $dn.='\\';

static $ra = array();

$handle = opendir($dn);

while($fn = readdir($handle)){

if($fn == '.' || $fn == '..') continue;

if(is_dir($dn.$fn)) list_dir($dn.$fn.'\\');

else $ra[] = $dn.$fn;

}

closedir($handle);

return $ra;

}

[#17]

emilebosch at hotmail dot com [2001-10-06 15:41:56]

To spare you ppl couple of hours of valuable time, you can only TOUCH a file that you own! Usually PHP is *nobody*

Warm regards,

Emile Bosch

[#18]

master at dreamphp dot com [2001-05-15 11:23:09]

$filename = "test.dat";

if (!file_exists($filename)) {

touch($filename); // Create blank file

chmod($filename,0666);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值