Files not being uploaded in dynamically created fo

2019-06-07 04:09发布

问题:

i need help to solve this problem Files not being uploaded in dynamically created folders/subfolders!

creating a dynamically subfolder using input type text and when i uploaded file moved to uploads folders but not moved to subfolder which is create using input type text?

but dynamically creating function working fine and also showing me folder which is typed in the textbox into the upload folder

Here My Code

//creating dynamically subfolders 
$folder = $_POST['folder'];
foreach( $folder as $key => $value){
$dirPath = 'uploads/'.$value;
$result = mkdir($dirPath);
}

if ($result == '1') {

//file move on function
$target_path = 'uploads/'.$results;
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
" has been uploaded";

} else{
echo "There was an error uploading the file, please try again!";
}
} else {
echo $dirPath . " has NOT been created";
}
}

<form method="post" enctype="multipart/form-data">
<input name="uploadedfile" type="file" /><br />
<input type="text"  id="folder" name="folder"><br />
<input type="submit" name="test" value="Upload File" />
</form>

My Problem Is Solved Now I Have Completed My Script

//creating a folder 
$folder = $_POST['folder'];
$dirPath = 'uploads/'.$folder;
$result = mkdir($dirPath);

if ($result == '1') {

//file move on        
$target_path = $dirPath .'/' . basename( $_FILES['uploadedfile']['name']); 

  if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
     echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
" has been uploaded";

  } else{
     echo "There was an error uploading the file, please try again!";
  }

 } else {
   echo $dirPath . " has NOT been created";
 }
}

回答1:

Try this,

Replace

 $target_path = 'uploads/'.$results; 

into

$target_path = $dirPath.'/';

Otherwise,

$target_path = $dirPath .'/'. basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}


回答2:

Where is $results defined? You are using it here:

$target_path = 'uploads/'.$results;

Why?

Also, $result is the true or false return of mkdir() so you can't use that one either.