/    Sign up×
Community /Pin to ProfileBookmark

How to move listBox selected file from one folder to another using Ajax

Hi,

I have updated select (listBox1) with files from folder.
How to move selected file from one folder in to another one?
For updating listBox with files a have this code:

In html i have this:
`<select id=”listBox1″ name=”listBox1″ class=”listBox” multiple></select>`
`<input type=”submit” id=”buttonMove1″ name=”buttonMove1″ class=”buttonMove1″ value=”Move”></input>`

in ( get-images.php ) file i have this:

[code]
<?php
if (isset($_POST[‘dir’])) {
$dir = $_POST[‘dir’];
$files = glob($dir . ‘*’);
foreach ($files as $file) {
echo ‘<option data-dir=”‘ . $dir . ‘”>’ . pathinfo($file, PATHINFO_BASENAME) . ‘</option>’;
}
}
?>
[/code]

and in Jquery-Ajax i have this:

[code]
$(function(){
$(“#button1”).click(function(e){

$.ajax({

url: ‘get-images.php’,
type: ‘POST’,
data: { dir: “./osaasto1/maanantai/”},
success: function (response) {

$(‘#listBox1’).empty().append(response);

}
});
[code]

Now i would like to move selected file from path: “./osaasto1/maanantai/” to path: “./osaasto1/valmis/” when buttonMove1 is clicked, and remove selected option from listBox1 .

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMar 01.2020 — Moving a file can be done by the funktion rename():

https://www.php.net/manual/en/function.rename.php

Create a second php script, e. g. move-image.php and call it by ajax, handing over the image path as a POST parameter. Where should the destination directory be defined? Fixed in the php script or as another POST parameter?
Copy linkTweet thisAlerts:
@AhmoauthorMar 01.2020 — Thank you for replay again:)

Hire is how to move file from one folder to another,and it works.

This is if we know which file it is.

Is it good for now :)?

move-image.php file
<i>
</i>&lt;?php
if (isset($_POST['destDir'])) {
$destDir= ($_POST['destDir']);
rename("./osaasto1/maanantai/image1.jpg", "$destDir image1.jpg");
}

?&gt;



Ajax code
[code]
$(function(){
$("#buttonMove1").click(function(e){
$.ajax({


url: 'move-image.php',
type: 'POST',
data: { destDir: "./osaasto1/valmis/"},
success: function (response) {

//..//

}
});

e.preventDefault();
});
});
[code]
Copy linkTweet thisAlerts:
@AhmoauthorMar 01.2020 — I dont know who you are, but you should be proud of yourself. I learned a lot from you in only 3 days.

Hire is my cod.I have to only remove selected option from listBox but that is not difficult.

move-image.php file

<i>
</i>&lt;?php
if (isset($_POST['pathDir']) &amp;&amp; !empty($_POST["pathDir"])) {
$pathDir= ($_POST['pathDir']);
$data = $_POST['name'];
$destDirectory = "osaasto1/valmis/";
$file = $data;

<i> </i>rename($pathDir.$data, $destDirectory.$data);
}

?&gt;



Ajax code

[code]
$(function(){


document.getElementById('listBox1').addEventListener('change', function(){
$('#listBox1 option').each(function() {
if (this.selected){

$("#buttonMove1").click(function(e){


var path= "./osaasto1/maanantai/"
var selectedFileName = $("#listBox1 option:selected").text();


$.ajax({
type: "POST",
url: "move-image.php",
data: {pathDir: path, name: selectedFileName}
}).done(function( respond ) {
//...
});
e.preventDefault();
});

}

});

});
});
[code]
Copy linkTweet thisAlerts:
@SempervivumMar 01.2020 — Fine that it's working. Thank your for your nice words!
×

Success!

Help @Ahmo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...