Jump to content

Upload a file to a selectable folder


neo71

Recommended Posts

I need to upload a file to a folder based on one of three possible categories


$folder1= “uploads/categoryA/”;
$folder2 = “uploads/categoryB/”;
$folder3 = “uploads/categoryC/”;

with a select I set the categories to the variable

then I upload

if (isset($_POST[‘btn-upload’])) {
if (isset($_FILES[‘file’]))
{ move_uploaded_file($file_loc,$folder1.$final_file); }

this works but I would like the file to be uploaded via $category in the corresponding $folder

Link to comment
Share on other sites

How does the User tell your code which "category" a file belongs to? 

Presumably, that would be another Form field, passed at the same time as the uploaded file itself. 
[Validate and then] Use that value to construct the target path for the file and pass that value to move_uploaded_file(). 

// pseudo-code
if ( isset( $_POST[ 'btn-upload' ] ) ) {
   if ( validatePostArguments( $_POST ) ) {
      $targetFile  = buildTargetPath( $_POST[ 'category' ], $_FILES[ 'file' ] ); 
      move_uploaded_file( $file_loc, $targetFile ); 
   }
}

Regards, 
   Phill W. 

Link to comment
Share on other sites

Thak u!. users choose a category by a select:

 

 

<fieldset>
Sellct category:<br>
<select name="siti" onchange="this.form.nome_mulino.disabled=(this.options[this.selectedIndex].value=='MOLINI')?false:true" >
<option value="MOLINI" selected="selected">ACQUA, FORZA MOTRICE. I MOLINI, RISORSA DI IERI E DI DOMANI</option>
<option value="VITA">LA VITA NEL FIUME</option>
<option value="FIUMI">I NOSTRI FIUMI VISTI DALL’ALTO</option>
		
Nome mulino<input type="text" name="nome_mulino" disabled="disabled" />
</select>
</fieldset>

$category = $_POST[‘siti’];

 

Link to comment
Share on other sites

1 hour ago, neo71 said:

$category = $_POST[‘siti’];

 

Or, perhaps better (i.e. safer) ... 

switch( $_POST[ 'siti' ] ) 
{
   case: 'FIUMI' : 
   case: 'MOLINI' : 
   case: 'VITA' : 
      $category = $_POST[ 'siti' ] ; 
      break; 
   default: 
      throw new \Exception( 'Invalid category!' ); 
}

Why? 
Just because you send the User an HTML SELECT list to use does not guarantee that the value you receive comes from that list! 

 

Regards, 
   Phill W. 

Link to comment
Share on other sites

This should help.

$category = $_POST['category'];

    $folder1 = "uploads/categoryA/";
    $folder2 = "uploads/categoryB/";
    $folder3 = "uploads/categoryC/";

     switch ($category) {
        case 'categoryA':
            $uploadFolder = $folder1;
            break;
        case 'categoryB':
            $uploadFolder = $folder2;
            break;
        case 'categoryC':
            $uploadFolder = $folder3;
            break;
        default:
            die("Invalid category");
    }

    // it check if the folder exists, if not, it create it
    if (!file_exists($uploadFolder)) {
        mkdir($uploadFolder, 0777, true);
    }

    $uploadedFile = $_FILES['file']['tmp_name'];
    $destination = $uploadFolder . $_FILES['file']['name'];

    if (move_uploaded_file($uploadedFile, $destination)) {
        echo "File successfully uploaded to $uploadFolder";
    } else {
        echo "Error uploading file";
    }

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.