ArshSingh Posted February 26, 2014 Share Posted February 26, 2014 im using the following code to move the files from my current folder (in url and server same named folder) to another folder like this ; i have current folder like this : uploads/admin/file.jpg and i want to move my file to ; uploads/admin/folder/file.jpg ,,, with following code : <?php $folderon = $_REQUEST['folder-in']; $file_pr = $_REQUEST['filename']; $file = "uploads/$loggedInUser->username$folderon$file_pr"; $folder_in = $_REQUEST['foldername']; $movefile = copy($file, "uploads/$loggedInUser->username$folderon$folder_in/" . $file_pr); //This script can move a file header("Location: " . $_SERVER['HTTP_REFERER']); ?> html form : <form action="models/move.php" method="POST" autocomplete="off"> <input type="text" value="filename" onblur="if(value=='') value = 'filename'" onfocus="if(value=='filename') value = ''" name="filename" maxlength="30" /> <input type="text" value="<?php echo "$uplfolder"; ?>" name="folder-in" style="display: none;"/> <button>Move</button> <input type="text" value="foldername" onblur="if(value=='') value = 'foldername'" onfocus="if(value=='foldername') value = ''" name="foldername" maxlength="30" style="margin: 15px 0px 15px -5px;" /> </form> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted February 26, 2014 Share Posted February 26, 2014 (edited) You need to add the directory separators (the / ) in your file paths. $file = "uploads/{$loggedInUser->username}/$folderon/$file_pr"; $movefile = copy($file, "uploads/{$loggedInUser->username}/$folderon/$folder_in/" . $file_pr); //This script can move a file Also note copy does not move a file, but makes a copy of the original file. If you want to physically move the file you'll want to use rename(). Before moving the file you will also want to make sure the the place where the file is being moved to exists. using file_exists(). If it doesn't exists you will want use mkdir() to create the destination folder(s) beforehand. if you're using a *nix based server you'll want to make the uploads/ folder writeable so php can create files and folders Edited February 26, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
ArshSingh Posted February 26, 2014 Author Share Posted February 26, 2014 You need to add the directory separators (the / ) in your file paths. $file = "uploads/{$loggedInUser->username}/$folderon/$file_pr"; $movefile = copy($file, "uploads/{$loggedInUser->username}/$folderon/$folder_in/" . $file_pr); //This script can move a file Also note copy does not move a file, but makes a copy of the original file. If you want to physically move the file you'll want to use rename(). Before moving the file you will also want to make sure the the place where the file is being moved to exists. using file_exists(). If it doesn't exists you will want use mkdir() to create the destination folder(s) beforehand. if you're using a *nix based server you'll want to make the uploads/ folder writeable so php can create files and folders my server is linux based with safe_mode off and i want to make dir with php and html form but its not working (users can manualy make dir with their own desired name) . Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 26, 2014 Share Posted February 26, 2014 Turn error reporting on, it would help. Quote Link to comment Share on other sites More sharing options...
tunage Posted February 27, 2014 Share Posted February 27, 2014 also check your permissions. file close, write move etc... will bork if anything is not apache:apache or what ever your distro web server user/group is. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.