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