3

I need to modify the file date change using my php script. At my localhost, I used XAMPP running in Windows 7. I had no problem using PHP touch and is working properly as I want it to be.

Yet, when I uploaded it to my production, LINUX OS the PHP touch is not working anymore. I investigated it and found that, Linux, doesn't allow PHP touch or doesn't allow anybody to change file mod date.

That's why I use exec("touch filename.txt") instead and it working properly, but when I use this code

exec("touch -t 201204040000.00 filename.txt");

It doesn't do what it must, am I missing something here?

These are my references:

EDIT

ls -l filename.txt
-rw-r--r-- 1 2012-11-04 12:00 filename.txt //supposed that 2012-11-04 12:00 is the original mod date of the file

If i run this code:

exec("touch filename.txt");
ls -l filename.txt
-rw-r--r-- 1 2012-11-05 11:00 filename.txt //supposed that 2012-11-05 11:00 is the current timestamp

The above code is working properly on me as everyone see. But if i run it like this:

exec("touch -t 201204040000.00 filename.txt");
ls -l filename.txt
-rw-r--r-- 1 2012-11-05 11:00 filename.txt //The mod date doesn't changed at all.
sephoy08
  • 1,104
  • 7
  • 16
  • What error are you getting when trying to `touch()` the file? It's likely the same reason why your exec is not working. You are probably getting an error message back from exec() – Pekka Nov 05 '12 at 09:12
  • What's the return of the exec? – Teejay Nov 05 '12 at 09:14
  • I assume you already checked if the user that is running the PHP script (the user that also runs Apache, most likely) has access and modification rights to the file you are trying to `touch`. This is a non-issue on Windows because the users are one and the same. – Victor Stanciu Nov 05 '12 at 09:15
  • Also have you tried running different commands to check if you actually have shell access? ` – Chris Hasiński Nov 05 '12 at 09:15
  • @Pekka there is no error, `touch()` is bool so it will only gives, true or false. i read that in linux, it doesn't allow PHP touch if its not the owner it strange that if i dont include the `-t 201204040000.00` it runs properly. however, on my referrence below, my syntax is okay. – sephoy08 Nov 05 '12 at 09:17
  • @KrzysztofHasiński: Yes i already tried that and it outputs `apache` @Teejay: none @VictorStanciu: Yes, i already did. – sephoy08 Nov 05 '12 at 09:19
  • You need to debug your exec() call and see what error message is returned. – Pekka Nov 05 '12 at 09:23
  • i already do this, `echo exec("touch -t 201204040000.00 filename.txt");` it doesn't output anything – sephoy08 Nov 05 '12 at 09:25
  • did you try shell_exec instead of exec? – Teejay Nov 05 '12 at 09:29
  • See [How can I debug exec() problems?](http://stackoverflow.com/q/12199353) use the `2>&1` the OP uses. It should give you the error message. – Pekka Nov 05 '12 at 09:50

1 Answers1

1

I ran your command and it does exactly what it's supposed to do:

php -r 'exec("touch -t 201204040000.00 filename.txt");'
ls -l filename.txt
-rw-r--r-- 1 2012-04-04 00:00 filename.txt

maybe if you tell us what you expect we can help you more.

iabdalkader
  • 17,009
  • 4
  • 47
  • 74
  • that exactly what i want to achieve. though it doesn't give me that output. – sephoy08 Nov 05 '12 at 09:22
  • i hope i can, but i dont have access to that. – sephoy08 Nov 05 '12 at 09:24
  • @sephoy08 if you don't have permissions that's a whole different story, and has nothing to do with PHP – iabdalkader Nov 05 '12 at 09:25
  • @mux I think sephoy08 intended it doesn't get the same result – Teejay Nov 05 '12 at 09:26
  • @mux: `exec("touch filename.txt");` this code is running perfectly with me. what i dont understand is, why is that when i added `-t 201204040000.00 ` it doesn't do anything. – sephoy08 Nov 05 '12 at 09:28
  • @sephoy08 what do you expect it to do ? note that -t sets a specific time stamp, so if you run it more that once it won't change the timestamp of the file. – iabdalkader Nov 05 '12 at 09:36
  • @mux: i totally understand that, but, in my case, it doesnt change anything at all – sephoy08 Nov 05 '12 at 09:39
  • @sephoy08 that's what I'm trying to explain, it shouldn't change the timestamp (except) for the first time it's ran, if that doesn't explain it, then please update the question and add the expected/actual result of running the command. – iabdalkader Nov 05 '12 at 09:41
  • @mux: i updated my question with clear situation discription. – sephoy08 Nov 07 '12 at 07:07
  • @sephoy08 what happens when you try the touch -t ... from the command line ? – iabdalkader Nov 07 '12 at 07:14
  • @mux i execute those code in `exec();` and about your question, nothings happened and nothing is returned – sephoy08 Nov 07 '12 at 07:18