Files not being uploaded in dynamically created folders/subfolders

落花浮王杯 提交于 2019-12-08 05:17:37

问题


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.



来源:https://stackoverflow.com/questions/19817829/files-not-being-uploaded-in-dynamically-created-folders-subfolders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!