...

View Full Version : rename not working, other options?



devinmaking
08-05-2012, 11:04 AM
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?

durangod
08-05-2012, 12:18 PM
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

devinmaking
08-05-2012, 12:54 PM
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?

AndrewGSW
08-05-2012, 02:53 PM
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().

devinmaking
08-05-2012, 03:33 PM
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.

DrDOS
08-05-2012, 04:30 PM
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.

Design gel code
08-05-2012, 04:36 PM
you could try it on a local type set up ?

tangoforce
08-05-2012, 05:56 PM
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.

devinmaking
08-06-2012, 10:38 AM
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:


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...

tangoforce
08-06-2012, 11:22 AM
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..



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.



for instance:


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.



Can you not use the / within the rename function,

Yes of course you can however WHERE you use it is more important.


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.

devinmaking
08-06-2012, 03:54 PM
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

tangoforce
08-07-2012, 01:52 PM
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.

Fou-Lu
08-07-2012, 10:55 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum