Jump to content

can't move file from current folder to another


ArshSingh

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

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

Archived

This topic is now archived and is closed to further replies.

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