Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts

    rename not working, other options?

    Hi guys

    i am a little stuck here.

    I want to be able to move a file that has already been uploaded to another folder.

    For instance:

    a user uploads and it goes to a temp folder called "upload_temp/image.jpg" now i would like to move it to "resources/user_images/profile/image.jpg"

    i have tried the obvious of rename() but this for some reason is not working.

    What other options are available?

  • #2
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,383
    Thanks
    264
    Thanked 32 Times in 31 Posts
    Have a look at this, i think its what your looking to do.

    http://php.net/manual/en/function.copy.php

    check the example on the page

  • #3
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by durangod View Post
    Have a look at this, i think its what your looking to do.

    http://php.net/manual/en/function.copy.php

    check the example on the page
    Unfortunatly the copy function doesnt work either, already tested, i did this before i tried the rename function.

    Could it be because i am testing on a windows platform on my local and not live?

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Could it be because i am testing on a windows platform on my local and not live?
    Possibly; try testing it live. Or change your browser's local-file access settings.

    I would consider move_uploaded_file().
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by AndrewGSW View Post
    Possibly; try testing it live. Or change your browser's local-file access settings.

    I would consider move_uploaded_file().
    Ive tried this also not working.

    Yet when i use move_uploaded_file to upload and place an image it works fine, but when an image is already placed within a folder its not working.

  • #6
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,974
    Thanks
    15
    Thanked 229 Times in 229 Posts
    You need the correct permissions, so you may need to chmod the folder, windows you may need the backslashes for folders. If you site is on a 'nix server you would make your life a lot easier if you installed a version of it and made a mirror for the site. You could install on an added hard drive, it wouldn't have to be very large, or even a new one, it could be in an old box you have around, just something so it could be an exact mirror.

  • #7
    New to the CF scene
    Join Date
    Aug 2012
    Location
    bury st edmunds suffolk
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post
    you could try it on a local type set up ?

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    It sounds like your host has disabled rename and copy functions.

    move_uploaded_file only works for files that are in the temp folder at the time of upload - not after.

    The only other option you have is to read out the file using file_get_contents() and then write it into another file with file_put_contents() assuming your host hasn't disabled those aswell. For larger files you'll need to read the files bit by bit whilst writing them so avoid those two functions for anything in the MB or GB range.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #9
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by tangoforce View Post
    It sounds like your host has disabled rename and copy functions.

    move_uploaded_file only works for files that are in the temp folder at the time of upload - not after.

    The only other option you have is to read out the file using file_get_contents() and then write it into another file with file_put_contents() assuming your host hasn't disabled those aswell. For larger files you'll need to read the files bit by bit whilst writing them so avoid those two functions for anything in the MB or GB range.
    Ive got the raname or copy working now but for some reason i cant get it to work as its not finding the file.

    I am even using the / to start from root.

    for instance:

    PHP Code:
    rename('/Control-Panel/TMP/' $image'/Control-Panel/Resources/Articles/Images/' rand(1,9999999) . $image); 
    Can you not use the / within the rename function, ive even tried the http:// wrapper because it does state that from 5.2.1 it is allowed but its not lol...

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote Originally Posted by devinmaking View Post
    Ive got the raname or copy working now but for some reason i cant get it to work as its not finding the file.
    So it was a file path issue then..

    Quote Originally Posted by devinmaking View Post
    I am even using the / to start from root.
    In other words you don't quite understand the path so you're guessing by using the / whereever you think it might make a difference. Thats not a great idea. What you need to do is find out your file path for definite. This is where debugging and printing things from $_SERVER come into play.

    Quote Originally Posted by devinmaking View Post
    for instance:

    PHP Code:
    rename('/Control-Panel/TMP/' $image'/Control-Panel/Resources/Articles/Images/' rand(1,9999999) . $image); 
    But what happens if you're already below 'Control-Panel' ? What happens if you're outside of it and in another directory? None of these you seem to understand so you need to get these principles clear in your head first before adding random / characters to the path.

    Quote Originally Posted by devinmaking View Post
    Can you not use the / within the rename function,
    Yes of course you can however WHERE you use it is more important.

    Quote Originally Posted by devinmaking View Post
    ive even tried the http:// wrapper because it does state that from 5.2.1 it is allowed but its not lol...
    Why are you trying to rename a file va http:// ? It's not a wrapper, it's a protocol. Unless you're using a webserver that will allow its files to be renamed via http (very rare and insecure) there is little point trying it - it will fail. The target server will refuse (or not even understand the request) because its been setup not to allow remote operations.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #11
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by tangoforce View Post
    Why are you trying to rename a file va http:// ? It's not a wrapper, it's a protocol. Unless you're using a webserver that will allow its files to be renamed via http (very rare and insecure) there is little point trying it - it will fail. The target server will refuse (or not even understand the request) because its been setup not to allow remote operations.
    Because i was despirate lol....

    The reason i am using the /Control-Panel/ is because the root is is at "/" so Control-Panel is the next directory, so even if the user was within the Control-Panel directory it would still find it from starting from the root.

    The file location from where the upload.php file is "TMP/Images/image.jpg" but this is not working either, i know where the file is and the path but its not finding it.

    Ive even tried doing "TMP/Images/image.jpg" and "./TMP/Images/image.jpg".

    Ive also tried: "dirname(__FILE__).'/TMP/Images/image.jpg'".

    As the TMP folder is within the Control-Panel as well as the upload.php the "dirname(__FILE__).'/TMP/Images/image.jpg" should find it, because mod writing is in place would this contribute to the problem

  • #12
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    IF your script is having trouble finding a file then you need to scan the current directory and see what is there using either glob() or scandir() and see what it can actually see at its current working location.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    It should be noted that php doesn't support rename through http wrappers, even if http rename is possible. I've never actually tried it to verify, but the http wrapper documentation specifies that its not possible.

    / is always the filesystem root. You do not have anything there you can access except potentially /tmp. What you mean to say is that its at the root of your site. PHP doesn't work the same has http. Specifying a / within the path of an http will resolve to the root of your public_html or whatever your document root is, while from PHP it resolves to filesystem root.

    You need to show us your paths here.
    If you do dirname(__FILE__) . '/TMP/Images/image.jpg' as a path, that indicates that from this file find the subdirectory path TMP/Images and give me image.jpg from within it. You can find your current directory using getcwd(), but that should be noted as not being equivilent to __DIR__ or dirname(__FILE__). getcwd gets the current working path and ignores paths within inclusions.

    I don't recommend the usage of paths from $_SERVER['DOCUMENT_ROOT']. As a server environment, it is not available from command line usage.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •