Problems with moving selected images to new or existing folder

I am having problem with this script

$dirname = "tempfolder"
.
.
.
.
.
.

if(isset($_POST['submit'])){//to run PHP script on submit
    if(!empty($_POST['image_select'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['image_select'] as $selected){
echo $selected."</br>";
}
}

if (!file_exists('uploads'.$dirname)){
    mkdir('uploads/'.$dirname);
} 
move_uploaded_file ($selected, 'uploads/$dirname');   
}

The array image_select comes from an array from a checkbox tag in the html tag
It keeps saying that the file exists when running the script on line
mkdir(‘uploads/’.$dirname) even though I put the ! to mean that if the folder doesn’t exist, then make the folder with the name
of what is held (for temporary sakes) in variable $dirname

What I am doing is select images from a folder, which then moves it into another folder after being selected, the folder has not to exists
for the mkdir() to be executed in the if() statement

Agh!! Found out that this should read

if (!file_exists(‘uploads/’.$dirname)){

But still have the problem of the file(s) not uploading

Try setting the following and see if there are any errors or warnings

if (!file_exists(‘uploads’.$dirname)){
echo ‘uploads’.$dirname; // ONLY FOR DEBUGGING
error_reporting(-1); // set to maximum // ONLY FOR DEBUGGING
ini_set(‘display_errors’, true); // ONLY FOR DEBUGGING
mkdir(‘uploads/’.$dirname);
}
move_uploaded_file ($selected, ‘uploads/$dirname’);
}
die; // ONLY FOR DEBUGGING

Hi there John,

I did that, but that was after I had found a typo in my script, it isn’t coming up with any error messages with what you suggested

It seems that I have now fixed the why the error message was occurring, but it isn’t moving the file(s) to the appropriate folder

I would hazard a guess that it did move your file. Check the uploaded folder for $dirname.

Single quotes are required around uploads/$dirname or '‘uploads/’ .$dirname

Try and echo the destination to view the path followed by die;

Tapped laboriously from a tablet :frowning:

move_uploaded_file is only for moving files uploaded to a PHP form. The filename of the uploaded file will appear in the $_FILES superglobal.

John, I did check the folder but there is no file there, just an empty folder

Michael, I will give anything a go, if you can give me a gist of what you are trying to say

Your code shows the use of the $_POST array which always sends Strings. So after you create the folder, you’re trying to move a String into it.
If you want to move a File into it the form needs type=“file” inputs and the code needs to use the $_FILES array to move them into the folder.

What does the form mark-up look like?

This is what my markup looks like

<body>
        <form action="#" method="post">
        <div id="container1" class="container">
            <img width="400" height="640" src="images/1.jpg">
        <input class="cbox" type="checkbox" name="image_select[]" value="1.jpg" />
</div>

        <div id="container2" class="container">
            <img width="400" height="640" src="images/2.jpg">
        <input class="cbox" type="checkbox" name="image_select[]" value="2.jpg" />
</div>

        <div id="container3" class="container">
            <img width="400" height="640" src="images/3.jpg">
        <input class="cbox" type="checkbox" name="image_select[]" value="3.jpg" />
</div>

        <div id="container4" class="container">
            <img width="400" height="640" src="images/4.jpg">
        <input class="cbox" type="checkbox" name="image_select[]" value="4.jpg" />
</div>

        <div id="container5" class="container">
            <img width="400" height="640" src="images/5.jpg">
        <input class="cbox" type="checkbox" name="image_select[]" value="5.jpg" />
</div>

        <div id="container6" class="container">
            <img width="400" height="640" src="images/6.jpg">
        <input class="cbox" type="checkbox" name="image_select[]" value="6.jpg" />
</div>

There are 6 files</br>            
        <input type="submit" name="submit" value="Submit"/>
        </form>

    </body>

Hi,
I know this doesn’t answer your question but why not make this into a loop?

<?php         
while($i < 7): ?>
            <div id="container<?php echo($i); ?>" class="container">
        <img width="400" height="640" src="images/<?php echo($i); ?>.jpg">
    <input class="cbox" type="checkbox" name="image_select[]" value="<?php echo($i); ?>.jpg" />
    <?php $i++;
    endwhile; ?>

Just a thought.
Also could you show the whole form HTML?

I believe your form will also need
multipart/form-data
http://php.net/manual/en/features.file-upload.php

Are the images uploading OK so that they can be moved?

I have already made this into a loop

I think I may have misunderstood the problem.

The images have already been uploaded successfully and are on the server?

What does the move_uploaded_file function look like?

It looks like this

move_uploaded_file ($selected, ‘uploads/’.$dirname);

Should’t the move_uploaded_file() be within the foreach loop, or did I misunderstand the structure of the code? Seems that all you do inside the foreach loop is display the filename.

But it’s passed to the move_uploaded_file function later on.

@cssbonding that’s the line of code that calls the function. I’d like to see the function itself. eg.

function move_uploaded_file($arg1, $arg2) {
  // does stuff
}

@Mittineague, I must be missing that bit:

if(isset($_POST['submit'])) {  //to run PHP script on submit
   if(!empty($_POST['image_select'])) {
      // Loop to store and display values of individual checked checkbox.
      foreach($_POST['image_select'] as $selected) {
         echo $selected."</br>";
         } // closes the foreach loop
      } // closes the empty() check
      if (!file_exists('uploads'.$dirname)){
         mkdir('uploads/'.$dirname);
         } // closes the check for the file already existing
      move_uploaded_file ($selected, 'uploads/$dirname');   
   } // closes the check for 'submit'

As image_select is an array, the loop runs around it but by the time we get to the function to move the files, all we have is the last value of $selected. Or perhaps nothing, I’m not sure. Or I’m being thick.

As nice as it would be to simply tell PHP to do something and have it be done, it doesn’t work that way :wink:

If you’re calling a function it needs to be there somewhere. Maybe it’s in an include()

Yes, fair point, but in the code above it doesn’t seem to call that function for each value of image_select in any case, only for whatever value of $selected is left over at the end of the loop, if any.

Ah, thanks, I missed seeing that $selected is limited in scope
foreach($_POST['image_select'] as $selected){

So the function call needs to be in the loop unless it can take an array that could be created in the loop.