0

I have a filestructure that looks like this:

Folder_1

  Folder_2

    phpfile1.php

  Folder_3

    Folder_4

       random_pic.jpg

    Folder_5.

What should be the src and dest paths if I wanna copy/move randompic.jpg from Folder 4 to Folder 5?

I've tried:

  • I thought this is how absolute path would look.

    copy("/var/a/b/c/Folder_3/Folder_4/random_pic.jpg", "/var/a/b/c/Folder_3/Folder_5/random_pic.jpg") ;
    
  • relative to the last place it was, src relative to where the script is and dest relative to src.

    copy("../Folder_3/Folder_4/random_pic.jpg", "../Folder_5/random_pic.jpg");
    
  • both src and dest paths relative to where php script is.

    copy("../Folder_3/Folder_4/random_pic.jpg", "../Folder_3/Folder_5/random_pic.jpg");
    

All 3 returned PHP Warning ... Failed to open stream ... no such file or directory in ...

Barmar
  • 741,623
  • 53
  • 500
  • 612
cybera
  • 351
  • 2
  • 17
  • 2
    It's your choice. "relative" is more portable, "absolute" is safer for some things. there's no way to give a single right/wrong answer. it's whatever's best for YOU. – Marc B Sep 26 '16 at 21:58
  • Do you have an idea of why the 3 options I've tried don't work, am I misunderstanding what absolute and relative paths are? – cybera Sep 26 '16 at 22:00
  • @cybera: the first option is missing the leading slash. – jakub wrona Sep 26 '16 at 22:02
  • my bad I actually had the leading slashes in my code, tried it again just in case, still the same result. – cybera Sep 26 '16 at 22:05
  • I would suggest checking the directory permissions. – jakub wrona Sep 26 '16 at 22:08
  • @jakubwrona That would cause a different error. – Barmar Sep 26 '16 at 22:09
  • The absolute version is missing `Folder_1` in the path. – Barmar Sep 26 '16 at 22:10
  • @jakub wrona r, w, x for owner, group and others are all checked. uid, gid and sticky bit are unchecked. its a linux machine. – cybera Sep 26 '16 at 22:11
  • @barmar, my bad again, I'm bad at giving examples, but all the directories are there in the actual script. – cybera Sep 26 '16 at 22:13
  • You must be missing something. Otherwise it would work. – Barmar Sep 26 '16 at 22:13
  • What does `echo getcwd();` show? – Barmar Sep 26 '16 at 22:14
  • @barmar, I'm coping the path directly from WinSCP, I don't see how I can copy it wrong. – cybera Sep 26 '16 at 22:15
  • @barmar echo getcwd(); shows the absolute path of the php script. both before and after the copy line. In the example that would be: /var/a/b/c/Folder_1/Folder_2 – cybera Sep 26 '16 at 22:18
  • Does `Folder_5` already exist? `copy()` and `rename()` won't create directories by themselves. – Barmar Sep 26 '16 at 22:20
  • Does the error message refer to the source or target filename? – Barmar Sep 26 '16 at 22:20
  • @Barmar Yes Folder_5 actually exists, I created it manually. The PHP error refers to the src. Failed to open stream doesn't refer anything and no such file or directory refers to the copy line of the php script. – cybera Sep 26 '16 at 22:24
  • Does `glob("/var/a/b/c/Folder_1/Folder_3/Folder_4/*")` return anything? Maybe you have the folders correct but the filename wrong? – Barmar Sep 26 '16 at 23:36
  • Its fixed, it worked. Thank you for all the help. I wish there was a up tick somewhere to give you an upvote. – cybera Sep 27 '16 at 12:31

1 Answers1

0

You should always only use absolute paths. You can read about why and how here : PHP - Failed to open stream : No such file or directory

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76