Jump to content

Recommended Posts

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>

 

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 by Ch0cu3r

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.