Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-05-2012, 10:04 AM   PM User | #1
devinmaking
Regular Coder

 
Join Date: Oct 2011
Posts: 236
Thanks: 11
Thanked 5 Times in 5 Posts
devinmaking has a little shameless behaviour in the past
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?
devinmaking is offline   Reply With Quote
Old 08-05-2012, 11:18 AM   PM User | #2
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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
durangod is offline   Reply With Quote
Old 08-05-2012, 11:54 AM   PM User | #3
devinmaking
Regular Coder

 
Join Date: Oct 2011
Posts: 236
Thanks: 11
Thanked 5 Times in 5 Posts
devinmaking has a little shameless behaviour in the past
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?
devinmaking is offline   Reply With Quote
Old 08-05-2012, 01:53 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
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
AndrewGSW is offline   Reply With Quote
Old 08-05-2012, 02:33 PM   PM User | #5
devinmaking
Regular Coder

 
Join Date: Oct 2011
Posts: 236
Thanks: 11
Thanked 5 Times in 5 Posts
devinmaking has a little shameless behaviour in the past
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.
devinmaking is offline   Reply With Quote
Old 08-05-2012, 03:30 PM   PM User | #6
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,155
Thanks: 10
Thanked 148 Times in 148 Posts
DrDOS is infamous around these parts
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.
DrDOS is offline   Reply With Quote
Old 08-05-2012, 03:36 PM   PM User | #7
Design gel code
New to the CF scene

 
Join Date: Aug 2012
Location: bury st edmunds suffolk
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
Design gel code is an unknown quantity at this point
you could try it on a local type set up ?
Design gel code is offline   Reply With Quote
Old 08-05-2012, 04:56 PM   PM User | #8
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,520
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 08-06-2012, 09:38 AM   PM User | #9
devinmaking
Regular Coder

 
Join Date: Oct 2011
Posts: 236
Thanks: 11
Thanked 5 Times in 5 Posts
devinmaking has a little shameless behaviour in the past
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...
devinmaking is offline   Reply With Quote
Old 08-06-2012, 10:22 AM   PM User | #10
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,520
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 08-06-2012, 02:54 PM   PM User | #11
devinmaking
Regular Coder

 
Join Date: Oct 2011
Posts: 236
Thanks: 11
Thanked 5 Times in 5 Posts
devinmaking has a little shameless behaviour in the past
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
devinmaking is offline   Reply With Quote
Old 08-07-2012, 12:52 PM   PM User | #12
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,520
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 08-07-2012, 09:55 PM   PM User | #13
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:52 PM.


Advertisement
Log in to turn off these ads.